main.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/lxn/walk"
  6. "github.com/lxn/walk/declarative"
  7. )
  8. type MyMainWindow struct {
  9. *walk.MainWindow
  10. edit *walk.TextEdit
  11. }
  12. func main() {
  13. mw := &MyMainWindow{}
  14. err := declarative.MainWindow{
  15. AssignTo: &mw.MainWindow, //窗口重定向至mw,重定向后可由重定向变量控制控件
  16. // Icon: "test.ico", //窗体图标
  17. Title: "文件选择对话框", //标题
  18. MinSize: declarative.Size{Width: 150, Height: 200},
  19. Size: declarative.Size{300, 400},
  20. Layout: declarative.VBox{}, //样式,纵向
  21. Children: []declarative.Widget{ //控件组
  22. declarative.TextEdit{
  23. AssignTo: &mw.edit,
  24. },
  25. declarative.PushButton{
  26. Text: "打开",
  27. OnClicked: mw.selectFile, //点击事件响应函数
  28. },
  29. },
  30. }.Create() //创建
  31. if err != nil {
  32. fmt.Fprintln(os.Stderr, err)
  33. os.Exit(1)
  34. }
  35. mw.Run() //运行
  36. }
  37. func (mw *MyMainWindow) selectFile() {
  38. dlg := new(walk.FileDialog)
  39. dlg.Title = "选择文件"
  40. dlg.Filter = "可执行文件 (*.exe)|*.exe|所有文件 (*.*)|*.*"
  41. mw.edit.SetText("") //通过重定向变量设置TextEdit的Text
  42. if ok, err := dlg.ShowOpen(mw); err != nil {
  43. mw.edit.AppendText("Error : File Open\r\n")
  44. return
  45. } else if !ok {
  46. mw.edit.AppendText("Cancel\r\n")
  47. return
  48. }
  49. s := fmt.Sprintf("Select : %s\r\n", dlg.FilePath)
  50. mw.edit.AppendText(s)
  51. }