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即可
  • 文件結構為


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM