Open aleass opened 5 months ago
system version:
go env:
my code, call the SystrayInit function in main func
package service
import (
"embed"
"fmt"
"github.com/getlantern/systray"
"log"
"os"
"syscall"
"time"
"wg-tag-printer-service/src/common"
)
//go:embed embed/run.ico
var logFiles embed.FS
// SystrayInit
//
// @Description: 托盘功能初始化
func SystrayInit() {
go systray.Run(onReady, nil)
}
// onReady
//
// @Description: 准备工作
func onReady() {
raw, _ := logFiles.ReadFile("embed/run.ico")
systray.SetIcon(raw)
systray.SetTitle(common.ManagerToolTitle)
systray.SetTooltip(common.ManagerToolTitle)
//添加菜单
addMenuItem()
}
// addMenuItem
//
// @Description: 添加菜单键
func addMenuItem() {
//窗口
showWindow := systray.AddMenuItem("打开窗口", "打开窗口")
hideWindow := systray.AddMenuItem("隐藏窗口", "隐藏窗口")
//退出
quitEvent := systray.AddMenuItem("退出", "关闭打印工具程序")
//dll
kernel32 := syscall.NewLazyDLL("kernel32.dll")
getConsoleWindows := kernel32.NewProc("GetConsoleWindow")
consoleHandle, r2, err := getConsoleWindows.Call()
if consoleHandle == 0 {
fmt.Println("Error call GetConsoleWindow: ", consoleHandle, r2, err.Error())
}
user32 := syscall.NewLazyDLL("user32.dll")
showWindowAsync := user32.NewProc("ShowWindowAsync")
const (
hideWindows uintptr = 0 //隐藏窗口并激活另一个窗口。
showWindows uintptr = 5 //激活窗口并以当前大小和位置显示窗口。
)
//启动默认隐藏
go func() {
//查看是否启动成功
fmt.Println("--------------------------5s 后自动隐藏窗口 -------------------------")
time.Sleep(time.Second * 5)
windowsOp(hideWindow, showWindowAsync, consoleHandle, hideWindows)
}()
go func() {
for {
select {
case <-quitEvent.ClickedCh:
quitEventFunc()
case <-showWindow.ClickedCh:
windowsOp(showWindow, showWindowAsync, consoleHandle, showWindows)
case <-hideWindow.ClickedCh:
windowsOp(hideWindow, showWindowAsync, consoleHandle, hideWindows)
}
}
}()
}
// quitEventFunc
//
// @Description: 退出事件
func quitEventFunc() {
log.Println(common.ManagerToolTitle + " 退出")
common.Logger.Debug(common.ManagerToolTitle + " 退出")
// 杀旧进程
common.KillProcess(common.ExecFileName)
//由于是go systray,所以在这里直接退出
os.Exit(0)
//systray.Quit()
}
// hideWindows
//
// @Description: 隐藏事件
// @param mSwitchWindow
// @param showWindowAsync
// @param consoleHandle
func windowsOp(mSwitchWindow *systray.MenuItem, showWindowAsync *syscall.LazyProc, consoleHandle uintptr, nCmdShow uintptr) {
mSwitchWindow.Disable()
mSwitchWindow.Enable()
r1, r2, err := showWindowAsync.Call(consoleHandle, nCmdShow)
if r1 != 1 {
log.Printf("Error call ShowWindow @SW_SHOW:%s,r1:%d,r2:%d", err.Error(), uint64(r1), uint64(r2))
common.Logger.Error(fmt.Sprintf("Error call ShowWindow @SW_SHOW:%s,r1:%d,r2:%d", err.Error(), uint64(r1), uint64(r2)))
}
}
func Get_Systray() { runtime.LockOSThread() defer runtime.UnlockOSThread()
Recoverr(func() {
systray.Run(onReady, onExit)
})
}
func Recoverr(fn func()) { defer func() { if r := recover(); r != nil { log.Printf("Recovered from panic: %v\n%s", r, debug.Stack()) } }() fn() } func main() { go gui.Get_Systray() select {} }
@wsczx thanks, after I add runtime.LockOSThread() in the systray.Run's gorountine, it works good.
Hello,when I run and tray, after a while, left or right click will not respond, I do not know whether it is a period of time or some event caused the trigger,but it's running in background,thanks.