From 971520115f384633d747ee42d90349751385ddea Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sun, 19 Jun 2022 00:48:38 +0100 Subject: [PATCH] Refactor (windows). --- const_windows.go | 22 --------------------- date_windows.go | 28 +++++++++++++------------- entry_windows.go | 26 ++++++++++++------------ file_windows.go | 4 ++-- internal/win/user32.go | 26 ++++++++++++++++++++++++ internal/win/zsyscall_windows.go | 30 ++++++++++++++++++++++++++++ list_windows.go | 32 +++++++++++++++--------------- msg_windows.go | 9 ++++----- progress_windows.go | 34 ++++++++++++++++---------------- pwd_windows.go | 30 ++++++++++++++-------------- util_windows.go | 22 ++++++++++----------- 11 files changed, 147 insertions(+), 116 deletions(-) diff --git a/const_windows.go b/const_windows.go index cba0578..86f58df 100644 --- a/const_windows.go +++ b/const_windows.go @@ -39,28 +39,6 @@ const ( _SC_CLOSE = 0xf060 - _WM_DESTROY = 0x0002 - _WM_CLOSE = 0x0010 - _WM_SETFONT = 0x0030 - _WM_SETICON = 0x0080 - _WM_NCCREATE = 0x0081 - _WM_NCDESTROY = 0x0082 - _WM_COMMAND = 0x0111 - _WM_SYSCOMMAND = 0x0112 - _WM_DPICHANGED = 0x02e0 - _WM_USER = 0x0400 - _EM_SETSEL = 0x00b1 - _LB_ADDSTRING = 0x0180 - _LB_GETCURSEL = 0x0188 - _LB_GETSELCOUNT = 0x0190 - _LB_GETSELITEMS = 0x0191 - _MCM_GETCURSEL = 0x1001 - _MCM_SETCURSEL = 0x1002 - _PBM_SETPOS = _WM_USER + 2 - _PBM_SETRANGE32 = _WM_USER + 6 - _PBM_SETMARQUEE = _WM_USER + 10 - _STM_SETICON = 0x0170 - _GWL_STYLE = -16 _PROGRESS_CLASS = "msctls_progress32" diff --git a/date_windows.go b/date_windows.go index 043f19f..f70f5c8 100644 --- a/date_windows.go +++ b/date_windows.go @@ -97,7 +97,7 @@ func (dlg *calendarDialog) setup(text string, opts options) (time.Time, error) { date.year = uint16(year) date.month = uint16(month) date.day = uint16(day) - sendMessage.Call(dlg.dateCtl, _MCM_SETCURSEL, 0, uintptr(unsafe.Pointer(&date))) + sendMessage.Call(dlg.dateCtl, win.MCM_SETCURSEL, 0, uintptr(unsafe.Pointer(&date))) } dlg.layout(getDPI(dlg.wnd)) @@ -111,7 +111,7 @@ func (dlg *calendarDialog) setup(text string, opts options) (time.Time, error) { go func() { select { case <-opts.ctx.Done(): - sendMessage.Call(dlg.wnd, _WM_SYSCOMMAND, _SC_CLOSE, 0) + sendMessage.Call(dlg.wnd, win.WM_SYSCOMMAND, _SC_CLOSE, 0) case <-wait: } }() @@ -128,11 +128,11 @@ func (dlg *calendarDialog) setup(text string, opts options) (time.Time, error) { func (dlg *calendarDialog) layout(dpi dpi) { font := dlg.font.forDPI(dpi) - sendMessage.Call(dlg.textCtl, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.dateCtl, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.okBtn, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.cancelBtn, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.extraBtn, _WM_SETFONT, font, 1) + sendMessage.Call(dlg.textCtl, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.dateCtl, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.okBtn, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.cancelBtn, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.extraBtn, win.WM_SETFONT, font, 1) setWindowPos.Call(dlg.wnd, 0, 0, 0, dpi.scale(281), dpi.scale(281), _SWP_NOZORDER|_SWP_NOMOVE) setWindowPos.Call(dlg.textCtl, 0, dpi.scale(12), dpi.scale(10), dpi.scale(241), dpi.scale(16), _SWP_NOZORDER) setWindowPos.Call(dlg.dateCtl, 0, dpi.scale(12), dpi.scale(30), dpi.scale(241), dpi.scale(164), _SWP_NOZORDER) @@ -149,30 +149,30 @@ func (dlg *calendarDialog) layout(dpi dpi) { func calendarProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr { var dlg *calendarDialog switch msg { - case _WM_NCCREATE: + case win.WM_NCCREATE: saveBackRef(wnd, *lparam) dlg = (*calendarDialog)(*lparam) - case _WM_NCDESTROY: + case win.WM_NCDESTROY: deleteBackRef(wnd) default: dlg = (*calendarDialog)(loadBackRef(wnd)) } switch msg { - case _WM_DESTROY: + case win.WM_DESTROY: postQuitMessage.Call(0) - case _WM_CLOSE: + case win.WM_CLOSE: dlg.err = ErrCanceled destroyWindow.Call(wnd) - case _WM_COMMAND: + case win.WM_COMMAND: switch wparam { default: return 1 case win.IDOK, win.IDYES: var date _SYSTEMTIME - sendMessage.Call(dlg.dateCtl, _MCM_GETCURSEL, 0, uintptr(unsafe.Pointer(&date))) + sendMessage.Call(dlg.dateCtl, win.MCM_GETCURSEL, 0, uintptr(unsafe.Pointer(&date))) dlg.out = time.Date(int(date.year), time.Month(date.month), int(date.day), 0, 0, 0, 0, time.UTC) case win.IDCANCEL: dlg.err = ErrCanceled @@ -181,7 +181,7 @@ func calendarProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointe } destroyWindow.Call(wnd) - case _WM_DPICHANGED: + case win.WM_DPICHANGED: dlg.layout(dpi(uint32(wparam) >> 16)) default: diff --git a/entry_windows.go b/entry_windows.go index 6d09cb1..13af8f6 100644 --- a/entry_windows.go +++ b/entry_windows.go @@ -97,7 +97,7 @@ func (dlg *entryDialog) setup(text string, opts options) (string, error) { centerWindow(dlg.wnd) setFocus.Call(dlg.editCtl) showWindow.Call(dlg.wnd, _SW_NORMAL, 0) - sendMessage.Call(dlg.editCtl, _EM_SETSEL, 0, intptr(-1)) + sendMessage.Call(dlg.editCtl, win.EM_SETSEL, 0, intptr(-1)) if opts.ctx != nil { wait := make(chan struct{}) @@ -105,7 +105,7 @@ func (dlg *entryDialog) setup(text string, opts options) (string, error) { go func() { select { case <-opts.ctx.Done(): - sendMessage.Call(dlg.wnd, _WM_SYSCOMMAND, _SC_CLOSE, 0) + sendMessage.Call(dlg.wnd, win.WM_SYSCOMMAND, _SC_CLOSE, 0) case <-wait: } }() @@ -122,11 +122,11 @@ func (dlg *entryDialog) setup(text string, opts options) (string, error) { func (dlg *entryDialog) layout(dpi dpi) { font := dlg.font.forDPI(dpi) - sendMessage.Call(dlg.textCtl, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.editCtl, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.okBtn, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.cancelBtn, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.extraBtn, _WM_SETFONT, font, 1) + sendMessage.Call(dlg.textCtl, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.editCtl, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.okBtn, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.cancelBtn, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.extraBtn, win.WM_SETFONT, font, 1) setWindowPos.Call(dlg.wnd, 0, 0, 0, dpi.scale(281), dpi.scale(141), _SWP_NOZORDER|_SWP_NOMOVE) setWindowPos.Call(dlg.textCtl, 0, dpi.scale(12), dpi.scale(10), dpi.scale(241), dpi.scale(16), _SWP_NOZORDER) setWindowPos.Call(dlg.editCtl, 0, dpi.scale(12), dpi.scale(30), dpi.scale(241), dpi.scale(24), _SWP_NOZORDER) @@ -143,24 +143,24 @@ func (dlg *entryDialog) layout(dpi dpi) { func entryProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr { var dlg *entryDialog switch msg { - case _WM_NCCREATE: + case win.WM_NCCREATE: saveBackRef(wnd, *lparam) dlg = (*entryDialog)(*lparam) - case _WM_NCDESTROY: + case win.WM_NCDESTROY: deleteBackRef(wnd) default: dlg = (*entryDialog)(loadBackRef(wnd)) } switch msg { - case _WM_DESTROY: + case win.WM_DESTROY: postQuitMessage.Call(0) - case _WM_CLOSE: + case win.WM_CLOSE: dlg.err = ErrCanceled destroyWindow.Call(wnd) - case _WM_COMMAND: + case win.WM_COMMAND: switch wparam { default: return 1 @@ -173,7 +173,7 @@ func entryProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) } destroyWindow.Call(wnd) - case _WM_DPICHANGED: + case win.WM_DPICHANGED: dlg.layout(dpi(uint32(wparam) >> 16)) default: diff --git a/file_windows.go b/file_windows.go index d9e7918..939e8db 100644 --- a/file_windows.go +++ b/file_windows.go @@ -348,9 +348,9 @@ func browseForFolder(opts options) (string, []string, error) { return str, []string{str}, nil } -func browseForFolderCallback(wnd uintptr, msg uint32, lparam, data uintptr) uintptr { +func browseForFolderCallback(wnd win.HWND, msg uint32, lparam, data uintptr) uintptr { if msg == 1 { // BFFM_INITIALIZED - sendMessage.Call(wnd, 1024+103 /* BFFM_SETSELECTIONW */, 1 /* TRUE */, data) + win.SendMessage(wnd, 1024+103 /* BFFM_SETSELECTIONW */, 1 /* TRUE */, data) } return 0 } diff --git a/internal/win/user32.go b/internal/win/user32.go index 6d1e16b..a7b0534 100644 --- a/internal/win/user32.go +++ b/internal/win/user32.go @@ -27,6 +27,28 @@ const ( MB_DEFBUTTON1 = windows.MB_DEFBUTTON1 MB_DEFBUTTON2 = windows.MB_DEFBUTTON2 MB_DEFBUTTON3 = windows.MB_DEFBUTTON3 + + WM_DESTROY = 0x0002 + WM_CLOSE = 0x0010 + WM_SETFONT = 0x0030 + WM_SETICON = 0x0080 + WM_NCCREATE = 0x0081 + WM_NCDESTROY = 0x0082 + WM_COMMAND = 0x0111 + WM_SYSCOMMAND = 0x0112 + WM_DPICHANGED = 0x02e0 + WM_USER = 0x0400 + EM_SETSEL = 0x00b1 + LB_ADDSTRING = 0x0180 + LB_GETCURSEL = 0x0188 + LB_GETSELCOUNT = 0x0190 + LB_GETSELITEMS = 0x0191 + MCM_GETCURSEL = 0x1001 + MCM_SETCURSEL = 0x1002 + PBM_SETPOS = WM_USER + 2 + PBM_SETRANGE32 = WM_USER + 6 + PBM_SETMARQUEE = WM_USER + 10 + STM_SETICON = 0x0170 ) func MessageBox(hwnd HWND, text *uint16, caption *uint16, boxtype uint32) (ret int32, err error) { @@ -34,3 +56,7 @@ func MessageBox(hwnd HWND, text *uint16, caption *uint16, boxtype uint32) (ret i } //sys GetDlgCtrlID(wnd HWND) (ret int) = user32.GetDlgCtrlID +//sys SendMessage(wnd HWND, msg uint32, wparam uintptr, lparam uintptr) (ret uintptr) = user32.SendMessageW +//sys SetWindowText(wnd HWND, text *uint16) (err error) = user32.SetWindowTextW +//sys EnumChildWindows(parent HWND, enumFunc uintptr, lparam uintptr) = user32.EnumChildWindows +//sys EnumWindows(enumFunc uintptr, lparam uintptr) (err error) = user32.EnumChildWindows diff --git a/internal/win/zsyscall_windows.go b/internal/win/zsyscall_windows.go index 10a1c85..130d932 100644 --- a/internal/win/zsyscall_windows.go +++ b/internal/win/zsyscall_windows.go @@ -62,7 +62,10 @@ var ( procSHCreateItemFromParsingName = modshell32.NewProc("SHCreateItemFromParsingName") procSHGetPathFromIDListEx = modshell32.NewProc("SHGetPathFromIDListEx") procShell_NotifyIconW = modshell32.NewProc("Shell_NotifyIconW") + procEnumChildWindows = moduser32.NewProc("EnumChildWindows") procGetDlgCtrlID = moduser32.NewProc("GetDlgCtrlID") + procSendMessageW = moduser32.NewProc("SendMessageW") + procSetWindowTextW = moduser32.NewProc("SetWindowTextW") procWTSSendMessageW = modwtsapi32.NewProc("WTSSendMessageW") ) @@ -161,12 +164,39 @@ func ShellNotifyIcon(message uint32, data *NOTIFYICONDATA) (ret int, err error) return } +func EnumChildWindows(parent HWND, enumFunc uintptr, lparam uintptr) { + syscall.Syscall(procEnumChildWindows.Addr(), 3, uintptr(parent), uintptr(enumFunc), uintptr(lparam)) + return +} + +func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) { + r1, _, e1 := syscall.Syscall(procEnumChildWindows.Addr(), 2, uintptr(enumFunc), uintptr(lparam), 0) + if r1 == 0 { + err = errnoErr(e1) + } + return +} + func GetDlgCtrlID(wnd HWND) (ret int) { r0, _, _ := syscall.Syscall(procGetDlgCtrlID.Addr(), 1, uintptr(wnd), 0, 0) ret = int(r0) return } +func SendMessage(wnd HWND, msg uint32, wparam uintptr, lparam uintptr) (ret uintptr) { + r0, _, _ := syscall.Syscall6(procSendMessageW.Addr(), 4, uintptr(wnd), uintptr(msg), uintptr(wparam), uintptr(lparam), 0, 0) + ret = uintptr(r0) + return +} + +func SetWindowText(wnd HWND, text *uint16) (err error) { + r1, _, e1 := syscall.Syscall(procSetWindowTextW.Addr(), 2, uintptr(wnd), uintptr(unsafe.Pointer(text)), 0) + if r1 == 0 { + err = errnoErr(e1) + } + return +} + func WTSSendMessage(server Handle, sessionID uint32, title *uint16, titleLength int, message *uint16, messageLength int, style uint32, timeout int, response *uint32, wait bool) (err error) { var _p0 uint32 if wait { diff --git a/list_windows.go b/list_windows.go index 93a1d25..e5c5b36 100644 --- a/list_windows.go +++ b/list_windows.go @@ -111,7 +111,7 @@ func (dlg *listDialog) setup(text string, opts options) ([]string, error) { } for _, item := range dlg.items { - sendMessage.Call(dlg.listCtl, _LB_ADDSTRING, 0, strptr(item)) + sendMessage.Call(dlg.listCtl, win.LB_ADDSTRING, 0, strptr(item)) } dlg.layout(getDPI(dlg.wnd)) @@ -125,7 +125,7 @@ func (dlg *listDialog) setup(text string, opts options) ([]string, error) { go func() { select { case <-opts.ctx.Done(): - sendMessage.Call(dlg.wnd, _WM_SYSCOMMAND, _SC_CLOSE, 0) + sendMessage.Call(dlg.wnd, win.WM_SYSCOMMAND, _SC_CLOSE, 0) case <-wait: } }() @@ -142,11 +142,11 @@ func (dlg *listDialog) setup(text string, opts options) ([]string, error) { func (dlg *listDialog) layout(dpi dpi) { font := dlg.font.forDPI(dpi) - sendMessage.Call(dlg.textCtl, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.listCtl, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.okBtn, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.cancelBtn, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.extraBtn, _WM_SETFONT, font, 1) + sendMessage.Call(dlg.textCtl, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.listCtl, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.okBtn, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.cancelBtn, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.extraBtn, win.WM_SETFONT, font, 1) setWindowPos.Call(dlg.wnd, 0, 0, 0, dpi.scale(281), dpi.scale(281), _SWP_NOZORDER|_SWP_NOMOVE) setWindowPos.Call(dlg.textCtl, 0, dpi.scale(12), dpi.scale(10), dpi.scale(241), dpi.scale(16), _SWP_NOZORDER) setWindowPos.Call(dlg.listCtl, 0, dpi.scale(12), dpi.scale(30), dpi.scale(241), dpi.scale(164), _SWP_NOZORDER) @@ -163,41 +163,41 @@ func (dlg *listDialog) layout(dpi dpi) { func listProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr { var dlg *listDialog switch msg { - case _WM_NCCREATE: + case win.WM_NCCREATE: saveBackRef(wnd, *lparam) dlg = (*listDialog)(*lparam) - case _WM_NCDESTROY: + case win.WM_NCDESTROY: deleteBackRef(wnd) default: dlg = (*listDialog)(loadBackRef(wnd)) } switch msg { - case _WM_DESTROY: + case win.WM_DESTROY: postQuitMessage.Call(0) - case _WM_CLOSE: + case win.WM_CLOSE: dlg.err = ErrCanceled destroyWindow.Call(wnd) - case _WM_COMMAND: + case win.WM_COMMAND: switch wparam { default: return 1 case win.IDOK, win.IDYES: if dlg.multiple { - if len, _, _ := sendMessage.Call(dlg.listCtl, _LB_GETSELCOUNT, 0, 0); int32(len) >= 0 { + if len, _, _ := sendMessage.Call(dlg.listCtl, win.LB_GETSELCOUNT, 0, 0); int32(len) >= 0 { dlg.out = make([]string, len) if len > 0 { indices := make([]int32, len) - sendMessage.Call(dlg.listCtl, _LB_GETSELITEMS, len, uintptr(unsafe.Pointer(&indices[0]))) + sendMessage.Call(dlg.listCtl, win.LB_GETSELITEMS, len, uintptr(unsafe.Pointer(&indices[0]))) for i, idx := range indices { dlg.out[i] = dlg.items[idx] } } } } else { - if idx, _, _ := sendMessage.Call(dlg.listCtl, _LB_GETCURSEL, 0, 0); int32(idx) >= 0 { + if idx, _, _ := sendMessage.Call(dlg.listCtl, win.LB_GETCURSEL, 0, 0); int32(idx) >= 0 { dlg.out = []string{dlg.items[idx]} } else { dlg.out = []string{} @@ -210,7 +210,7 @@ func listProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) u } destroyWindow.Call(wnd) - case _WM_DPICHANGED: + case win.WM_DPICHANGED: dlg.layout(dpi(uint32(wparam) >> 16)) default: diff --git a/msg_windows.go b/msg_windows.go index 8923ca9..cb9101a 100644 --- a/msg_windows.go +++ b/msg_windows.go @@ -88,9 +88,8 @@ func message(kind messageKind, text string, opts options) error { } func hookMessageDialog(opts options) (unhook context.CancelFunc, err error) { - return hookDialog(opts.ctx, opts.windowIcon, nil, func(wnd uintptr) { - enumChildWindows.Call(wnd, - syscall.NewCallback(hookMessageDialogCallback), + return hookDialog(opts.ctx, opts.windowIcon, nil, func(wnd win.HWND) { + win.EnumChildWindows(wnd, syscall.NewCallback(hookMessageDialogCallback), uintptr(unsafe.Pointer(&opts))) }) } @@ -108,14 +107,14 @@ func hookMessageDialogCallback(wnd win.HWND, lparam *options) uintptr { text = lparam.extraButton } if text != nil { - setWindowText.Call(uintptr(wnd), strptr(*text)) + win.SetWindowText(wnd, syscall.StringToUTF16Ptr(*text)) } if ctl == 20 /*IDC_STATIC_OK*/ { icon := getIcon(lparam.icon) if icon.handle != 0 { defer icon.delete() - sendMessage.Call(uintptr(wnd), _STM_SETICON, icon.handle, 0) + win.SendMessage(wnd, win.STM_SETICON, icon.handle, 0) } } return 1 diff --git a/progress_windows.go b/progress_windows.go index bc83139..0d021d7 100644 --- a/progress_windows.go +++ b/progress_windows.go @@ -71,7 +71,7 @@ func (d *progressDialog) Text(text string) error { func (d *progressDialog) Value(value int) error { select { default: - sendMessage.Call(d.progCtl, _PBM_SETPOS, uintptr(value), 0) + sendMessage.Call(d.progCtl, win.PBM_SETPOS, uintptr(value), 0) if value >= d.max { enableWindow.Call(d.okBtn, 1) } @@ -93,8 +93,8 @@ func (d *progressDialog) Complete() error { select { default: setWindowLong.Call(d.progCtl, intptr(_GWL_STYLE), _WS_CHILD|_WS_VISIBLE|_PBS_SMOOTH) - sendMessage.Call(d.progCtl, _PBM_SETRANGE32, 0, 1) - sendMessage.Call(d.progCtl, _PBM_SETPOS, 1, 0) + sendMessage.Call(d.progCtl, win.PBM_SETRANGE32, 0, 1) + sendMessage.Call(d.progCtl, win.PBM_SETPOS, 1, 0) enableWindow.Call(d.okBtn, 1) enableWindow.Call(d.cancelBtn, 0) return nil @@ -104,7 +104,7 @@ func (d *progressDialog) Complete() error { } func (d *progressDialog) Close() error { - sendMessage.Call(d.wnd, _WM_SYSCOMMAND, _SC_CLOSE, 0) + sendMessage.Call(d.wnd, win.WM_SYSCOMMAND, _SC_CLOSE, 0) <-d.done if d.err == ErrCanceled { return nil @@ -179,9 +179,9 @@ func (dlg *progressDialog) setup(opts options) error { centerWindow(dlg.wnd) showWindow.Call(dlg.wnd, _SW_NORMAL, 0) if opts.maxValue < 0 { - sendMessage.Call(dlg.progCtl, _PBM_SETMARQUEE, 1, 0) + sendMessage.Call(dlg.progCtl, win.PBM_SETMARQUEE, 1, 0) } else { - sendMessage.Call(dlg.progCtl, _PBM_SETRANGE32, 0, uintptr(opts.maxValue)) + sendMessage.Call(dlg.progCtl, win.PBM_SETRANGE32, 0, uintptr(opts.maxValue)) } once.Do(dlg.init.Done) @@ -191,7 +191,7 @@ func (dlg *progressDialog) setup(opts options) error { go func() { select { case <-opts.ctx.Done(): - sendMessage.Call(dlg.wnd, _WM_SYSCOMMAND, _SC_CLOSE, 0) + sendMessage.Call(dlg.wnd, win.WM_SYSCOMMAND, _SC_CLOSE, 0) case <-wait: } }() @@ -208,10 +208,10 @@ func (dlg *progressDialog) setup(opts options) error { func (d *progressDialog) layout(dpi dpi) { font := d.font.forDPI(dpi) - sendMessage.Call(d.textCtl, _WM_SETFONT, font, 1) - sendMessage.Call(d.okBtn, _WM_SETFONT, font, 1) - sendMessage.Call(d.cancelBtn, _WM_SETFONT, font, 1) - sendMessage.Call(d.extraBtn, _WM_SETFONT, font, 1) + sendMessage.Call(d.textCtl, win.WM_SETFONT, font, 1) + sendMessage.Call(d.okBtn, win.WM_SETFONT, font, 1) + sendMessage.Call(d.cancelBtn, win.WM_SETFONT, font, 1) + sendMessage.Call(d.extraBtn, win.WM_SETFONT, font, 1) setWindowPos.Call(d.wnd, 0, 0, 0, dpi.scale(281), dpi.scale(133), _SWP_NOZORDER|_SWP_NOMOVE) setWindowPos.Call(d.textCtl, 0, dpi.scale(12), dpi.scale(10), dpi.scale(241), dpi.scale(16), _SWP_NOZORDER) setWindowPos.Call(d.progCtl, 0, dpi.scale(12), dpi.scale(30), dpi.scale(241), dpi.scale(16), _SWP_NOZORDER) @@ -237,24 +237,24 @@ func (d *progressDialog) layout(dpi dpi) { func progressProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr { var dlg *progressDialog switch msg { - case _WM_NCCREATE: + case win.WM_NCCREATE: saveBackRef(wnd, *lparam) dlg = (*progressDialog)(*lparam) - case _WM_NCDESTROY: + case win.WM_NCDESTROY: deleteBackRef(wnd) default: dlg = (*progressDialog)(loadBackRef(wnd)) } switch msg { - case _WM_DESTROY: + case win.WM_DESTROY: postQuitMessage.Call(0) - case _WM_CLOSE: + case win.WM_CLOSE: dlg.err = ErrCanceled destroyWindow.Call(wnd) - case _WM_COMMAND: + case win.WM_COMMAND: switch wparam { default: return 1 @@ -267,7 +267,7 @@ func progressProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointe } destroyWindow.Call(wnd) - case _WM_DPICHANGED: + case win.WM_DPICHANGED: dlg.layout(dpi(uint32(wparam) >> 16)) default: diff --git a/pwd_windows.go b/pwd_windows.go index 7f1bed4..9bd79d1 100644 --- a/pwd_windows.go +++ b/pwd_windows.go @@ -114,7 +114,7 @@ func (dlg *passwordDialog) setup(opts options) (string, string, error) { centerWindow(dlg.wnd) setFocus.Call(dlg.uEditCtl) showWindow.Call(dlg.wnd, _SW_NORMAL, 0) - sendMessage.Call(dlg.uEditCtl, _EM_SETSEL, 0, intptr(-1)) + sendMessage.Call(dlg.uEditCtl, win.EM_SETSEL, 0, intptr(-1)) if opts.ctx != nil { wait := make(chan struct{}) @@ -122,7 +122,7 @@ func (dlg *passwordDialog) setup(opts options) (string, string, error) { go func() { select { case <-opts.ctx.Done(): - sendMessage.Call(dlg.wnd, _WM_SYSCOMMAND, _SC_CLOSE, 0) + sendMessage.Call(dlg.wnd, win.WM_SYSCOMMAND, _SC_CLOSE, 0) case <-wait: } }() @@ -139,13 +139,13 @@ func (dlg *passwordDialog) setup(opts options) (string, string, error) { func (dlg *passwordDialog) layout(dpi dpi) { font := dlg.font.forDPI(dpi) - sendMessage.Call(dlg.uTextCtl, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.uEditCtl, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.pTextCtl, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.pEditCtl, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.okBtn, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.cancelBtn, _WM_SETFONT, font, 1) - sendMessage.Call(dlg.extraBtn, _WM_SETFONT, font, 1) + sendMessage.Call(dlg.uTextCtl, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.uEditCtl, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.pTextCtl, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.pEditCtl, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.okBtn, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.cancelBtn, win.WM_SETFONT, font, 1) + sendMessage.Call(dlg.extraBtn, win.WM_SETFONT, font, 1) setWindowPos.Call(dlg.wnd, 0, 0, 0, dpi.scale(281), dpi.scale(191), _SWP_NOZORDER|_SWP_NOMOVE) setWindowPos.Call(dlg.uTextCtl, 0, dpi.scale(12), dpi.scale(10), dpi.scale(241), dpi.scale(16), _SWP_NOZORDER) setWindowPos.Call(dlg.uEditCtl, 0, dpi.scale(12), dpi.scale(30), dpi.scale(241), dpi.scale(24), _SWP_NOZORDER) @@ -164,24 +164,24 @@ func (dlg *passwordDialog) layout(dpi dpi) { func passwordProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr { var dlg *passwordDialog switch msg { - case _WM_NCCREATE: + case win.WM_NCCREATE: saveBackRef(wnd, *lparam) dlg = (*passwordDialog)(*lparam) - case _WM_NCDESTROY: + case win.WM_NCDESTROY: deleteBackRef(wnd) default: dlg = (*passwordDialog)(loadBackRef(wnd)) } switch msg { - case _WM_DESTROY: + case win.WM_DESTROY: postQuitMessage.Call(0) - case _WM_CLOSE: + case win.WM_CLOSE: dlg.err = ErrCanceled destroyWindow.Call(wnd) - case _WM_COMMAND: + case win.WM_COMMAND: switch wparam { default: return 1 @@ -195,7 +195,7 @@ func passwordProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointe } destroyWindow.Call(wnd) - case _WM_DPICHANGED: + case win.WM_DPICHANGED: dlg.layout(dpi(uint32(wparam) >> 16)) default: diff --git a/util_windows.go b/util_windows.go index 335f662..a33709f 100644 --- a/util_windows.go +++ b/util_windows.go @@ -36,8 +36,6 @@ var ( destroyWindow = user32.NewProc("DestroyWindow") dispatchMessage = user32.NewProc("DispatchMessageW") enableWindow = user32.NewProc("EnableWindow") - enumChildWindows = user32.NewProc("EnumChildWindows") - enumWindows = user32.NewProc("EnumWindows") getDpiForWindow = user32.NewProc("GetDpiForWindow") getMessage = user32.NewProc("GetMessageW") getSystemMetrics = user32.NewProc("GetSystemMetrics") @@ -79,7 +77,7 @@ func hwnd(i uint64) win.HWND { return win.HWND(uintptr(i)) } func setup() context.CancelFunc { var wnd uintptr - enumWindows.Call(syscall.NewCallback(setupEnumCallback), uintptr(unsafe.Pointer(&wnd))) + win.EnumWindows(syscall.NewCallback(setupEnumCallback), uintptr(unsafe.Pointer(&wnd))) if wnd == 0 { wnd, _, _ = getConsoleWindow.Call() } @@ -130,7 +128,7 @@ func setupEnumCallback(wnd uintptr, lparam *uintptr) uintptr { return 1 // continue enumeration } -func hookDialog(ctx context.Context, icon any, title *string, init func(wnd uintptr)) (unhook context.CancelFunc, err error) { +func hookDialog(ctx context.Context, icon any, title *string, init func(wnd win.HWND)) (unhook context.CancelFunc, err error) { if ctx != nil && ctx.Err() != nil { return nil, ctx.Err() } @@ -149,10 +147,10 @@ type dialogHook struct { done chan struct{} icon any title *string - init func(wnd uintptr) + init func(wnd win.HWND) } -func newDialogHook(ctx context.Context, icon any, title *string, init func(wnd uintptr)) (*dialogHook, error) { +func newDialogHook(ctx context.Context, icon any, title *string, init func(wnd win.HWND)) (*dialogHook, error) { tid, _, _ := getCurrentThreadId.Call() hk, _, err := setWindowsHookEx.Call(12, // WH_CALLWNDPROCRET syscall.NewCallback(dialogHookProc), 0, tid) @@ -181,19 +179,19 @@ func dialogHookProc(code int32, wparam uintptr, lparam *_CWPRETSTRUCT) uintptr { if lparam.Message == 0x0110 { // WM_INITDIALOG tid, _, _ := getCurrentThreadId.Call() hook := (*dialogHook)(loadBackRef(tid)) - atomic.StoreUintptr(&hook.wnd, lparam.Wnd) + atomic.StoreUintptr(&hook.wnd, uintptr(lparam.Wnd)) if hook.ctx != nil && hook.ctx.Err() != nil { - sendMessage.Call(lparam.Wnd, _WM_SYSCOMMAND, _SC_CLOSE, 0) + win.SendMessage(lparam.Wnd, win.WM_SYSCOMMAND, _SC_CLOSE, 0) } else { if hook.icon != nil { icon := getIcon(hook.icon) if icon.handle != 0 { defer icon.delete() - sendMessage.Call(lparam.Wnd, _WM_SETICON, 0, icon.handle) + win.SendMessage(lparam.Wnd, win.WM_SETICON, 0, icon.handle) } } if hook.title != nil { - setWindowText.Call(lparam.Wnd, strptr(*hook.title)) + win.SetWindowText(lparam.Wnd, syscall.StringToUTF16Ptr(*hook.title)) } if hook.init != nil { hook.init(lparam.Wnd) @@ -217,7 +215,7 @@ func (h *dialogHook) wait() { select { case <-h.ctx.Done(): if wnd := atomic.LoadUintptr(&h.wnd); wnd != 0 { - sendMessage.Call(wnd, _WM_SYSCOMMAND, _SC_CLOSE, 0) + win.SendMessage(win.HWND(wnd), win.WM_SYSCOMMAND, _SC_CLOSE, 0) } case <-h.done: } @@ -462,7 +460,7 @@ type _CWPRETSTRUCT struct { LParam uintptr WParam uintptr Message uint32 - Wnd uintptr + Wnd win.HWND } // https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-nonclientmetricsw