powershell script writes a tray icon

1. Prepare icons in ico format

star_bethlehem_icon
insert image description here
filename changed tostar.ico

2. Install VSCode

How to download and install the VSCode
extension: PowerShell extension

3. Create a project

1. Run the PowerShell command

mkdir trayicon_ps1;cd trayicon_ps1;New-Item trayicon.ps1;code .

2. Put it star.icointo trayicon_ps1the folder

4. Modify the encoding of trayicon.ps1

Select file trayicon.ps1
Click on the lower right corner UTF-8
insert image description here
Select 通过编码保存
insert image description here
SelectUTF-8 with BOM
insert image description here

5. Edit trayicon.ps1

# 脚本文件编码为UTF8 with BOM才能支持中文

# 加载WinForms程序集
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName PresentationFramework

# 图标文件路径要使用完整的路径。
$iconPath = "$(Get-Location)/star.ico"
$tooltip = "这是文字"

# 右键菜单
$contextMenu = [System.Windows.Forms.ContextMenuStrip]::new()

$menuItemMsg = [System.Windows.Forms.ToolStripMenuItem]::new()
$menuItemMsg.Text = "弹出消息框"
$menuItemMsg.add_Click({
    
     [System.Windows.MessageBox]::Show('这是消息框') })
$null = $contextMenu.Items.Add($menuItemMsg)

$menuItemExit = [System.Windows.Forms.ToolStripMenuItem]::new()
$menuItemExit.Text = "退出"
$menuItemExit.add_Click({
    
     $script:done = $true }) # 单击菜单项时,将$done设置为$true。
$null = $contextMenu.Items.Add($menuItemExit)

# 构造NotifyIcon对象。
$notifyIcon = [System.Windows.Forms.NotifyIcon]::new()
$notifyIcon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($iconPath)
$notifyIcon.Text = $tooltip
$notifyIcon.Visible = $true
$notifyIcon.ContextMenuStrip = $contextMenu
$notifyIcon.add_Click({
    
     
    param($evtSender, $evtArgs)
    if ($evtArgs.Button -eq [System.Windows.Forms.MouseButtons]::Left){
    
    
        [System.Windows.MessageBox]::Show("单击了图标")
    }
})

# 定义一个变量,该变量表示是否应退出脚本,并从add_Click()事件处理程序设置为$true。
$done = $false

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) {
    
    
        # 使WinForms处理其事件。
        [System.Windows.Forms.Application]::DoEvents()
        # 睡一会儿,保持用户界面的响应。
        # 理论上可以在这里执行其他任务,只要它们快速完成,以便仍然允许足够频繁的DoEvents()调用。
        Start-Sleep -MilliSeconds 100
    }
}
finally {
    
    
    # 处理通知图标,删除图标。
    $notifyIcon.Dispose()
    Write-Verbose -Verbose 'Exiting.'
}

6. Final effect

Click the triangle icon in the upper right corner to run the script.
insert image description here
Mouse hover effect.
insert image description here
Mouse click effect.
insert image description here
Right mouse button effect.
insert image description here
Pop-up message box button effect
insert image description here
. The exit button just exits and has no effect.

7. How to not display the powershell window

Newstart.vbs

set ws=createobject("wscript.shell")
ws.Run "powershell.exe .\trayicon.ps1" , 0, False

Double click to run

Guess you like

Origin blog.csdn.net/qq_39124701/article/details/132090720