普拉多VX

人生一路,不问来时,不知归期

0%

Golang Windows GUI开发实战之lxn/Walk (一)

说明

本系列文档记录下最近研究golang 开发windows gui的一些技术要点和操作教程。

walk介绍

今天因项目需要研究下GOLANG的GUI,发现了walk这个好东西,可以实现非常优美的界面程序。but只能在windows下开发

开发文档为:

walk下载

如果下载失败,需要设置代理

1
2
3
4
5
6
7
C:\Users\UI\go\src\ThorGui>go get github.com/lxn/walk
go get github.com/lxn/walk: module github.com/lxn/walk: Get "https://proxy.golang.org/github.com/lxn/walk/@v/list": dial tcp 216.58.200.49:443: connectex: A connection attempt failed because the connected party did not properly resp
ond after a period of time, or established connection failed because connected host has failed to respond.

C:\Users\UI\go\src\ThorGui>go env -w GO111MODULE=on

C:\Users\UI\go\src\ThorGui>go env -w GOPROXY=https://goproxy.cn,direct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
C:\Users\UI\go\src\ThorGui>go get github.com/lxn/walk
go: downloading github.com/lxn/walk v0.0.0-20201110160827-18ea5e372cdb
go: github.com/lxn/walk upgrade => v0.0.0-20201110160827-18ea5e372cdb
go: finding module for package gopkg.in/Knetic/govaluate.v3
go: finding module for package golang.org/x/sys/windows
go: finding module for package github.com/lxn/win
go: downloading gopkg.in/Knetic/govaluate.v3 v3.0.0
go: downloading golang.org/x/sys v0.0.0-20201112073958-5cba982894dd
go: downloading github.com/lxn/win v0.0.0-20201111105847-2a20daff6a55
go: found github.com/lxn/win in github.com/lxn/win v0.0.0-20201111105847-2a20daff6a55
go: found golang.org/x/sys/windows in golang.org/x/sys v0.0.0-20201112073958-5cba982894dd
go: found gopkg.in/Knetic/govaluate.v3 in gopkg.in/Knetic/govaluate.v3 v3.0.0

C:\Users\UI\go\src\ThorGui>

测试

第一个walk main.go程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main

import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"strings"
)

func main() {
var inTE, outTE *walk.TextEdit // 声明两个文本编辑控件

//主窗口对象
MainWindow{
Title: "windows程序", // 窗口标题设置
MinSize: Size{600, 400}, //窗体的大小
Layout: VBox{}, // 窗体的布局形式
//定义vbox的所有控件
Children: []Widget{ //定义控件
HSplitter{ //水平分割控件
Children: []Widget{ //定义子控件
TextEdit{AssignTo: &inTE},
TextEdit{AssignTo: &outTE, ReadOnly: true},
},
},
PushButton{ //按钮控件
Text: "确定",
OnClicked: func() {
outTE.SetText(strings.ToUpper(inTE.Text()))
},
},
},
}.Run()
}

编译

创建manifest文件,文件名 thorgui.manifest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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>

下载rsrc工具

1
2
3
C:\Users\UI\go\src\ThorGui>go get github.com/akavel/rsrc
go: downloading github.com/akavel/rsrc v0.9.0
go: github.com/akavel/rsrc upgrade => v0.9.0

生成.syso文件

1
C:\Users\UI\go\src\ThorGui>rsrc -manifest thorgui.manifest -o thorgui.syso
1
C:\Users\UI\go\src\ThorGui>go build -ldflags="-H windowsgui"  #生成exe文件

运行结果测试