winforms 如何退出在系统托盘中运行的powershell脚本?

lzfw57am  于 2023-03-19  发布在  Shell
关注(0)|答案(1)|浏览(146)

我想用鼠标左键单击文本“Quit "来退出这个系统托盘程序。当我悬停时,鼠标显示一个旋转的蓝色图标,我单击它什么也不做。脚本有什么问题吗?

# a systray program, that should be exited (but it doesn't)
# 2023-03-18

$iconPath = "H:\Dropbox\400 - Scriptprogrammierung\Powershell\Taskleiste mit Wochentag\icons\ico\Logo.ico" # icon path
Write-Host -ForegroundColor Yellow $iconPath
$tooltip = "This is a text."

# NotifyIcon-object
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon
$notifyIcon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($iconPath)
$notifyIcon.Text = $tooltip

########################
# Here seems to be the problem...
$contextMenu = New-Object System.Windows.Forms.ContextMenuStrip
$menuItemExit = New-Object System.Windows.Forms.ToolStripMenuItem
$menuItemExit.Text = "Quit."
$contextMenu.Items.Add($menuItemExit)
$notifyIcon.ContextMenuStrip = $contextMenu
$menuItemExit.add_Click({ $notifyIcon.Dispose(); exit })
########################

# Show icon in systray
$notifyIcon.Visible = $true

# Loop
while ($true) {
    $notifyIcon.Text = $tooltip
    Start-Sleep -Seconds 60 # wait 60 seconds
}
brtdzjyr

brtdzjyr1#

所需的关键变革包括:

  • 不要直接从.add_Click()事件处理程序脚本块调用exit:会让你的剧本崩溃的。
  • 下面的代码还将$notifyIcon.Dispose()移出此脚本块,并将其移入try语句的finally块,该语句 Package while循环,并通过脚本级$done变量通知循环希望退出,该变量通过事件处理程序(在脚本的 child 作用域中运行)中的$script:done = $true设置。
  • 这可确保使用Ctrl-C终止脚本时也能正确处理图标并将其从通知区域中删除。
    *while循环中,您必须定期调用[System.Windows.Forms.Application]::DoEvents(),以便WinForms处理其UI事件。在这些调用之间仅休眠一个 * 短 * while,以便保持UI响应-长时间休眠将在休眠期间阻止事件处理。
# Load the WinForms assembly.
Add-Type -AssemblyName System.Windows.Forms

$iconPath = "H:\Dropbox\400 - Scriptprogrammierung\Powershell\Taskleiste mit Wochentag\icons\ico\Logo.ico" # icon path
$tooltip = "This is a text."

# NotifyIcon-object
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon
$notifyIcon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($iconPath)
$notifyIcon.Text = $tooltip

# Define a script-level variable that indicates whether the
# script should be exited, to be set to $true from the .add_Click() event handler.
$done = $false

$contextMenu = New-Object System.Windows.Forms.ContextMenuStrip
$menuItemExit = New-Object System.Windows.Forms.ToolStripMenuItem
$menuItemExit.Text = "Quit."
$null = $contextMenu.Items.Add($menuItemExit)
$notifyIcon.ContextMenuStrip = $contextMenu
# Set the script-level $done variable to $true when the menu item is clicked.
$menuItemExit.add_Click({ $script:done = $true })

# Show icon in systray (notification area)
$notifyIcon.Visible = $true

Write-Verbose -Verbose @"
Adding icon to notification area (system tray).
Use the icon's context menu to quit this script, 
or press Ctrl-C in the console window.
"@

# Loop
try {
  while (-not $done) {
      # IMPORTANT: Make WinForms process its events.
      [System.Windows.Forms.Application]::DoEvents()
      # Sleep just a little, to keep the UI responsive.
      Start-Sleep -MilliSeconds 100
  }
}
finally {
  $notifyIcon.Dispose()
}

相关问题