Refactor (windows).

This commit is contained in:
Nuno Cruces 2022-06-21 01:26:03 +01:00
parent 6df549b1f0
commit 2428e68504
8 changed files with 76 additions and 57 deletions

View file

@ -146,25 +146,25 @@ func (dlg *calendarDialog) layout(dpi dpi) {
} }
} }
func calendarProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr { func calendarProc(wnd win.HWND, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr {
var dlg *calendarDialog var dlg *calendarDialog
switch msg { switch msg {
case win.WM_NCCREATE: case win.WM_NCCREATE:
saveBackRef(wnd, *lparam) saveBackRef(uintptr(wnd), *lparam)
dlg = (*calendarDialog)(*lparam) dlg = (*calendarDialog)(*lparam)
case win.WM_NCDESTROY: case win.WM_NCDESTROY:
deleteBackRef(wnd) deleteBackRef(uintptr(wnd))
default: default:
dlg = (*calendarDialog)(loadBackRef(wnd)) dlg = (*calendarDialog)(loadBackRef(uintptr(wnd)))
} }
switch msg { switch msg {
case win.WM_DESTROY: case win.WM_DESTROY:
postQuitMessage.Call(0) win.PostQuitMessage(0)
case win.WM_CLOSE: case win.WM_CLOSE:
dlg.err = ErrCanceled dlg.err = ErrCanceled
destroyWindow.Call(wnd) win.DestroyWindow(wnd)
case win.WM_COMMAND: case win.WM_COMMAND:
switch wparam { switch wparam {
@ -179,14 +179,13 @@ func calendarProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointe
case win.IDNO: case win.IDNO:
dlg.err = ErrExtraButton dlg.err = ErrExtraButton
} }
destroyWindow.Call(wnd) win.DestroyWindow(wnd)
case win.WM_DPICHANGED: case win.WM_DPICHANGED:
dlg.layout(dpi(uint32(wparam) >> 16)) dlg.layout(dpi(uint32(wparam) >> 16))
default: default:
res, _, _ := defWindowProc.Call(wnd, uintptr(msg), wparam, uintptr(unsafe.Pointer(lparam))) return win.DefWindowProc(wnd, msg, wparam, unsafe.Pointer(lparam))
return res
} }
return 0 return 0

View file

@ -140,25 +140,25 @@ func (dlg *entryDialog) layout(dpi dpi) {
} }
} }
func entryProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr { func entryProc(wnd win.HWND, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr {
var dlg *entryDialog var dlg *entryDialog
switch msg { switch msg {
case win.WM_NCCREATE: case win.WM_NCCREATE:
saveBackRef(wnd, *lparam) saveBackRef(uintptr(wnd), *lparam)
dlg = (*entryDialog)(*lparam) dlg = (*entryDialog)(*lparam)
case win.WM_NCDESTROY: case win.WM_NCDESTROY:
deleteBackRef(wnd) deleteBackRef(uintptr(wnd))
default: default:
dlg = (*entryDialog)(loadBackRef(wnd)) dlg = (*entryDialog)(loadBackRef(uintptr(wnd)))
} }
switch msg { switch msg {
case win.WM_DESTROY: case win.WM_DESTROY:
postQuitMessage.Call(0) win.PostQuitMessage(0)
case win.WM_CLOSE: case win.WM_CLOSE:
dlg.err = ErrCanceled dlg.err = ErrCanceled
destroyWindow.Call(wnd) win.DestroyWindow(wnd)
case win.WM_COMMAND: case win.WM_COMMAND:
switch wparam { switch wparam {
@ -171,14 +171,13 @@ func entryProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer)
case win.IDNO: case win.IDNO:
dlg.err = ErrExtraButton dlg.err = ErrExtraButton
} }
destroyWindow.Call(wnd) win.DestroyWindow(wnd)
case win.WM_DPICHANGED: case win.WM_DPICHANGED:
dlg.layout(dpi(uint32(wparam) >> 16)) dlg.layout(dpi(uint32(wparam) >> 16))
default: default:
res, _, _ := defWindowProc.Call(wnd, uintptr(msg), wparam, uintptr(unsafe.Pointer(lparam))) return win.DefWindowProc(wnd, msg, wparam, unsafe.Pointer(lparam))
return res
} }
return 0 return 0

View file

@ -174,13 +174,16 @@ type WNDCLASSEX struct {
//sys CreateIconFromResource(resBits []byte, icon bool, ver uint32) (ret Handle, err error) = user32.CreateIconFromResource //sys CreateIconFromResource(resBits []byte, icon bool, ver uint32) (ret Handle, err error) = user32.CreateIconFromResource
//sys CreateWindowEx(exStyle uint32, className *uint16, windowName *uint16, style uint32, x int, y int, width int, height int, parent HWND, menu Handle, instance Handle, param unsafe.Pointer) (ret HWND, err error) = user32.CreateWindowExW //sys CreateWindowEx(exStyle uint32, className *uint16, windowName *uint16, style uint32, x int, y int, width int, height int, parent HWND, menu Handle, instance Handle, param unsafe.Pointer) (ret HWND, err error) = user32.CreateWindowExW
//sys DestroyIcon(icon Handle) (err error) = user32.DestroyIcon //sys DestroyIcon(icon Handle) (err error) = user32.DestroyIcon
//sys DestroyWindow(wnd HWND) (err error) = user32.DestroyWindow
//sys DispatchMessage(msg *MSG) (ret uintptr) = user32.DispatchMessageW //sys DispatchMessage(msg *MSG) (ret uintptr) = user32.DispatchMessageW
//sys DefWindowProc(wnd HWND, msg uint32, wparam uintptr, lparam unsafe.Pointer) (ret uintptr) = user32.DefWindowProcW
//sys EnableWindow(wnd HWND, enable bool) (ok bool) = user32.EnableWindow //sys EnableWindow(wnd HWND, enable bool) (ok bool) = user32.EnableWindow
//sys EnumChildWindows(parent HWND, enumFunc uintptr, lparam unsafe.Pointer) = user32.EnumChildWindows //sys EnumChildWindows(parent HWND, enumFunc uintptr, lparam unsafe.Pointer) = user32.EnumChildWindows
//sys EnumWindows(enumFunc uintptr, lparam unsafe.Pointer) (err error) = user32.EnumChildWindows //sys EnumWindows(enumFunc uintptr, lparam unsafe.Pointer) (err error) = user32.EnumChildWindows
//sys GetDlgCtrlID(wnd HWND) (ret int) = user32.GetDlgCtrlID //sys GetDlgCtrlID(wnd HWND) (ret int) = user32.GetDlgCtrlID
//sys getDpiForWindow(wnd HWND) (ret int) = user32.GetDpiForWindow //sys getDpiForWindow(wnd HWND) (ret int) = user32.GetDpiForWindow
//sys GetMessage(msg *MSG, wnd HWND, msgFilterMin uint32, msgFilterMax uint32) (ret uintptr) = user32.GetMessageW //sys GetMessage(msg *MSG, wnd HWND, msgFilterMin uint32, msgFilterMax uint32) (ret uintptr) = user32.GetMessageW
//sys GetSystemMetrics(index int) (ret int) = user32.GetSystemMetrics
//sys GetWindowDC(wnd HWND) (ret Handle) = user32.GetWindowDC //sys GetWindowDC(wnd HWND) (ret Handle) = user32.GetWindowDC
//sys GetWindowRect(wnd HWND, cmdShow *RECT) (err error) = user32.GetWindowRect //sys GetWindowRect(wnd HWND, cmdShow *RECT) (err error) = user32.GetWindowRect
//sys GetWindowText(wnd HWND, str *uint16, maxCount int) (ret int, err error) = user32.GetWindowTextW //sys GetWindowText(wnd HWND, str *uint16, maxCount int) (ret int, err error) = user32.GetWindowTextW
@ -188,6 +191,7 @@ type WNDCLASSEX struct {
//sys IsDialogMessage(wnd HWND, msg *MSG) (ok bool) = user32.IsDialogMessageW //sys IsDialogMessage(wnd HWND, msg *MSG) (ok bool) = user32.IsDialogMessageW
//sys LoadIcon(instance Handle, resource uintptr) (ret Handle, err error) = user32.LoadIconW //sys LoadIcon(instance Handle, resource uintptr) (ret Handle, err error) = user32.LoadIconW
//sys LoadImage(instance Handle, name *uint16, typ int, cx int, cy int, load int) (ret Handle, err error) = user32.LoadImageW //sys LoadImage(instance Handle, name *uint16, typ int, cx int, cy int, load int) (ret Handle, err error) = user32.LoadImageW
//sys PostQuitMessage(exitCode int) = user32.PostQuitMessage
//sys RegisterClassEx(cls *WNDCLASSEX) (err error) = user32.RegisterClassExW //sys RegisterClassEx(cls *WNDCLASSEX) (err error) = user32.RegisterClassExW
//sys ReleaseDC(wnd HWND, dc Handle) (ok bool) = user32.ReleaseDC //sys ReleaseDC(wnd HWND, dc Handle) (ok bool) = user32.ReleaseDC
//sys SendMessage(wnd HWND, msg uint32, wparam uintptr, lparam uintptr) (ret uintptr) = user32.SendMessageW //sys SendMessage(wnd HWND, msg uint32, wparam uintptr, lparam uintptr) (ret uintptr) = user32.SendMessageW

View file

@ -68,13 +68,16 @@ var (
procShell_NotifyIconW = modshell32.NewProc("Shell_NotifyIconW") procShell_NotifyIconW = modshell32.NewProc("Shell_NotifyIconW")
procCreateIconFromResource = moduser32.NewProc("CreateIconFromResource") procCreateIconFromResource = moduser32.NewProc("CreateIconFromResource")
procCreateWindowExW = moduser32.NewProc("CreateWindowExW") procCreateWindowExW = moduser32.NewProc("CreateWindowExW")
procDefWindowProcW = moduser32.NewProc("DefWindowProcW")
procDestroyIcon = moduser32.NewProc("DestroyIcon") procDestroyIcon = moduser32.NewProc("DestroyIcon")
procDestroyWindow = moduser32.NewProc("DestroyWindow")
procDispatchMessageW = moduser32.NewProc("DispatchMessageW") procDispatchMessageW = moduser32.NewProc("DispatchMessageW")
procEnableWindow = moduser32.NewProc("EnableWindow") procEnableWindow = moduser32.NewProc("EnableWindow")
procEnumChildWindows = moduser32.NewProc("EnumChildWindows") procEnumChildWindows = moduser32.NewProc("EnumChildWindows")
procGetDlgCtrlID = moduser32.NewProc("GetDlgCtrlID") procGetDlgCtrlID = moduser32.NewProc("GetDlgCtrlID")
procGetDpiForWindow = moduser32.NewProc("GetDpiForWindow") procGetDpiForWindow = moduser32.NewProc("GetDpiForWindow")
procGetMessageW = moduser32.NewProc("GetMessageW") procGetMessageW = moduser32.NewProc("GetMessageW")
procGetSystemMetrics = moduser32.NewProc("GetSystemMetrics")
procGetWindowDC = moduser32.NewProc("GetWindowDC") procGetWindowDC = moduser32.NewProc("GetWindowDC")
procGetWindowRect = moduser32.NewProc("GetWindowRect") procGetWindowRect = moduser32.NewProc("GetWindowRect")
procGetWindowTextLengthW = moduser32.NewProc("GetWindowTextLengthW") procGetWindowTextLengthW = moduser32.NewProc("GetWindowTextLengthW")
@ -82,6 +85,7 @@ var (
procIsDialogMessageW = moduser32.NewProc("IsDialogMessageW") procIsDialogMessageW = moduser32.NewProc("IsDialogMessageW")
procLoadIconW = moduser32.NewProc("LoadIconW") procLoadIconW = moduser32.NewProc("LoadIconW")
procLoadImageW = moduser32.NewProc("LoadImageW") procLoadImageW = moduser32.NewProc("LoadImageW")
procPostQuitMessage = moduser32.NewProc("PostQuitMessage")
procRegisterClassExW = moduser32.NewProc("RegisterClassExW") procRegisterClassExW = moduser32.NewProc("RegisterClassExW")
procReleaseDC = moduser32.NewProc("ReleaseDC") procReleaseDC = moduser32.NewProc("ReleaseDC")
procSendMessageW = moduser32.NewProc("SendMessageW") procSendMessageW = moduser32.NewProc("SendMessageW")
@ -253,6 +257,12 @@ func CreateWindowEx(exStyle uint32, className *uint16, windowName *uint16, style
return return
} }
func DefWindowProc(wnd HWND, msg uint32, wparam uintptr, lparam unsafe.Pointer) (ret uintptr) {
r0, _, _ := syscall.Syscall6(procDefWindowProcW.Addr(), 4, uintptr(wnd), uintptr(msg), uintptr(wparam), uintptr(lparam), 0, 0)
ret = uintptr(r0)
return
}
func DestroyIcon(icon Handle) (err error) { func DestroyIcon(icon Handle) (err error) {
r1, _, e1 := syscall.Syscall(procDestroyIcon.Addr(), 1, uintptr(icon), 0, 0) r1, _, e1 := syscall.Syscall(procDestroyIcon.Addr(), 1, uintptr(icon), 0, 0)
if r1 == 0 { if r1 == 0 {
@ -261,6 +271,14 @@ func DestroyIcon(icon Handle) (err error) {
return return
} }
func DestroyWindow(wnd HWND) (err error) {
r1, _, e1 := syscall.Syscall(procDestroyWindow.Addr(), 1, uintptr(wnd), 0, 0)
if r1 == 0 {
err = errnoErr(e1)
}
return
}
func DispatchMessage(msg *MSG) (ret uintptr) { func DispatchMessage(msg *MSG) (ret uintptr) {
r0, _, _ := syscall.Syscall(procDispatchMessageW.Addr(), 1, uintptr(unsafe.Pointer(msg)), 0, 0) r0, _, _ := syscall.Syscall(procDispatchMessageW.Addr(), 1, uintptr(unsafe.Pointer(msg)), 0, 0)
ret = uintptr(r0) ret = uintptr(r0)
@ -308,6 +326,12 @@ func GetMessage(msg *MSG, wnd HWND, msgFilterMin uint32, msgFilterMax uint32) (r
return return
} }
func GetSystemMetrics(index int) (ret int) {
r0, _, _ := syscall.Syscall(procGetSystemMetrics.Addr(), 1, uintptr(index), 0, 0)
ret = int(r0)
return
}
func GetWindowDC(wnd HWND) (ret Handle) { func GetWindowDC(wnd HWND) (ret Handle) {
r0, _, _ := syscall.Syscall(procGetWindowDC.Addr(), 1, uintptr(wnd), 0, 0) r0, _, _ := syscall.Syscall(procGetWindowDC.Addr(), 1, uintptr(wnd), 0, 0)
ret = Handle(r0) ret = Handle(r0)
@ -364,6 +388,11 @@ func LoadImage(instance Handle, name *uint16, typ int, cx int, cy int, load int)
return return
} }
func PostQuitMessage(exitCode int) {
syscall.Syscall(procPostQuitMessage.Addr(), 1, uintptr(exitCode), 0, 0)
return
}
func RegisterClassEx(cls *WNDCLASSEX) (err error) { func RegisterClassEx(cls *WNDCLASSEX) (err error) {
r1, _, e1 := syscall.Syscall(procRegisterClassExW.Addr(), 1, uintptr(unsafe.Pointer(cls)), 0, 0) r1, _, e1 := syscall.Syscall(procRegisterClassExW.Addr(), 1, uintptr(unsafe.Pointer(cls)), 0, 0)
if r1 == 0 { if r1 == 0 {

View file

@ -160,25 +160,25 @@ func (dlg *listDialog) layout(dpi dpi) {
} }
} }
func listProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr { func listProc(wnd win.HWND, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr {
var dlg *listDialog var dlg *listDialog
switch msg { switch msg {
case win.WM_NCCREATE: case win.WM_NCCREATE:
saveBackRef(wnd, *lparam) saveBackRef(uintptr(wnd), *lparam)
dlg = (*listDialog)(*lparam) dlg = (*listDialog)(*lparam)
case win.WM_NCDESTROY: case win.WM_NCDESTROY:
deleteBackRef(wnd) deleteBackRef(uintptr(wnd))
default: default:
dlg = (*listDialog)(loadBackRef(wnd)) dlg = (*listDialog)(loadBackRef(uintptr(wnd)))
} }
switch msg { switch msg {
case win.WM_DESTROY: case win.WM_DESTROY:
postQuitMessage.Call(0) win.PostQuitMessage(0)
case win.WM_CLOSE: case win.WM_CLOSE:
dlg.err = ErrCanceled dlg.err = ErrCanceled
destroyWindow.Call(wnd) win.DestroyWindow(wnd)
case win.WM_COMMAND: case win.WM_COMMAND:
switch wparam { switch wparam {
@ -208,14 +208,13 @@ func listProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) u
case win.IDNO: case win.IDNO:
dlg.err = ErrExtraButton dlg.err = ErrExtraButton
} }
destroyWindow.Call(wnd) win.DestroyWindow(wnd)
case win.WM_DPICHANGED: case win.WM_DPICHANGED:
dlg.layout(dpi(uint32(wparam) >> 16)) dlg.layout(dpi(uint32(wparam) >> 16))
default: default:
res, _, _ := defWindowProc.Call(wnd, uintptr(msg), wparam, uintptr(unsafe.Pointer(lparam))) return win.DefWindowProc(wnd, msg, wparam, unsafe.Pointer(lparam))
return res
} }
return 0 return 0

View file

@ -234,25 +234,25 @@ func (d *progressDialog) layout(dpi dpi) {
} }
} }
func progressProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr { func progressProc(wnd win.HWND, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr {
var dlg *progressDialog var dlg *progressDialog
switch msg { switch msg {
case win.WM_NCCREATE: case win.WM_NCCREATE:
saveBackRef(wnd, *lparam) saveBackRef(uintptr(wnd), *lparam)
dlg = (*progressDialog)(*lparam) dlg = (*progressDialog)(*lparam)
case win.WM_NCDESTROY: case win.WM_NCDESTROY:
deleteBackRef(wnd) deleteBackRef(uintptr(wnd))
default: default:
dlg = (*progressDialog)(loadBackRef(wnd)) dlg = (*progressDialog)(loadBackRef(uintptr(wnd)))
} }
switch msg { switch msg {
case win.WM_DESTROY: case win.WM_DESTROY:
postQuitMessage.Call(0) win.PostQuitMessage(0)
case win.WM_CLOSE: case win.WM_CLOSE:
dlg.err = ErrCanceled dlg.err = ErrCanceled
destroyWindow.Call(wnd) win.DestroyWindow(wnd)
case win.WM_COMMAND: case win.WM_COMMAND:
switch wparam { switch wparam {
@ -265,14 +265,13 @@ func progressProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointe
case win.IDNO: case win.IDNO:
dlg.err = ErrExtraButton dlg.err = ErrExtraButton
} }
destroyWindow.Call(wnd) win.DestroyWindow(wnd)
case win.WM_DPICHANGED: case win.WM_DPICHANGED:
dlg.layout(dpi(uint32(wparam) >> 16)) dlg.layout(dpi(uint32(wparam) >> 16))
default: default:
res, _, _ := defWindowProc.Call(wnd, uintptr(msg), wparam, uintptr(unsafe.Pointer(lparam))) return win.DefWindowProc(wnd, msg, wparam, unsafe.Pointer(lparam))
return res
} }
return 0 return 0

View file

@ -161,25 +161,25 @@ func (dlg *passwordDialog) layout(dpi dpi) {
} }
} }
func passwordProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr { func passwordProc(wnd win.HWND, msg uint32, wparam uintptr, lparam *unsafe.Pointer) uintptr {
var dlg *passwordDialog var dlg *passwordDialog
switch msg { switch msg {
case win.WM_NCCREATE: case win.WM_NCCREATE:
saveBackRef(wnd, *lparam) saveBackRef(uintptr(wnd), *lparam)
dlg = (*passwordDialog)(*lparam) dlg = (*passwordDialog)(*lparam)
case win.WM_NCDESTROY: case win.WM_NCDESTROY:
deleteBackRef(wnd) deleteBackRef(uintptr(wnd))
default: default:
dlg = (*passwordDialog)(loadBackRef(wnd)) dlg = (*passwordDialog)(loadBackRef(uintptr(wnd)))
} }
switch msg { switch msg {
case win.WM_DESTROY: case win.WM_DESTROY:
postQuitMessage.Call(0) win.PostQuitMessage(0)
case win.WM_CLOSE: case win.WM_CLOSE:
dlg.err = ErrCanceled dlg.err = ErrCanceled
destroyWindow.Call(wnd) win.DestroyWindow(wnd)
case win.WM_COMMAND: case win.WM_COMMAND:
switch wparam { switch wparam {
@ -193,14 +193,13 @@ func passwordProc(wnd uintptr, msg uint32, wparam uintptr, lparam *unsafe.Pointe
case win.IDNO: case win.IDNO:
dlg.err = ErrExtraButton dlg.err = ErrExtraButton
} }
destroyWindow.Call(wnd) win.DestroyWindow(wnd)
case win.WM_DPICHANGED: case win.WM_DPICHANGED:
dlg.layout(dpi(uint32(wparam) >> 16)) dlg.layout(dpi(uint32(wparam) >> 16))
default: default:
res, _, _ := defWindowProc.Call(wnd, uintptr(msg), wparam, uintptr(unsafe.Pointer(lparam))) return win.DefWindowProc(wnd, msg, wparam, unsafe.Pointer(lparam))
return res
} }
return 0 return 0

View file

@ -20,10 +20,6 @@ var (
user32 = windows.NewLazySystemDLL("user32.dll") user32 = windows.NewLazySystemDLL("user32.dll")
callNextHookEx = user32.NewProc("CallNextHookEx") callNextHookEx = user32.NewProc("CallNextHookEx")
defWindowProc = user32.NewProc("DefWindowProcW")
destroyWindow = user32.NewProc("DestroyWindow")
getSystemMetrics = user32.NewProc("GetSystemMetrics")
postQuitMessage = user32.NewProc("PostQuitMessage")
setWindowsHookEx = user32.NewProc("SetWindowsHookExW") setWindowsHookEx = user32.NewProc("SetWindowsHookExW")
systemParametersInfo = user32.NewProc("SystemParametersInfoW") systemParametersInfo = user32.NewProc("SystemParametersInfoW")
unhookWindowsHookEx = user32.NewProc("UnhookWindowsHookEx") unhookWindowsHookEx = user32.NewProc("UnhookWindowsHookEx")
@ -312,16 +308,11 @@ func (i *icon) delete() {
} }
func centerWindow(wnd win.HWND) { func centerWindow(wnd win.HWND) {
getMetric := func(i uintptr) int32 {
n, _, _ := getSystemMetrics.Call(i)
return int32(n)
}
var rect win.RECT var rect win.RECT
win.GetWindowRect(wnd, &rect) win.GetWindowRect(wnd, &rect)
x := (getMetric(0 /* SM_CXSCREEN */) - (rect.Right - rect.Left)) / 2 x := (win.GetSystemMetrics(0 /* SM_CXSCREEN */) - int(rect.Right-rect.Left)) / 2
y := (getMetric(1 /* SM_CYSCREEN */) - (rect.Bottom - rect.Top)) / 2 y := (win.GetSystemMetrics(1 /* SM_CYSCREEN */) - int(rect.Bottom-rect.Top)) / 2
win.SetWindowPos(wnd, 0, int(x), int(y), 0, 0, 0x5) // SWP_NOZORDER|SWP_NOSIZE win.SetWindowPos(wnd, 0, x, y, 0, 0, 0x5) // SWP_NOZORDER|SWP_NOSIZE
} }
func getWindowString(wnd win.HWND) string { func getWindowString(wnd win.HWND) string {