实现了控制台启动后自动隐藏

This commit is contained in:
2025-10-31 17:29:14 +08:00
parent 22b4402737
commit 8d41c1ba62
6 changed files with 82 additions and 59 deletions

View File

@@ -1,50 +1,42 @@
package main package main
import (
"image"
"os"
"github.com/icza/icox" // 替换为icox库
"github.com/nfnt/resize"
)
func main() { func main() {
// 1. 读取PNG文件 //// 1. 读取PNG文件
inputPath := "manifest/images/logo.png" // 输入PNG路径 //inputPath := "manifest/images/logo.png" // 输入PNG路径
outputPath := "manifest/images/favicon.ico" // 输出ICO路径 //outputPath := "manifest/images/favicon.ico" // 输出ICO路径
//
pngFile, err := os.Open(inputPath) //pngFile, err := os.Open(inputPath)
if err != nil { //if err != nil {
panic("无法打开PNG文件: " + err.Error()) // panic("无法打开PNG文件: " + err.Error())
} //}
defer pngFile.Close() //defer pngFile.Close()
//
// 2. 解码PNG为image.Image对象 //// 2. 解码PNG为image.Image对象
img, _, err := image.Decode(pngFile) //img, _, err := image.Decode(pngFile)
if err != nil { //if err != nil {
panic("PNG解码失败: " + err.Error()) // panic("PNG解码失败: " + err.Error())
} //}
//
// 3. 定义ICO需要包含的尺寸常见尺寸 //// 3. 定义ICO需要包含的尺寸常见尺寸
sizes := []uint{16, 32, 64, 128} // 可根据需求添加更多尺寸 //sizes := []uint{16, 32, 64, 128} // 可根据需求添加更多尺寸
var icoImages []image.Image //var icoImages []image.Image
//
// 4. 缩放图像到每个目标尺寸并收集 //// 4. 缩放图像到每个目标尺寸并收集
for _, size := range sizes { //for _, size := range sizes {
// 使用Lanczos3算法缩放高质量 // // 使用Lanczos3算法缩放高质量
resized := resize.Resize(size, size, img, resize.Lanczos3) // resized := resize.Resize(size, size, img, resize.Lanczos3)
icoImages = append(icoImages, resized) // icoImages = append(icoImages, resized)
} //}
//
// 5. 编码为ICO并写入文件关键修改使用icox.Encode //// 5. 编码为ICO并写入文件关键修改使用icox.Encode
icoFile, err := os.Create(outputPath) //icoFile, err := os.Create(outputPath)
if err != nil { //if err != nil {
panic("无法创建ICO文件: " + err.Error()) // panic("无法创建ICO文件: " + err.Error())
} //}
defer icoFile.Close() //defer icoFile.Close()
//
// icox.Encode直接支持多尺寸切片[]image.Image //// icox.Encode直接支持多尺寸切片[]image.Image
if err := icox.Encode(icoFile, icoImages); err != nil { //if err := icox.Encode(icoFile, icoImages); err != nil {
panic("ICO编码失败: " + err.Error()) // panic("ICO编码失败: " + err.Error())
} //}
} }

2
go.mod
View File

