go 使用systray 实现托盘和程序退出

1.先 go get 安装 包

bash 复制代码
go get github.com/getlantern/systray

2.使用的代码

bash 复制代码
func main() {
	fmt.Println("开始")
	systray.Run(onReady, onExit)
}

func onReady() {
	systray.SetIcon(icon.Data)
	systray.SetTitle("Awesome App")
	systray.SetTooltip("Pretty awesome超级棒")
	mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
	fmt.Println("开始启动")
	// Sets the icon of a menu item. Only available on Mac and Windows.
	mQuit.SetIcon(icon.Data)
	//监听点击Quit按钮
	go func() {
		<-mQuit.ClickedCh
		systray.Quit()
	}()
}
//关闭对应的程序,这里的command只适用于windows
func onExit() {
	// clean up here
	fmt.Println("结束")
	command := exec.Command("taskkill", "/im", "server.exe", "/F")
	output, err := command.CombinedOutput()
	if err != nil {
		fmt.Println("output err:", err)
		return
	}
	fmt.Println("output:", string(output))
}
相关推荐
excel5 小时前
Nginx 与 Node.js(PM2)的对比优势及 HTTPS 自动续签配置详解
后端
bobz9657 小时前
vxlan 为什么一定要封装在 udp 报文里?
后端
bobz9657 小时前
vxlan 直接使用 ip 层封装是否可以?
后端
郑道9 小时前
Docker 在 macOS 下的安装与 Gitea 部署经验总结
后端
3Katrina9 小时前
妈妈再也不用担心我的课设了---Vibe Coding帮你实现期末课设!
前端·后端·设计
汪子熙9 小时前
HSQLDB 数据库锁获取失败深度解析
数据库·后端
高松燈9 小时前
若伊项目学习 后端分页源码分析
后端·架构
没逻辑9 小时前
主流消息队列模型与选型对比(RabbitMQ / Kafka / RocketMQ)
后端·消息队列
倚栏听风雨10 小时前
SwingUtilities.invokeLater 详解
后端
Java中文社群10 小时前
AI实战:一键生成数字人视频!
java·人工智能·后端