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