@@ -16,11 +16,11 @@ require (
github.com/libp2p/go-libp2p-kad-dht v0.35.1 github.com/libp2p/go-libp2p-kad-dht v0.35.1
github.com/multiformats/go-multiaddr v0.16.1 github.com/multiformats/go-multiaddr v0.16.1
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/ayflying/update-github-release v0.0.3
) )
require ( require (
github.com/BurntSushi/toml v1.5.0 // indirect github.com/BurntSushi/toml v1.5.0 // indirect
github.com/ayflying/update-github-release v0.0.2 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect

2
go.sum
View File

@@ -17,6 +17,8 @@ github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYr
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/ayflying/update-github-release v0.0.2 h1:pZ2W0VKagBsJ5OsrREfniy/0fE70nbU8dJU7TtpzY0w= github.com/ayflying/update-github-release v0.0.2 h1:pZ2W0VKagBsJ5OsrREfniy/0fE70nbU8dJU7TtpzY0w=
github.com/ayflying/update-github-release v0.0.2/go.mod h1:9ctXuagiEKABWLaS4ocRCUcnN77jdYm7zir7NQ+rIeM= github.com/ayflying/update-github-release v0.0.2/go.mod h1:9ctXuagiEKABWLaS4ocRCUcnN77jdYm7zir7NQ+rIeM=
github.com/ayflying/update-github-release v0.0.3 h1:Tba83oXcs0ufU9fMGrGRGXai96F9LUwSYrl7qMgAruk=
github.com/ayflying/update-github-release v0.0.3/go.mod h1:9ctXuagiEKABWLaS4ocRCUcnN77jdYm7zir7NQ+rIeM=
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o= github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=

View File

@@ -12,6 +12,7 @@ import (
"github.com/gogf/gf/v2/os/gcmd" "github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gres"
) )
// 引入 Windows API 函数 // 引入 Windows API 函数
@@ -20,6 +21,9 @@ var (
kernel32 = syscall.NewLazyDLL("kernel32.dll") kernel32 = syscall.NewLazyDLL("kernel32.dll")
showWindow = user32.NewProc("ShowWindow") showWindow = user32.NewProc("ShowWindow")
getConsoleWnd = kernel32.NewProc("GetConsoleWindow") getConsoleWnd = kernel32.NewProc("GetConsoleWindow")
freeConsole = kernel32.NewProc("FreeConsole")
attachConsole = kernel32.NewProc("AttachConsole")
allocConsole = kernel32.NewProc("AllocConsole")
) )
func (s *sOS) start() { func (s *sOS) start() {
@@ -30,9 +34,21 @@ func (s *sOS) start() {
// 系统托盘初始化(设置图标、右键菜单) // 系统托盘初始化(设置图标、右键菜单)
func (s *sOS) onSystrayReady() { func (s *sOS) onSystrayReady() {
//s.hideConsole() s.hideConsole()
var iconByte []byte
if !gfile.Exists(s.systray.Icon) {
iconByte = gres.GetContent(s.systray.Icon)
gfile.PutBytes(s.systray.Icon, iconByte)
}
iconByte = gfile.GetBytes(s.systray.Icon)
//if gres.Contains(s.systray.Icon) {
// iconByte = gres.GetContent(s.systray.Icon)
// gfile.PutBytes(s.systray.Icon, iconByte)
//} else {
// iconByte = gfile.GetBytes(s.systray.Icon)
//}
iconByte := gfile.GetBytes(s.systray.Icon)
systray.SetIcon(iconByte) systray.SetIcon(iconByte)
systray.SetTitle(s.systray.Title) systray.SetTitle(s.systray.Title)
systray.SetTooltip(s.systray.Tooltip) systray.SetTooltip(s.systray.Tooltip)
@@ -46,6 +62,7 @@ func (s *sOS) onSystrayReady() {
select { select {
case <-mQuit.ClickedCh: case <-mQuit.ClickedCh:
systray.Quit() systray.Quit()
case <-mShow.ClickedCh: case <-mShow.ClickedCh:
// 显示窗口 // 显示窗口
s.showConsole() s.showConsole()
@@ -77,13 +94,14 @@ func (s *sOS) update(version, server string) {
// 隐藏控制台窗口 // 隐藏控制台窗口
func (s *sOS) hideConsole() { func (s *sOS) hideConsole() {
// 获取当前控制台窗口句柄 // 彻底脱离控制台,避免仅最小化
freeConsole.Call()
// 获取当前控制台窗口句柄并隐藏窗口
hWnd, _, _ := getConsoleWnd.Call() hWnd, _, _ := getConsoleWnd.Call()
if hWnd == 0 { if hWnd != 0 {
return // 无控制台窗口如编译为GUI子系统时 // SW_HIDE = 0隐藏窗口
showWindow.Call(hWnd, 0)
} }
// SW_HIDE = 0隐藏窗口
showWindow.Call(hWnd, 0)
} }
// 显示控制台窗口 // 显示控制台窗口
@@ -91,8 +109,18 @@ func (s *sOS) showConsole() {
// 获取当前控制台窗口句柄 // 获取当前控制台窗口句柄
hWnd, _, _ := getConsoleWnd.Call() hWnd, _, _ := getConsoleWnd.Call()
if hWnd == 0 { if hWnd == 0 {
return // 如果当前进程没有控制台,尝试附加到父进程控制台
// ATTACH_PARENT_PROCESS = (DWORD)-1
ret, _, _ := attachConsole.Call(uintptr(^uint32(0)))
if ret == 0 {
// 附加失败则分配一个新的控制台窗口
allocConsole.Call()
}
// 重新获取控制台窗口句柄
hWnd, _, _ = getConsoleWnd.Call()
}
if hWnd != 0 {
// SW_SHOW = 5显示窗口
showWindow.Call(hWnd, 5)
} }
// SW_SHOW = 5显示窗口
showWindow.Call(hWnd, 5)
} }

View File

@@ -23,7 +23,7 @@ func init() {
// 每天0点检查更新 // 每天0点检查更新
gcron.Add(gctx.New(), "0 0 0 * * *", func(ctx context.Context) { gcron.Add(gctx.New(), "0 0 0 * * *", func(ctx context.Context) {
err := update.CheckUpdate() err := update.CheckUpdate(true)
if err != nil { if err != nil {
g.Log().Errorf(ctx, "检查更新失败:%v", err) g.Log().Errorf(ctx, "检查更新失败:%v", err)
} }
@@ -31,7 +31,7 @@ func init() {
go func() { go func() {
//在协程中检查更新,预防主程序阻塞 //在协程中检查更新,预防主程序阻塞
err := update.CheckUpdate() err := update.CheckUpdate(true)
if err != nil { if err != nil {
g.Log().Errorf(gctx.New(), "检查更新失败:%v", err) g.Log().Errorf(gctx.New(), "检查更新失败:%v", err)
} }

View File

@@ -1 +1,2 @@
package packed package packed