安装
go get github.com/akavel/rsrc
go get github.com/lxn/win
go get github.com/lxn/walk
代码示例
package main
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/lxn/win"
)
func main() {
window, _ := walk.NewMainWindow()
window.SetTitle(`hello world`)
l := walk.NewVBoxLayout()
window.SetLayout(l)
s := walk.Size{ 500,1000}
window.SetSize(s)
window.SetX((int(win.GetSystemMetrics(0)) - window.Width()) / 2)
window.SetY((int(win.GetSystemMetrics(1)) - window.Height()) / 2)
window.Show()
window.Run()
}
创建清单文件 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>
生成 .syso 文件
- 这个步骤只需要运行一次, 调试/编译 只需一直执行 第二步
- 生成的 main.syso 是 可以拿到别的 GUI 项目中使用的
- 除非你想修改 main.manifest 或者 图标文件 才要重新生成一次
- 在当前窗口下 打开 cmd,运行如下命令:
rsrc -manifest main.manifest -o main.syso
编译
- 不管 调试 还是 正式编译,都必须通过 双击 exe 文件运行,无法使用 go run main.go 直接运行
go build
go build -ldflags="-H windowsgui"
运行结果