Developing GUI desktop application by golang

Development of GUI desktop application by golang (I) Basi...

Development of GUI desktop application by golang (I)

Basic operation

Installation dependency

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

Create the file 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 test", MinSize: Size, Layout: VBox{}, Children: []Widget{ HSplitter{ Children: []Widget{ TextEdit, TextEdit, }, }, PushButton{ Text: "SCREAM", OnClicked: func() { outTE.SetText(strings.ToUpper(inTE.Text())) }, }, }, }.Run() }
go mod init go-gui

Create the file go GUI / go gui.exe.manifest with suffix 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> <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 -manifest go-gui.exe.manifest -o rsrc.syso go build -ldflags="-H windowsgui"

Double click go-gui.exe

The file go-gui.exe can be copied anywhere and run directly because the manifest file has been compiled.

Add Icon

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

favicon.ico is the downloaded icon file. The effect is as follows.

Specify 1. manifest file

This is a new Dll management solution to solve the "Dll hell" problem on windows. As we all know, Dll is a dynamic loading shared library. The same Dll may be used by multiple programs. The so-called "Dll hell" is that when the program depends on the same Dll, but the versions are different, because the system can't distinguish which is which, the wrong Dll version is loaded and then hangs. So gates learned a lesson and made an assembly list. Each program should have a list. This list is stored in the. manifest file with the same name as his own application, which lists all the dependencies it needs. The dependencies listed here are not simply distinguished by the file description, but according to a kind of "strong file name" What distinguishes......

2. Role of rsrc program
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_.syso'

rsrc compiles static resource files into syso files. go language already supports links to syso files in the latest version, and will search the current directory and automatically link.

The embed ded package has been introduced since Go1.16, which can also compile static files. reference resources: https://www.php.cn/be/go/484192.html

Go1.14 is installed on this machine. There is no way to load the manifest file by using embed ded.

Of course, we can not compile the manifest file. For example, there are only gui.go and go-gui.exe.manifest files in my directory, and then use go build -ldflags="-H windowsgui" to compile. Get go-gui.exe. Note that the manifest file name at this time must be go-gui.exe.manifest. Click go-gui.exe, the effect is the same, but go-gui.exe and go-gui.exe.manifest should be in the same directory.

3. Meaning of parameter - ldflags="-H windowsgui"

Normally, any GUI developed in any language will be accompanied by a CMD box, such as this

Yes, if you only use go build to compile, this is the effect. If you take the parameter - ldflags="-H windowsgui", the CMD box behind will be removed.

In addition, you can also compress the size of the generated program: - ldflags="-H windowsgui -w"

26 November 2021, 18:43 | Views: 5676

Add new comment

For adding a comment, please log in
or create account

0 comments