lxn / walk

A Windows GUI toolkit for the Go Programming Language
Other
6.79k stars 885 forks source link

mw.Hide() Will not hide to the tray, just exit #699

Closed rookiefront closed 3 years ago

rookiefront commented 3 years ago
package main

import (
    "github.com/lxn/win"
    "log"

    "github.com/lxn/walk"
    . "github.com/lxn/walk/declarative"
)

type MyWindow struct {
    *walk.MainWindow
    ni   *walk.NotifyIcon
}

func (mw *MyWindow) AddNotifyIcon() {
    var err error
    mw.ni, err = walk.NewNotifyIcon(mw)
    if err != nil {
        log.Fatal(err)
    }

    icon, err := walk.Resources.Image("show.ico")
    if err != nil {
        log.Fatal(err)
    }
    mw.SetIcon(icon)
    mw.ni.SetIcon(icon)
    mw.ni.SetVisible(true)

    mw.ni.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
        if button == walk.LeftButton {
            mw.Show()
            win.ShowWindow(mw.Handle(), win.SW_RESTORE)
        }
    })

}

func main() {
    mw := new(MyWindow)
    if err := (MainWindow{
        AssignTo: &mw.MainWindow,
        Title:    "notify icon",
        Size:     Size{600, 380},
        Layout:   VBox{MarginsZero: true},
        //OnSizeChanged: func() {
        //  if win.IsIconic(mw.Handle()) {
        //      mw.Hide()
        //  }
        //},
    }.Create()); err != nil {
        log.Fatal(err)
    }

    mw.AddNotifyIcon()
    mw.Closing().Attach(func(canceled *bool, reason walk.CloseReason) {
        mw.Hide()
        //mw.Run()
        //fmt.Println(reason)
        //  fmt.Println(canceled)
        //mw.Hide()
        //fmt.Println(canceled)
        //fmt.Println(reflect.TypeOf(reason).String())
        //if reason == walk.CloseReasonUser {
        //  *canceled = true
        //
        //  mw.Hide()
        //}
    })

    mw.Run()
}
rookiefront commented 3 years ago
*canceled=true
mw.Hide()

Can achieve the effect