123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package main
- import (
- "fmt"
- "os"
- "github.com/lxn/walk"
- "github.com/lxn/walk/declarative"
- )
- type MyMainWindow struct {
- *walk.MainWindow
- edit *walk.TextEdit
- }
- func main() {
- mw := &MyMainWindow{}
- err := declarative.MainWindow{
- AssignTo: &mw.MainWindow, //窗口重定向至mw,重定向后可由重定向变量控制控件
- // Icon: "test.ico", //窗体图标
- Title: "文件选择对话框", //标题
- MinSize: declarative.Size{Width: 150, Height: 200},
- Size: declarative.Size{300, 400},
- Layout: declarative.VBox{}, //样式,纵向
- Children: []declarative.Widget{ //控件组
- declarative.TextEdit{
- AssignTo: &mw.edit,
- },
- declarative.PushButton{
- Text: "打开",
- OnClicked: mw.selectFile, //点击事件响应函数
- },
- },
- }.Create() //创建
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- os.Exit(1)
- }
- mw.Run() //运行
- }
- func (mw *MyMainWindow) selectFile() {
- dlg := new(walk.FileDialog)
- dlg.Title = "选择文件"
- dlg.Filter = "可执行文件 (*.exe)|*.exe|所有文件 (*.*)|*.*"
- mw.edit.SetText("") //通过重定向变量设置TextEdit的Text
- if ok, err := dlg.ShowOpen(mw); err != nil {
- mw.edit.AppendText("Error : File Open\r\n")
- return
- } else if !ok {
- mw.edit.AppendText("Cancel\r\n")
- return
- }
- s := fmt.Sprintf("Select : %s\r\n", dlg.FilePath)
- mw.edit.AppendText(s)
- }
|