当前位置: 首页 > news >正文

powershell脚本写一个托盘图标

1、准备ico格式图标

star_bethlehem_icon
在这里插入图片描述
文件名改为star.ico

2、安装VSCode

如何下载安装VSCode
扩展:PowerShell扩展

3、创建项目

1、运行PowerShell命令

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

2、将star.ico放入trayicon_ps1文件夹

4、修改trayicon.ps1的编码

选择文件trayicon.ps1
点击右下角UTF-8
在这里插入图片描述
选择通过编码保存
在这里插入图片描述
选择UTF-8 with BOM
在这里插入图片描述

5、编辑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 = $falseWrite-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、最终效果

点击右上角三角图标运行脚本
在这里插入图片描述
鼠标悬浮效果
在这里插入图片描述
鼠标单击效果
在这里插入图片描述
鼠标右键效果
在这里插入图片描述
弹出消息框按钮效果
在这里插入图片描述
退出按钮就是退出,没啥效果

7、不显示powershell窗口的方式

新建start.vbs

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

双击运行即可

http://www.lryc.cn/news/107708.html

相关文章:

  • 前端Vue入门-day08-vant组件库
  • 华为OD机考--【磁盘容量排序】
  • 实现弧形切角两种方式
  • 什么是强化学习?
  • 如何在Linux系统上安装cpolar内网穿透
  • 分布式软件架构——内容分发网络
  • 【HAL库】STM32CubeMX开发----STM32F407----LAN8720A----移植FreeModbus实现ModbusTCP
  • 11-矩阵(matrix)_方阵_对称阵_单位阵_对角阵
  • AWS多账户单点登录 IAM Identity Center(AWS SSO)
  • 实验2-3-3 求奇数分之一序列前N项和 (15 分)
  • 关于Android studio中的自动化测试脚本UiAutomator框架以及UiAutomatorViewer工具的使用——项目案例
  • OA办公自动化系统设计与实现(论文+源码)_kaic
  • ansible——playbook
  • DDS中间件设计
  • aws的EC2云服务器自己操作记录
  • 基本ACL 和高级ACL配置
  • 【uniapp 报错 Cannot read properties of null (reading ‘offsetWidth‘)解决办法】
  • 6.s081/6.1810(Fall 2022)Lab2: System calls
  • Git在VSCode中的使用
  • 【双指针_移动零_C++】
  • 【网络安全】网络安全威胁实时地图 - 2023
  • 视频过大如何压缩变小?文件压缩技巧分享
  • 组合模式(Composite)
  • grid map学习笔记3之详解grid_map_pcl库实现point cloud点云转换成grid map栅格地图
  • ebpf开发问题汇总
  • 认识 mysql 命令
  • IK(Inverse Kinematics,逆运动学)
  • Cadence 小技巧系列(持续更新)
  • 【unity】Pico VR 开发笔记(基础篇)
  • 竞争之王CEO商战课,聚百家企业在京举行