使用Golang语言walk框架开发一个简单的windowsGUI

1、前言

最近使用golang语言开发了一个windowsGUI的Demo,由于golang语言属于比较新的语言,对GUI开发这一块并没有原生库也没有推荐库,因此我在github上找到了一个叫做walk的GUI开发框架。

github原址为:http://github.com/lxn/walk

在golang社区也能看到他的文档:http://godoc.org/github.com/lxn/walk

当然,你看了github和godoc就能发现,这个文档和直接看源码没啥区别,文档里没有让人一眼就能明白的描述,只是对方法和属性的介绍,至于具体使用更是没有。全靠自己摸索。

当然,在他的源码里,你可以看到有一个examples的文件夹,这个里面包含了所有作者写出的示例(当然,也没有什么有用的注释告诉你这个例子干了什么)。只能在terminal里进入一个个的文件夹,一次次的编译,然后运行.exe文件看效果,才能知道这个例子到底是干嘛的。

当然,作者贴心的在每个示例里上传了.syso文件,这样我们可以直接使用go build进行编译然后运行.exe查看效果了。否则的话还需要安装rsrc工具,对.manifest文件进行打包,然后再编译.exe文件。如果想编译出的.exe文件没有cmd命令行,则需要加上 -ldflags="-H windowsgui"参数,所以最终编译的命令为:go build -ldflags="-H windowsgui"

2、源码地址

项目名称为clientDemo

github地址:https://github.com/imcc2022/clientDemo

gitee地址:https://gitee.com/imcc2022/client-demo

访问不了github的同学可以访问gitee。项目的详细介绍在 README.md文件中。这里再贴一下

3、项目介绍

演示图
演示图

 

clientDemo

一个用walk框架写的windowsGUI示例(golang) / A windows GUI demo written by walk framework(golang)

功能点包括:输入框,密码行,登录,系统托盘,页面切换,窗口最大化禁用,窗口尺寸禁止修改等 / Function nodes include: input box, password line, login, system tray, page switching, window maximization disabled, window size disabled, etc

项目结构 / Project Structure

1:主目录(clientDemo)

master directory(clientDemo)

2:资源目录(clientDemo/resource)[包含图片和manifest声明打包文件]

resource directory(clientDemo/resource)[Include imgs and manifest files]

3:服务目录(clientDemo/service)[包含所有非ui相关的功能]

service directory(clientDemo/service)[Include all functions except ui]

4:界面目录(clientDemo/ui)[包含所有ui功能]

ui directory(clientDemo/ui)[Include all ui functions]

5:main.go[程序入口]

main.go[Program entry]

6:clientDemo.syso[由rsrc工具执行生成]

clientDemo.syso[Generate by rsrc tool]

7:go.mod[golang的包管理文件]

go.mod[Package management file of golang]

步骤 / Steps

1:安装golang环境,需要Go 1.11.x或更新的版本(https://go.dev/doc/install)

Install the golang environment,require Go 1.11.x or later(https://go.dev/doc/install)

2:执行"go mod tidy"命令下载go.mod文件中的依赖

Run "go mod tidy" command to download the dependencies in the go.mod file

3:如果想完整掌握项目编译过程,则需要安装rsrc工具

If you want to fully master the project compilation process, you need to install the rsrc tool

(1):执行"go get github.com/akavel/rsrc" 命令下载rsrc工具

Execute the "go get github.com/akavel/rsrc" command to download the rsrc tool

(2):进入GOPATH并找到rsrc目录

Enter GOPATH and find the rsrc directory

如果配置了GOPATH环境变量则可以直接通过"%GOPATH%\pkg\mod\github.com\akavel"进入

If the GOPATH environment variable is configured, you can directly enter through "%GOPATH%\pkg\mod\github.com\akavel"

如果没有配置且golang是默认安装的话,GOPATH通常会在"C:\Users\yourComputerName\go"目录下

If there is no configuration and golang is installed by default, GOPATH is usually in the "C: Users yourComputerName go" directory

(3):进入rsrc目录并将rsrc.exe文件拷贝到%GOROOT%\bin目录下,这时你就可以使用rsrc命令了(%GOROOT%是golang的安装目录)

Enter the rsrc directory and set the rsrc Copy the exe file to the% GOROOT% bin directory, and then you can use the rsrc command (% GOROOT% is the installation directory of golang)

(4):使用"rsrc -manifest resource/clientDemo.exe.manifest -o clientDemo.syso -ico resource/icon.ico"命令进行打包,"-manifest resource/clientDemo.exe.manifest"表示将"resource/clientDemo.exe.manifest"文件作为打包时的声明文件,"-o clientDemo.syso"设置生成的文件名称,"-ico resource/icon.ico"声明最终生成.exe文件的图标。PS:icon.ico文件必须是标准的.ico文件,不能是.jpeg文件直接修改文件后缀名形成。

Use the "rsrc -manifest resource/clientDemo.exe.manifest -o clientDemo.syso -ico resource/icon.ico" command to package. "-manifest resource/clientDemo.exe.manifest" means to use the "resource/clientDemo.exe.manifest" file as the declaration file when packaging. "-o clientDemo.syso" sets the name of the generated file. "-ico resource/icon.ico" declares the icon of the final generated.exe file. PS:The icon.ico file must be a standard. ico file, not a .jpeg file.can't get by Modifing the file suffix!

(5):执行" go build -ldflags="-H windowsgui" "命令进行编译,并形成.exe文件," -ldflags="-H windowsgui" "参数用于消除cmd命令框,如果需要则可以直接使用"go build"命令进行编译

Execute the " go build -ldflags="-H windowsgui" " command to compile and form Exe file, " -ldflags="-H windowsgui" " parameter is used to eliminate the cmd command box. If necessary, you can directly use the "go build" command to compile

(6):运行clientDemo.exe文件

run clientDemo.exe file

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先,我们需要了解两个概念:客户端和服务器端。客户端是指用户使用的设备,如电脑、手机等;服务器端是指提供服务的计算机,如网站、聊天室等。 在本例中,我们需要用到以下技术:HTML、CSS、JavaScript 和 Go 语言。其中,HTML、CSS 和 JavaScript 用于开发客户端,Go 语言用于开发服务器端。 以下是一个简单的聊天室实现步骤: 1. 创建 HTML 页面,包括聊天室界面和发送消息的表单。 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Chat Room</title> <style> /* 样式表 */ </style> </head> <body> <div id="chatroom"> <!-- 聊天室界面 --> </div> <form id="message-form"> <input type="text" id="message-input"> <button type="submit">Send</button> </form> <script src="chatroom.js"></script> </body> </html> ``` 2. 创建 CSS 样式表。 ```css /* 样式表 */ ``` 3. 创建 JavaScript 文件 chatroom.js,实现客户端与服务器端通信和消息显示功能。 ```javascript // 创建 WebSocket 连接 const ws = new WebSocket("ws://localhost:8000/ws"); // 监听连接事件 ws.addEventListener("open", event => { console.log("Connected to server"); }); // 监听消息事件 ws.addEventListener("message", event => { const message = event.data; // 在聊天室界面中显示消息 }); // 监听表单提交事件 const form = document.getElementById("message-form"); form.addEventListener("submit", event => { event.preventDefault(); // 阻止表单提交 const input = document.getElementById("message-input"); const message = input.value; input.value = ""; // 清空输入框 // 发送消息到服务器 ws.send(message); }); ``` 4. 创建 Go 语言服务器端,实现 WebSocket 连接和消息转发功能。 ```go package main import ( "fmt" "net/http" "github.com/gorilla/websocket" ) var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, } func main() { http.HandleFunc("/ws", handleWebSocket) http.ListenAndServe(":8000", nil) } func handleWebSocket(w http.ResponseWriter, r *http.Request) { conn, err := upgrader.Upgrade(w, r, nil) if err != nil { fmt.Println(err) return } defer conn.Close() for { // 接收消息 _, message, err := conn.ReadMessage() if err != nil { fmt.Println(err) break } // 转发消息到所有客户端 for _, c := range clients { err = c.WriteMessage(websocket.TextMessage, message) if err != nil { fmt.Println(err) break } } } } ``` 以上就是一个简单的聊天室实现步骤。需要注意的是,本例中的服务器端只是一个简单的示例,实际应用中需要考虑更多的安全性和可靠性问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值