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))
}
相关推荐
Chan166 分钟前
【 Java八股文面试 | Redis篇 缓存问题、持久化、分布式锁 】
java·数据库·redis·后端·spring·缓存·面试
q***471813 分钟前
Spring Boot 整合 Druid 并开启监控
java·spring boot·后端
bagadesu1 小时前
MySQL----case的用法
java·后端
百***58142 小时前
Spring Boot 2.7.x 至 2.7.18 及更旧的版本,漏洞说明
java·spring boot·后端
q***38512 小时前
Spring boot启动原理及相关组件
数据库·spring boot·后端
q***75602 小时前
【Golang】——Gin 框架中间件详解:从基础到实战
中间件·golang·gin
q***04052 小时前
Spring Boot项目中解决跨域问题(四种方式)
spring boot·后端·dubbo
q***64972 小时前
Spring BOOT 启动参数
java·spring boot·后端
百***62853 小时前
Spring Boot 3.X:Unable to connect to Redis错误记录
spring boot·redis·后端
我命由我123453 小时前
Java 开发 - 粘包处理器 - 基于消息头 + 消息体(魔数验证、长度验证)
java·网络·后端·网络协议·java-ee·intellij-idea·intellij idea