1、安装lxn/walk
go get github.com/lxn/walk
2、创建test.go文件
package main
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"strings"
)
func main() {
var inTE, outTE *walk.TextEdit
MainWindow{
Title: "SCREAMO",
MinSize: Size{600, 400},
Layout: VBox{},
Children: []Widget{
HSplitter{
Children: []Widget{
TextEdit{AssignTo: &inTE},
TextEdit{AssignTo: &outTE, ReadOnly: true},
},
},
PushButton{
Text: "SCREAM",
OnClicked: func() {
outTE.SetText(strings.ToUpper(inTE.Text()))
},
},
},
}.Run()
}
3、创建系统支持文件test.manifest
<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, PerMonitordpiAwareness>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">TruedpiAware>
windowsSettings>
application>
assembly>
4 、生成rsrc.syso文件
go get github.com/akavel/rsrc
rsrc -manifest test.manifest -o rsrc.syso
5、只运行,不打包
go build
6、打包
go build -ldflags="-H windowsgui"
-----------------END----------------------
7、小工具
1、弹出框
walk.MsgBox(mw, "info", "警告信息", walk.MsgBoxIconInformation)
2、利用cmd调用默认浏览器打开链接
cmd := exec.Command(`cmd`, `/c`, `start`,arr["url"].(string))
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} //隐藏cmd窗口
3、修改打开软件显示位置
xScreen := win.GetSystemMetrics(win.SM_CXSCREEN)
yScreen := win.GetSystemMetrics(win.SM_CYSCREEN)
win.SetWindowPos(
mw.Handle(),
0,
(xScreen - SIZE_W)/2,
(yScreen - SIZE_H)/2,
SIZE_W,
SIZE_H,
win.SWP_FRAMECHANGED,
)
8、完整案例
package main
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"log"
"github.com/lxn/win"
"syscall"
"net"
"net/http"
"strings"
"io/ioutil"
"encoding/json"
"os/exec"
)
const (
SIZE_W = 570
SIZE_H = 380
)
var (
libuser32 *syscall.DLL
isIconic *syscall.Proc
)
func init() {
libuser32 = syscall.MustLoadDLL("user32.dll")
isIconic = libuser32.MustFindProc("IsIconic")
}
func IsIconic(hWnd win.HWND) bool {
ret, _, _ := isIconic.Call(uintptr(hWnd))
return ret == win.TRUE
}
type MyWindow struct {
*walk.MainWindow
}
func main() {
mw := new(MyWindow)
var username, password *walk.LineEdit
if err := (MainWindow{
AssignTo: &mw.MainWindow,
Title: "WitMooni",
MinSize: Size{50, 50},
MaxSize: Size{50, 50},
Size: Size{Width: 300, Height: 150},
Layout: VBox{},
Children: []Widget{
VSplitter{
Children: []Widget{
Label{
Text:"Имя Клиента",
},
LineEdit{
Text:"",
AssignTo: &username,
},
Label{
Text:"пароль",
},
LineEdit{
Text:"",
AssignTo: &password,
},
},
},
PushButton{
Text: "Нужно войти",
OnClicked: func() {
var username= username.Text()
var password= password.Text()
if username == "" {
walk.MsgBox(mw, "предостережение", "Счёт не может быть пустым", walk.MsgBoxIconInformation)
return
}
if password == "" {
walk.MsgBox(mw, "предостережение", "Пароль не может быть пустым", walk.MsgBoxIconInformation)
return
}
macAdd := ""
interfaces, _ := net.Interfaces()
for _, inter := range interfaces {
mac := inter.HardwareAddr
if mac.String() != "" {
if (mac.String() != "") {
mac := mac.String()
macAdd = mac
}
}
}
client := &http.Client{}
req, _ := http.NewRequest("POST", "http://www.****.com/user/login/*****",
strings.NewReader("username="+username+"&password="+password+"&mac="+macAdd))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Cookie", "liubaonanha=anny")
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
res := string(body)
str := []byte(res)
arr := make(map[string]interface{})
json.Unmarshal(str, &arr)
_, ok := arr["url"]
if (ok) {
//exec.Command(`cmd`, `/c`, `start`, arr["url"].(string)).Start()
cmd := exec.Command(`cmd`, `/c`, `start`,arr["url"].(string))
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} //隐藏cmd窗口
cmd.Start()
mw.Close()
} else {
walk.MsgBox(mw, "предостережение", arr["message"].(string), walk.MsgBoxIconInformation)
}
},
},
},
}).Create(); err != nil {
log.Fatal(err)
}
mw.SizeChanged().Attach(func() {
})
xScreen := win.GetSystemMetrics(win.SM_CXSCREEN)
yScreen := win.GetSystemMetrics(win.SM_CYSCREEN)
win.SetWindowPos(
mw.Handle(),
0,
(xScreen - SIZE_W)/2,
(yScreen - SIZE_H)/2,
SIZE_W,
SIZE_H,
win.SWP_FRAMECHANGED,
)
win.ShowWindow(mw.Handle(), win.SW_SHOW)
mw.Run()
}