芹菜是一根葱
专业解决各种前端Bug,吊打各种面试官

环境:go fyne 

要求:电脑右下方添加系统托盘,点击图标显示托盘菜单

效果:

实现:

 1 package main
 2 
 3 import (
 4     "fyne.io/fyne/v2"
 5     "fyne.io/fyne/v2/app"
 6     "fyne.io/fyne/v2/widget"
 7     "fyne.io/fyne/v2/driver/desktop"
 8     "fyne.io/fyne/v2/theme"
 9 )
10 
11 func main() {
12     myApp := app.New()
13     
14     // 加载自定义图标
15     icon, _ := fyne.LoadResourceFromPath("golang.ico")
16     myApp.SetIcon(icon)
17     myWindow := myApp.NewWindow("Button Widget")
18     addSystemTray(myApp,myWindow)
19 
20     label := widget.NewLabel("add icon")
21 
22     myWindow.SetContent(label)
23     myWindow.Resize(fyne.NewSize(500, 500))
24     //窗口居中
25     myWindow.CenterOnScreen()
26     myWindow.ShowAndRun()
27 }
28 
29 // 系统托盘
30 func addSystemTray(a fyne.App, w fyne.Window) {
31     if desk, ok := a.(desktop.App); ok {
32         h := fyne.NewMenuItem("芹菜是一根葱", func() { w.Show() /* 显示窗口 */ })
33         h.Icon = theme.HomeIcon() //图标
34         m := fyne.NewMenu("MyApp",h)
35         desk.SetSystemTrayMenu(m)
36     }
37     w.SetCloseIntercept(func() {
38        w.Hide()
39     })
40 }

 

posted on 2023-09-18 18:07  芹菜是一根葱  阅读(472)  评论(0编辑  收藏  举报