This commit is contained in:
Nuno Cruces 2022-07-11 17:36:06 +01:00
parent dc4cafcea5
commit a3a7512489
3 changed files with 57 additions and 28 deletions

View file

@ -44,4 +44,6 @@ func GetSystemDirectory() (string, error) { return windows.GetSystemDirectory()
//sys GenerateConsoleCtrlEvent(ctrlEvent uint32, processGroupId int) (err error) = kernel32.GenerateConsoleCtrlEvent //sys GenerateConsoleCtrlEvent(ctrlEvent uint32, processGroupId int) (err error) = kernel32.GenerateConsoleCtrlEvent
//sys GetConsoleWindow() (ret HWND) = kernel32.GetConsoleWindow //sys GetConsoleWindow() (ret HWND) = kernel32.GetConsoleWindow
//sys GetModuleHandle(moduleName *uint16) (ret Handle, err error) = kernel32.GetModuleHandleW //sys GetModuleHandle(moduleName *uint16) (ret Handle, err error) = kernel32.GetModuleHandleW
//sys GlobalAlloc(flags uint32, bytes uintptr) (ret Handle, err error) = kernel32.GlobalAlloc
//sys GlobalFree(mem Handle) (err error) [failretval!=0] = kernel32.GlobalFree
//sys ReleaseActCtx(actCtx Handle) = kernel32.ReleaseActCtx //sys ReleaseActCtx(actCtx Handle) = kernel32.ReleaseActCtx

View file

@ -281,30 +281,21 @@ func SendMessagePointer(wnd HWND, msg uint32, wparam uintptr, lparam unsafe.Poin
return return
} }
func GetDpiForWindow(wnd HWND) (ret int, err error) {
if err := procGetDpiForWindow.Find(); err != nil {
return 0, err
}
return getDpiForWindow(wnd), nil
}
func SetThreadDpiAwarenessContext(dpiContext uintptr) (ret uintptr, err error) {
if err := procSetThreadDpiAwarenessContext.Find(); err != nil {
return 0, err
}
return setThreadDpiAwarenessContext(dpiContext), nil
}
// https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues // https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
func MessageLoop(wnd HWND) error { func MessageLoop(wnd HWND) error {
msg, err := GlobalAlloc(0, unsafe.Sizeof(MSG{}))
if err != nil {
return err
}
defer GlobalFree(msg)
getMessage := procGetMessageW.Addr() getMessage := procGetMessageW.Addr()
translateMessage := procTranslateMessage.Addr() translateMessage := procTranslateMessage.Addr()
dispatchMessage := procDispatchMessageW.Addr() dispatchMessage := procDispatchMessageW.Addr()
isDialogMessage := procIsDialogMessageW.Addr() isDialogMessage := procIsDialogMessageW.Addr()
for { for {
var msg MSG s, _, err := syscall.Syscall6(getMessage, 4, uintptr(msg), 0, 0, 0, 0, 0)
s, _, err := syscall.Syscall6(getMessage, 4, uintptr(unsafe.Pointer(&msg)), 0, 0, 0, 0, 0)
if int32(s) == -1 { if int32(s) == -1 {
return err return err
} }
@ -312,10 +303,10 @@ func MessageLoop(wnd HWND) error {
return nil return nil
} }
s, _, _ = syscall.Syscall(isDialogMessage, 2, uintptr(wnd), uintptr(unsafe.Pointer(&msg)), 0) s, _, _ = syscall.Syscall(isDialogMessage, 2, uintptr(wnd), uintptr(msg), 0)
if s == 0 { if s == 0 {
syscall.Syscall(translateMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0) syscall.Syscall(translateMessage, 1, uintptr(msg), 0, 0)
syscall.Syscall(dispatchMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0) syscall.Syscall(dispatchMessage, 1, uintptr(msg), 0, 0)
} }
} }
} }
@ -398,8 +389,8 @@ type CWPRETSTRUCT struct {
//sys EnableWindow(wnd HWND, enable bool) (ok bool) = user32.EnableWindow //sys EnableWindow(wnd HWND, enable bool) (ok bool) = user32.EnableWindow
//sys EnumWindows(enumFunc uintptr, lparam unsafe.Pointer) (err error) = user32.EnumChildWindows //sys EnumWindows(enumFunc uintptr, lparam unsafe.Pointer) (err error) = user32.EnumChildWindows
//sys GetDlgItem(dlg HWND, dlgItemID int) (ret HWND, err error) = user32.GetDlgItem //sys GetDlgItem(dlg HWND, dlgItemID int) (ret HWND, err error) = user32.GetDlgItem
//sys getDpiForWindow(wnd HWND) (ret int) = user32.GetDpiForWindow //sys GetDpiForWindow(wnd HWND) (ret int, err error) [false] = 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, err error) [int32(failretval)==-1] = user32.GetMessageW
//sys GetSystemMetrics(index int) (ret int) = user32.GetSystemMetrics //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
@ -415,7 +406,7 @@ type CWPRETSTRUCT struct {
//sys SetDlgItemText(dlg HWND, dlgItemID int, str *uint16) (err error) = user32.SetDlgItemTextW //sys SetDlgItemText(dlg HWND, dlgItemID int, str *uint16) (err error) = user32.SetDlgItemTextW
//sys SetFocus(wnd HWND) (ret HWND, err error) = user32.SetFocus //sys SetFocus(wnd HWND) (ret HWND, err error) = user32.SetFocus
//sys SetForegroundWindow(wnd HWND) (ok bool) = user32.SetForegroundWindow //sys SetForegroundWindow(wnd HWND) (ok bool) = user32.SetForegroundWindow
//sys setThreadDpiAwarenessContext(dpiContext uintptr) (ret uintptr) = user32.SetThreadDpiAwarenessContext //sys SetThreadDpiAwarenessContext(dpiContext uintptr) (ret uintptr, err error) [false] = user32.SetThreadDpiAwarenessContext?
//sys SetWindowLong(wnd HWND, index int, newLong int) (ret int, err error) = user32.SetWindowLongW //sys SetWindowLong(wnd HWND, index int, newLong int) (ret int, err error) = user32.SetWindowLongW
//sys SetWindowPos(wnd HWND, wndInsertAfter HWND, x int, y int, cx int, cy int, flags int) (err error) = user32.SetWindowPos //sys SetWindowPos(wnd HWND, wndInsertAfter HWND, x int, y int, cx int, cy int, flags int) (err error) = user32.SetWindowPos
//sys SetWindowsHookEx(idHook int, fn uintptr, mod Handle, threadID uint32) (ret Handle, err error) = user32.SetWindowsHookExW //sys SetWindowsHookEx(idHook int, fn uintptr, mod Handle, threadID uint32) (ret Handle, err error) = user32.SetWindowsHookExW

View file

@ -61,6 +61,8 @@ var (
procGenerateConsoleCtrlEvent = modkernel32.NewProc("GenerateConsoleCtrlEvent") procGenerateConsoleCtrlEvent = modkernel32.NewProc("GenerateConsoleCtrlEvent")
procGetConsoleWindow = modkernel32.NewProc("GetConsoleWindow") procGetConsoleWindow = modkernel32.NewProc("GetConsoleWindow")
procGetModuleHandleW = modkernel32.NewProc("GetModuleHandleW") procGetModuleHandleW = modkernel32.NewProc("GetModuleHandleW")
procGlobalAlloc = modkernel32.NewProc("GlobalAlloc")
procGlobalFree = modkernel32.NewProc("GlobalFree")
procReleaseActCtx = modkernel32.NewProc("ReleaseActCtx") procReleaseActCtx = modkernel32.NewProc("ReleaseActCtx")
procCoCreateInstance = modole32.NewProc("CoCreateInstance") procCoCreateInstance = modole32.NewProc("CoCreateInstance")
procSHBrowseForFolder = modshell32.NewProc("SHBrowseForFolder") procSHBrowseForFolder = modshell32.NewProc("SHBrowseForFolder")
@ -203,6 +205,23 @@ func GetModuleHandle(moduleName *uint16) (ret Handle, err error) {
return return
} }
func GlobalAlloc(flags uint32, bytes uintptr) (ret Handle, err error) {
r0, _, e1 := syscall.Syscall(procGlobalAlloc.Addr(), 2, uintptr(flags), uintptr(bytes), 0)
ret = Handle(r0)
if ret == 0 {
err = errnoErr(e1)
}
return
}
func GlobalFree(mem Handle) (err error) {
r1, _, e1 := syscall.Syscall(procGlobalFree.Addr(), 1, uintptr(mem), 0, 0)
if r1 != 0 {
err = errnoErr(e1)
}
return
}
func ReleaseActCtx(actCtx Handle) { func ReleaseActCtx(actCtx Handle) {
syscall.Syscall(procReleaseActCtx.Addr(), 1, uintptr(actCtx), 0, 0) syscall.Syscall(procReleaseActCtx.Addr(), 1, uintptr(actCtx), 0, 0)
return return
@ -332,15 +351,25 @@ func GetDlgItem(dlg HWND, dlgItemID int) (ret HWND, err error) {
return return
} }
func getDpiForWindow(wnd HWND) (ret int) { func GetDpiForWindow(wnd HWND) (ret int, err error) {
r0, _, _ := syscall.Syscall(procGetDpiForWindow.Addr(), 1, uintptr(wnd), 0, 0) err = procGetDpiForWindow.Find()
if err != nil {
return
}
r0, _, e1 := syscall.Syscall(procGetDpiForWindow.Addr(), 1, uintptr(wnd), 0, 0)
ret = int(r0) ret = int(r0)
if false {
err = errnoErr(e1)
}
return return
} }
func GetMessage(msg *MSG, wnd HWND, msgFilterMin uint32, msgFilterMax uint32) (ret uintptr) { func GetMessage(msg *MSG, wnd HWND, msgFilterMin uint32, msgFilterMax uint32) (ret uintptr, err error) {
r0, _, _ := syscall.Syscall6(procGetMessageW.Addr(), 4, uintptr(unsafe.Pointer(msg)), uintptr(wnd), uintptr(msgFilterMin), uintptr(msgFilterMax), 0, 0) r0, _, e1 := syscall.Syscall6(procGetMessageW.Addr(), 4, uintptr(unsafe.Pointer(msg)), uintptr(wnd), uintptr(msgFilterMin), uintptr(msgFilterMax), 0, 0)
ret = uintptr(r0) ret = uintptr(r0)
if int32(ret) == -1 {
err = errnoErr(e1)
}
return return
} }
@ -454,9 +483,16 @@ func SetForegroundWindow(wnd HWND) (ok bool) {
return return
} }
func setThreadDpiAwarenessContext(dpiContext uintptr) (ret uintptr) { func SetThreadDpiAwarenessContext(dpiContext uintptr) (ret uintptr, err error) {
r0, _, _ := syscall.Syscall(procSetThreadDpiAwarenessContext.Addr(), 1, uintptr(dpiContext), 0, 0) err = procSetThreadDpiAwarenessContext.Find()
if err != nil {
return
}
r0, _, e1 := syscall.Syscall(procSetThreadDpiAwarenessContext.Addr(), 1, uintptr(dpiContext), 0, 0)
ret = uintptr(r0) ret = uintptr(r0)
if false {
err = errnoErr(e1)
}
return return
} }