文章目录
go get -u -v github.com/akavel/rsrc
rsrc -manifest main.manifest -o rsrc.syso
go build
1.1 main.go
package main
import (
"log"
"github.com/lxn/win"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
type MyWindow struct {
*walk.MainWindow
hWnd win.HWND
minimizeBox *walk.CheckBox
maximizeBox *walk.CheckBox
closeBox *walk.CheckBox
sizeBox *walk.CheckBox
ni *walk.NotifyIcon
}
func (mw *MyWindow) SetMinimizeBox() {
if mw.minimizeBox.Checked() {
mw.addStyle(win.WS_MINIMIZEBOX)
return
}
mw.removeStyle(^win.WS_MINIMIZEBOX)
}
func (mw *MyWindow) SetMaximizeBox() {
if mw.maximizeBox.Checked() {
mw.addStyle(win.WS_MAXIMIZEBOX)
return
}
mw.removeStyle(^win.WS_MAXIMIZEBOX)
}
func (mw *MyWindow) SetSizePersistent() {
if mw.sizeBox.Checked() {
mw.addStyle(win.WS_SIZEBOX)
return
}
mw.removeStyle(^win.WS_SIZEBOX)
}
func (mw *MyWindow) addStyle(style int32) {
currStyle := win.GetWindowLong(mw.hWnd, win.GWL_STYLE)
win.SetWindowLong(mw.hWnd, win.GWL_STYLE, currStyle|style)
}
func (mw *MyWindow) removeStyle(style int32) {
currStyle := win.GetWindowLong(mw.hWnd, win.GWL_STYLE)
win.SetWindowLong(mw.hWnd, win.GWL_STYLE, currStyle&style)
}
func (mw *MyWindow) SetCloseBox() {
if mw.closeBox.Checked() {
win.GetSystemMenu(mw.hWnd, true)
return
}
hMenu := win.GetSystemMenu(mw.hWnd, false)
win.RemoveMenu(hMenu, win.SC_CLOSE, win.MF_BYCOMMAND)
}
func (mw *MyWindow) AddNotifyIcon() {
var err error
mw.ni, err = walk.NewNotifyIcon(mw)
if err != nil {
log.Fatal(err)
}
icon, err := walk.Resources.Image("img/show.ico")
if err != nil {
log.Fatal(err)
}
mw.SetIcon(icon)
mw.ni.SetIcon(icon)
mw.ni.SetVisible(true)
mw.ni.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
if button == walk.LeftButton {
mw.Show()
win.ShowWindow(mw.Handle(), win.SW_RESTORE)
}
})
}
func main() {
mw := new(MyWindow)
if err := (MainWindow{
AssignTo: &mw.MainWindow,
Title: "notify icon",
Size: Size{550, 380},
Layout: VBox{MarginsZero: true},
OnSizeChanged: func() {
if win.IsIconic(mw.Handle()) {
mw.Hide()
mw.ni.SetVisible(true)
}
},
Children: []Widget{
CheckBox{
AssignTo: &mw.minimizeBox,
Text: "显示最小化按钮",
Checked: true,
OnCheckStateChanged: mw.SetMinimizeBox,
},
CheckBox{
AssignTo: &mw.maximizeBox,
Text: "显示最大化按钮",
Checked: true,
OnCheckStateChanged: mw.SetMaximizeBox,
},
CheckBox{
AssignTo: &mw.closeBox,
Text: "显示关闭按钮",
Checked: true,
OnCheckStateChanged: mw.SetCloseBox,
},
CheckBox{
AssignTo: &mw.sizeBox,
Text: "允许修改大小",
Checked: true,
OnCheckStateChanged: mw.SetSizePersistent,
},
},
}.Create()); err != nil {
log.Fatal(err)
}
mw.hWnd = mw.Handle()
mw.AddNotifyIcon()
mw.Run()
}
1.2 main.manifest
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="SomeFunkyNameHere" type="win32"/>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
</dependentAssembly>
</dependency>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True</dpiAware>
</windowsSettings>
</application>
</assembly>
本帖子中包含资源
您需要 登录 才可以下载,没有帐号?立即注册