如何移除托盘图标右键菜单的选中状态边距
解决系统托盘右键菜单文本未左对齐的问题
你的问题核心是当前使用的ContextMenu控件没有提供移除勾选指示器边距的属性,需要替换为ContextMenuStrip并设置ShowCheckedMargin为$false来解决。以下是具体修改要点和完整代码:
修改要点
- 替换
System.Windows.Forms.ContextMenu为System.Windows.Forms.ContextMenuStrip - 将所有
MenuItem替换为ToolStripMenuItem(ContextMenuStrip对应的菜单项类型) - 设置
$context.ShowCheckedMargin = $false,移除勾选指示器预留的边距 - 将
NotifyIcon的ContextMenu属性改为ContextMenuStrip
修改后的完整代码
Add-Type -AssemblyName 'System.Windows.Forms' Add-Type -AssemblyName 'System.Drawing' Add-Type -AssemblyName 'PresentationFramework' $2 = "BASE64 CODE" $bitmap = New-Object System.Windows.Media.Imaging.BitmapImage $bitmap.BeginInit() $bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($2) $bitmap.EndInit() $bitmap.Freeze() $image = [System.Drawing.Bitmap][System.Drawing.Image]::FromStream($bitmap.StreamSource) $icon = [System.Drawing.Icon]::FromHandle($image.GetHicon()) $app = New-Object System.Windows.Forms.NotifyIcon $app.Text = "" $app.Icon = $icon $app.Visible = $true # 替换MenuItem为ToolStripMenuItem $Zenek = New-Object System.Windows.Forms.ToolStripMenuItem $Zenek.Text = "Zenek" $NetExt = New-Object System.Windows.Forms.ToolStripMenuItem $NetExt.Text = "NetExt" $Busy = New-Object System.Windows.Forms.ToolStripMenuItem $Busy.Text = "BUSY" $Time = New-Object System.Windows.Forms.ToolStripMenuItem $Time.Text = "Time" $Exit = New-Object System.Windows.Forms.ToolStripMenuItem $Exit.Text = "Exit" # 替换ContextMenu为ContextMenuStrip $context = New-Object System.Windows.Forms.ContextMenuStrip # 移除勾选指示器边距 $context.ShowCheckedMargin = $false # 关联到NotifyIcon的ContextMenuStrip属性 $app.ContextMenuStrip = $context $context.Items.AddRange(@($Zenek, $NetExt, $Busy, $Time, $Exit)) $keepAwakeScript = { while (1) { $wsh = New-Object -ComObject WScript.Shell $wsh.SendKeys('+{F15}') Start-Sleep -seconds 59 } } Start-Job -ScriptBlock $keepAwakeScript -Name "keepAwake" | Out-Null $Zenek.Add_Click({Set-Clipboard -Value "SOME TEXT"}) $NetExt.Add_Click({Set-Clipboard -Value "SOME OTHER TEXT"}) $Busy.Add_Click({ $running = Get-Job | ?{$_.Name -like 'KAPS'} $icon = New-Object System.Drawing.Icon("MY PRIVATE LOCATION\red.ico") $app.Icon = $icon if($running -eq $null){ Start-Job -Name KAPS -ScriptBlock{ while ($true){ $shell = New-Object -ComObject WScript.Shell $shell.SendKeys('{CAPSLOCK}') Start-Sleep 1 } } }else{ Stop-Job KAPS Remove-Job KAPS $icon = [System.Drawing.Icon]::FromHandle($image.GetHicon()) $app.Icon = $icon } }) $Time.Add_Click({ $main = New-Object System.Windows.Forms.Form $main.ClientSize = '200,200' $main.MinimizeBox = $false $main.MaximizeBox = $false $label = New-Object System.Windows.Forms.Label $label.Size = '190,30' $label.Location = '10,10' $label.Text = 'Ile minut?' $font = New-Object System.Drawing.Font("Arial", 23) $label.Font = $font $textbox = New-Object System.Windows.Forms.TextBox $textbox.Size = '50,100' $textbox.Location = '10,70' $button = New-Object System.Windows.Forms.Button $button.Size = '50,50' $button.Location = '10,120' $button.Text = 'GO' $button.Add_Click({ $timer.Start() $textbox.Visible = $false $button.Visible = $false $label.Visible = $false $script:1 = New-TimeSpan -minutes ([int]$textbox.Text) $label2.Visible = $true $label2.Text = $1.ToString("mm\:ss") }) $timer = New-Object System.Windows.Forms.Timer $timer.Enabled = $false $timer.Interval = '1000' $second = New-TimeSpan -Seconds 1 $timer.Add_Tick({ $script:1 = $1.Subtract($second) $label2.Text = $1.ToString("mm\:ss") if($script:1.TotalSeconds -le 0){ $timer.Stop() $timer.Dispose() [System.Windows.Forms.Application]::SetSuspendState("Hibernate", $true, $false) $main.Close() $main.Dispose() } }) $label2 = New-Object System.Windows.Forms.Label $label2.Size = '190,30' $label2.Location = '10,10' $label2.Text = $1 $font = New-Object System.Drawing.Font("Arial", 23) $label2.Font = $font $label2.Visible = $false $main.Controls.Add($textbox) $main.Controls.Add($button) $main.Controls.Add($label) $main.Controls.Add($label2) $main.ShowDialog() }) $Exit.Add_Click({ $app.Visible = $false Stop-Job -Name "keepAwake" $appContext.ExitThread() Stop-Process -Id $PID }) $appContext = New-Object System.Windows.Forms.ApplicationContext [void][System.Windows.Forms.Application]::Run($appContext)
内容的提问来源于stack exchange,提问作者Nawad-sama
相关产品推荐
相关产品推荐

