您现在的位置是:网站首页> Go语言
walk UI程序
- Go语言
- 2021-04-05
- 909人已阅读
代码:
简单代码
package main
import (
"syscall"
"github.com/lxn/walk"
"github.com/lxn/win"
)
func main() {
window, _ := walk.NewMainWindow()
// 设置窗体标题
window.SetTitle(`你好世界!`)
// 设置窗体的宽高
window.SetWidth(400)
window.SetHeight(400)
// 设置窗体生成在屏幕的正中间
// 窗体横坐标 = ( 屏幕宽度 - 窗体宽度 ) / 2
// 窗体纵坐标 = ( 屏幕高度 - 窗体高度 ) / 2
window.SetX((int(win.GetSystemMetrics(0)) - window.Width()) / 2)
window.SetY((int(win.GetSystemMetrics(1)) - window.Height()) / 2)
// 设置窗体为显示状态(默认:隐藏状态)
window.Show()
// 运行窗体
window.Run()
}
// HelloWalkUI project main.go
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
type MyMainWindow struct {
*walk.MainWindow
edit *walk.TextEdit
}
func main() {
mw := &MyMainWindow{}
if err := (MainWindow{
AssignTo: &mw.MainWindow,
MinSize: Size{400, 300},
Size: Size{600, 400},
MenuItems: []MenuItem{
Menu{
Text: "文件",
Items: []MenuItem{
Action{
Text: "打开文件",
Shortcut: Shortcut{ //定义快捷键后会有响应提示显示
Modifiers: walk.ModControl,
Key: walk.KeyO,
},
OnTriggered: mw.openFileActionTriggered, //点击动作触发响应函数
},
Action{
Text: "另存为",
Shortcut: Shortcut{
Modifiers: walk.ModControl | walk.ModShift,
Key: walk.KeyS,
},
OnTriggered: mw.saveFileActionTriggered,
},
Action{
Text: "退出",
OnTriggered: func() {
mw.Close()
},
},
},
},
Menu{
Text: "帮助",
Items: []MenuItem{
Action{
Text: "关于",
OnTriggered: func() {
walk.MsgBox(mw, "关于", "这是一个菜单和工具栏的实例",
walk.MsgBoxIconInformation|walk.MsgBoxDefButton1)
},
},
},
},
},
ToolBar: ToolBar{ //工具栏
ButtonStyle: ToolBarButtonTextOnly,
Items: []MenuItem{
Menu{
Text: "New",
Items: []MenuItem{
Action{
Text: "A",
OnTriggered: mw.newAction_Triggered,
},
Action{
Text: "B",
OnTriggered: mw.newAction_Triggered,
},
},
OnTriggered: mw.newAction_Triggered, //在菜单中不可如此定义,会无响应
},
Separator{}, //分隔符
Action{
Text: "View",
OnTriggered: mw.changeViewAction_Triggered,
},
},
},
Layout: VBox{},
Children: []Widget{
TextEdit{
AssignTo: &mw.edit,
},
},
OnDropFiles: mw.dropFiles, //放置文件事件响应函数
}).Create(); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
mw.Run()
}
func (mw *MyMainWindow) openFileActionTriggered() {
dlg := new(walk.FileDialog)
dlg.Title = "打开文件"
dlg.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*"
if ok, err := dlg.ShowOpen(mw); err != nil {
fmt.Fprintln(os.Stderr, "错误:打开文件时\r\n")
return
} else if !ok {
fmt.Fprintln(os.Stderr, "用户取消\r\n")
return
}
s := fmt.Sprintf("选择了:%s\r\n", dlg.FilePath)
mw.edit.SetText(s)
}
func (mw *MyMainWindow) saveFileActionTriggered() {
dlg := new(walk.FileDialog)
dlg.Title = "另存为"
if ok, err := dlg.ShowSave(mw); err != nil {
fmt.Fprintln(os.Stderr, err)
return
} else if !ok {
fmt.Fprintln(os.Stderr, "取消")
return
}
data := mw.edit.Text()
filename := dlg.FilePath
f, err := os.Open(filename)
if err != nil {
f, _ = os.Create(filename)
} else {
f.Close()
f, err = os.OpenFile(filename, os.O_WRONLY, 0x666)
}
if len(data) == 0 {
f.Close()
return
}
io.Copy(f, strings.NewReader(data))
f.Close()
}
func (mw *MyMainWindow) newAction_Triggered() {
walk.MsgBox(mw, "New", "Newing something up... or not.", walk.MsgBoxIconInformation)
}
func (mw *MyMainWindow) changeViewAction_Triggered() {
walk.MsgBox(mw, "Change View", "By now you may have guessed it. Nothing changed.", walk.MsgBoxIconInformation)
}
func (mw *MyMainWindow) dropFiles(files []string) {
mw.edit.SetText("")
for _, v := range files {
mw.edit.AppendText(v + "\r\n")
}
}
需要同名exe文件加个.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>
下一篇:Go中调用C的动态库与静态库