簡體   English   中英

如何刪除托盤圖標上下文菜單中的檢查邊距

[英]How to remove checked margin in tray icon context menu

我創建了這個在托盤中運行的小腳本。 我注意到上下文菜單文本沒有完全放在左邊。 經過一番調查,我發現這是因為.checked state 指標還有空間。 我發現它可以通過ContextMenuStrip.showcheckedmargin屬性刪除,但不知道如何實現它。 請指教。

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

$Zenek = New-Object System.Windows.Forms.MenuItem
$Zenek.Text = "Zenek"

$NetExt = New-Object System.Windows.Forms.MenuItem
$NetExt.Text = "NetExt"

$Busy = New-Object System.Windows.Forms.MenuItem
$Busy.Text = "BUSY"

$Time = New-Object System.Windows.Forms.MenuItem
$Time.Text = "Time"

$Exit = New-object System.Windows.Forms.MenuItem
$Exit.Text = "Exit"

$context = New-Object System.Windows.Forms.ContextMenu

$app.ContextMenu = $context
$app.ContextMenu.MenuItems.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)

對於任何有同樣問題的人。 我已經設法弄明白了。 SO的另一篇文章允許我這樣做。

我必須將Menu本身的.MenuItem替換為.ToolStripMenuItem .ContextMenu .ContextMenuStrip .ShowImageMargin添加到ContextMenuStrip變量,然后最后通過.Items.AddRange(@())添加項目。


在此處輸入圖像描述

在此處輸入圖像描述

最后代碼看起來像這樣並且按預期工作。

add-type -AssemblyName 'System.Windows.Forms'
Add-Type -AssemblyName 'System.Drawing'
Add-Type -AssemblyName 'PresentationFramework'


$2 = 
"BASE 64 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

$Zenek = New-Object System.Windows.Forms.ToolStripMenuItem
$Zenek.Text = "Zenek"
$Zenek.Checked = $true

$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"

$sep = New-Object System.Windows.Forms.ToolStripSeparator

$context = New-Object System.Windows.Forms.ContextMenuStrip
$context.ShowImageMargin = $false
$context.Items.AddRange(@($Zenek, $NetExt, $Busy, $Time, $sep, $Exit))

$app.ContextMenuStrip = $context



$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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM