mer*_*uwe 4 powershell systray click exit winforms
我想通过用鼠标左键单击文本“退出”来退出此系统托盘程序。当我悬停时,鼠标会显示一个旋转的蓝色图标,并且单击不会执行任何操作。剧本有什么问题?
# 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
}
Run Code Online (Sandbox Code Playgroud)
所需的关键改变是:
不要exit直接从.add_Click()事件处理程序脚本块调用:它会使您的脚本崩溃。
下面的代码也$notifyIcon.Dispose()移出了此脚本块,而是将其移动到包装循环finally的语句块中,并通过脚本级变量通知循环希望退出,该变量是通过事件处理程序设置的(它在脚本的子范围中运行)。trywhile$done$script:done = $true
这可确保使用Ctrl-C终止脚本也能正确处理该图标并将其从通知区域中删除。
在循环中while,您必须定期调用[System.Windows.Forms.Application]::DoEvents()以便允许 WinForms 处理其 UI 事件。在这些调用之间仅休眠一小段时间,以便保持 UI 响应 - 长时间休眠会在休眠期间阻塞事件处理。
ApplicationContext) 并在单个阻塞[System.Windows.Forms.Application]::Run()调用中使用它 - 请参阅此答案。# Load the WinForms assembly.
Add-Type -AssemblyName System.Windows.Forms
# Use PowerShell's icon in this example; be sure to use a full path.
$iconPath = (Get-Process -Id $PID).Path
$tooltip = "This is a text."
# Construct the NotifyIcon object.
$notifyIcon = [System.Windows.Forms.NotifyIcon]::new()
$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 = [System.Windows.Forms.ContextMenuStrip]::new()
$menuItemExit = [System.Windows.Forms.ToolStripMenuItem]::new()
$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 a PowerShell 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.
# Note:
# In theory, you could perform other tasks here,
# as long as they complete quickly so as to still
# allow frequent enough ::DoEvents() calls.
Start-Sleep -MilliSeconds 100
}
}
finally {
# Dispose of the notify icon, which also removes it from display.
$notifyIcon.Dispose()
Write-Verbose -Verbose 'Exiting.'
}
Run Code Online (Sandbox Code Playgroud)