生命不止,继续 go go go!!!
golang官方并没有提供Windows gui库,但是今天还是要跟大家分享一下使用golang开发Windows桌面程序,当然又是面向github编程了。
知乎上有一个问答:
golang为什么没有官方的gui包?
这里,主要使用第三方库lxn/walk,进行Windows GUI编程。
lxn/walk
github地址:
https://github.com/lxn/walk
star:
2018
描述:
A Windows GUI toolkit for the Go Programming Language
获取:
go get github.com/lxn/walk
- 1
例子:
main.go
package mainimport ( "github.com/lxn/walk" . "github.com/lxn/walk/declarative" "strings")func main() { var inTE, outTE *walk.TextEdit MainWindow{ Title: "SCREAMO", MinSize: Size{600, 400}, Layout: VBox{}, Children: []Widget{ HSplitter{ Children: []Widget{ TextEdit{AssignTo: &inTE}, TextEdit{AssignTo: &outTE, ReadOnly: true}, }, }, PushButton{ Text: "SCREAM", OnClicked: func() { outTE.SetText(strings.ToUpper(inTE.Text())) }, }, }, }.Run()}
- 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
go build生成go_windows_gui.exe。
go_windows_gui.exe.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> </assembly>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
运行结果:
什么是manifest
上面提到了manifest,这是干什么的呢?
https://msdn.microsoft.com/en-us/library/windows/desktop/aa375365(v=vs.85).aspx
介绍:
Manifests are XML files that accompany and describe side-by-side assemblies or isolated applications. Manifests uniquely identify the assembly through the assembly’s assemblyIdentity element. They contain information used for binding and activation, such as COM classes, interfaces, and type libraries, that has traditionally been stored in the registry. Manifests also specify the files that make up the assembly and may include Windows classes if the assembly author wants them to be versioned. Side-by-side assemblies are not registered on the system, but are available to applications and other assemblies on the system that specify dependencies in manifest files.
是一种xml文件,标明所依赖的side-by-side组建。
如果用VS开发,可以Set通过porperty->configuration properties->linker->manifest file->Generate manifest To Yes来自动创建manifest来指定系统的和CRT的assembly版本。
详解
观察上面的manifest文件:
<xml>这是xml声明:
版本号----<?xml version="1.0"?>。这是必选项。 尽管以后的 XML 版本可能会更改该数字,但是 1.0 是当前的版本。编码声明------<?xml version="1.0" encoding="UTF-8"?>这是可选项。 如果使用编码声明,必须紧接在 XML 声明的版本信息之后,并且必须包含代表现有字符编码的值。standalone表示该xml是不是独立的,如果是yes,则表示这个XML文档时独立的,不能引用外部的DTD规范文件;如果是no,则该XML文档不是独立的,表示可以用外部的DTD规范文档。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
<dependency>这一部分指明了其依赖于一个库:
<assemblyIdentity>属性里面还分别是:type----系统类型,version----版本号,processorArchitecture----平台环境,publicKeyToken----公匙
- 1
- 2
- 3
- 4
- 5
应用
做一个巨丑无比的登录框
这里用到了LineEdit、LineEdit控件
package mainimport ( "github.com/lxn/walk" . "github.com/lxn/walk/declarative")func main() { var usernameTE, passwordTE *walk.LineEdit MainWindow{ Title: "登录", MinSize: Size{270, 290}, Layout: VBox{}, Children: []Widget{ Composite{ Layout: Grid{Columns: 2, Spacing: 10}, Children: []Widget{ VSplitter{ Children: []Widget{ Label{ Text: "用户名", }, }, }, VSplitter{ Children: []Widget{ LineEdit{ MinSize: Size{160, 0}, AssignTo: &usernameTE, }, }, }, VSplitter{ Children: []Widget{ Label{MaxSize: Size{160, 40}, Text: "密码", }, }, }, VSplitter{ Children: []Widget{ LineEdit{ MinSize: Size{160, 0}, AssignTo: &passwordTE, }, }, }, }, }, PushButton{ Text: "登录", MinSize: Size{120,