golang 如何开发windows窗口界面

golang 如何开发windows窗口界面

  • 调用包
go get github.com/lxn/walk
  • 使用walk写一个简单的window应用程序
package main

import (
	"log"
	"strings"

	"github.com/lxn/walk"
	."github.com/lxn/walk/declarative"
)

func init(){
	log.SetFlags(log.Ldate|log.Lshortfile)
}

func main(){
	var inTE,outTE *walk.TextEdit

	MainWindow{
		Title: "test",
		MinSize: Size{600,400},
		Layout: VBox{},
		Children: []Widget{
			HSplitter{
				Children:[]Widget{
					TextEdit{AssignTo:&inTE,MaxLength:10},
					TextEdit{AssignTo:&outTE,ReadOnly:true},

				},
			},
			PushButton{
				Text: "SCREAM",
				OnClicked: func(){
					outTE.SetText(strings.ToUpper(inTE.Text()))
				},
			},
		},
	}.Run()
}
  • 在程序中嵌入二进制资源工具
go get github.com/akavel/rsrc

拉取下来之后,就会在GOPATH/src/bin中会发现有一个rsrc.exe
  • 在上面例子的同文件下,创建一个gui.mainifest的文件,并且在里面写入:
<?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>
  • 然后运行
rsrc -manifest gui.manifest -o rsrc.syso
  • 然后编译该文件
go build -ldflags="-H windowsgui"
  • 点击运行gui.exe即可
  • 文件结构为
posted @ 2020-06-23 22:49  Myuniverse  阅读(2015)  评论(0编辑  收藏  举报