栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

golang开发GUI桌面应用walk(一)

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

golang开发GUI桌面应用walk(一)

golang开发GUI桌面应用(一)

基础操作

requires:Go 1.11.x or later

安装依赖

go get github.com/lxn/walk
go get github.com/akavel/rsrc
rsrc

创建文件 go-gui/gui.go

package main

import (
	"strings"

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

func main() {
	var inTE, outTE *walk.TextEdit
	MainWindow{
		Title: "xiaochuan测试",
		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 mod init go-gui

创建后缀为manifest的文件 go-gui/go-gui.exe.manifest



    
    
        
            
        
    
    
        
            PerMonitorV2, PerMonitor
            True
        
    

rsrc -manifest go-gui.exe.manifest -o rsrc.syso
go build -ldflags="-H windowsgui"

双击 go-gui.exe

文件go-gui.exe可以复制到任何地方直接运行,因为已经将manifest文件编译进去了。

添加图标

rsrc -manifest go-gui.exe.manifest -ico favicon.ico -o rsrc.syso
go build -ldflags="-H windowsgui"

favicon.ico为下载的图标文件,效果如下。


运行程序,任务栏上的图标也变成了这个。

具体说明 1、manifest 文件

这个东西的作用就是为了解决 以前windows上的“Dll 地狱” 问题才产生的新的DLL管理解决方案。大家知道,Dll是动态加载共享库,同一个Dll可能被多个程序所使用,而所谓“Dll 地狱”就是当不通程序依赖的Dll相同,但版本不同时,由于系统不能分辨到底哪个是哪个,所以加载错了Dll版本,然后就挂了。于是盖茨就吸取了教训,搞了一个程序集清单的东西,每个程序都要有一个清单,这个清单存再和自己应用程序同名的.manifest文件中,里面列出其所需要的所有依赖,这儿所列出的依赖可不是简单地靠文件明来区分的,而是根据一种叫做“强文件名”的东西区分的。。。。。。

2、rsrc程序的作用
rsrc - Tool for embedding binary resources in Go programs.

Generates a .syso file with specified resources embedded in .rsrc section,
  aimed for consumption by Go linker when building Win32 excecutables.

The generated *.syso files should get automatically recognized by 'go build'
command and linked into an executable/library, as long as there are any *.go
files in the same directory.

OPTIONS:
  -arch string
    	architecture of output file - one of: 386, amd64, [EXPERIMENTAL: arm, arm64] (default "amd64")
  -ico string
    	comma-separated list of paths to .ico files to embed
  -manifest string
    	path to a Windows manifest file to embed
  -o string
    	name of output COFF (.res or .syso) file; if set to empty, will default to 'rsrc_windows_{arch}.syso'

rsrc将静态资源文件编译到syso文件中,go语言在最新版本中已经支持syso文件的链接并且会搜索当前目录,自动链接。

在Go1.16起引入了embed包,也可以实现将静态文件编译进去。参考:https://www.php.cn/be/go/484192.html

本机安装的是Go1.14,没有办法演示使用 embed 的方式加载 manifest 文件。

当然,我们也可以不将 manifest文件编译进去,比如,我的目录下只有gui.go和go-gui.exe.manifest文件,然后使用go build -ldflags="-H windowsgui"来编译。得到go-gui.exe,注意,此时的manifest文件名一定要是go-gui.exe.manifest。点击go-gui.exe,效果一样,只是,go-gui.exe和go-gui.exe.manifest要在同一个目录下。

3、参数-ldflags="-H windowsgui"的意义

正常情况下,任何语言开发的GUI都会伴随着一个CMD框在后面,比如这样

是的,如果仅仅使用go build来编译的话,就是这个效果,带上参数-ldflags="-H windowsgui"就会去掉后面的CMD框。

这个CMD框的作用是打印信息,输出等。

另外,还可以压缩一下生成的程序的大小:-ldflags="-H windowsgui -w"

关于 golang GUI

win
win把win32 api几乎所有相关的声明都导成了go。如果发现有没导出的,可以参照里面的方法导出,很简单的。

walk
是对win包的再次封装,方便使用。

fyne
文档相对丰富一些。

govcl

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/903122.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号