github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/windows/winc/w32/user32.go (about) 1 //go:build windows 2 3 /* 4 * Copyright (C) 2019 Tad Vizbaras. All Rights Reserved. 5 * Copyright (C) 2010-2012 The W32 Authors. All Rights Reserved. 6 */ 7 8 package w32 9 10 import ( 11 "fmt" 12 "runtime" 13 "syscall" 14 "unsafe" 15 ) 16 17 var ( 18 moduser32 = syscall.NewLazyDLL("user32.dll") 19 20 procRegisterClassEx = moduser32.NewProc("RegisterClassExW") 21 procLoadIcon = moduser32.NewProc("LoadIconW") 22 procLoadCursor = moduser32.NewProc("LoadCursorW") 23 procShowWindow = moduser32.NewProc("ShowWindow") 24 procShowWindowAsync = moduser32.NewProc("ShowWindowAsync") 25 procUpdateWindow = moduser32.NewProc("UpdateWindow") 26 procCreateWindowEx = moduser32.NewProc("CreateWindowExW") 27 procFindWindowW = moduser32.NewProc("FindWindowW") 28 procAdjustWindowRect = moduser32.NewProc("AdjustWindowRect") 29 procAdjustWindowRectEx = moduser32.NewProc("AdjustWindowRectEx") 30 procDestroyWindow = moduser32.NewProc("DestroyWindow") 31 procDefWindowProc = moduser32.NewProc("DefWindowProcW") 32 procDefDlgProc = moduser32.NewProc("DefDlgProcW") 33 procPostQuitMessage = moduser32.NewProc("PostQuitMessage") 34 procGetMessage = moduser32.NewProc("GetMessageW") 35 procTranslateMessage = moduser32.NewProc("TranslateMessage") 36 procDispatchMessage = moduser32.NewProc("DispatchMessageW") 37 procSendMessage = moduser32.NewProc("SendMessageW") 38 procPostMessage = moduser32.NewProc("PostMessageW") 39 procWaitMessage = moduser32.NewProc("WaitMessage") 40 procSetWindowText = moduser32.NewProc("SetWindowTextW") 41 procGetWindowTextLength = moduser32.NewProc("GetWindowTextLengthW") 42 procGetWindowText = moduser32.NewProc("GetWindowTextW") 43 procGetWindowRect = moduser32.NewProc("GetWindowRect") 44 procGetWindowInfo = moduser32.NewProc("GetWindowInfo") 45 procSetWindowCompositionAttribute = moduser32.NewProc("SetWindowCompositionAttribute") 46 procMoveWindow = moduser32.NewProc("MoveWindow") 47 procScreenToClient = moduser32.NewProc("ScreenToClient") 48 procCallWindowProc = moduser32.NewProc("CallWindowProcW") 49 procSetWindowLong = moduser32.NewProc("SetWindowLongW") 50 procSetWindowLongPtr = moduser32.NewProc("SetWindowLongW") 51 procGetWindowLong = moduser32.NewProc("GetWindowLongW") 52 procGetWindowLongPtr = moduser32.NewProc("GetWindowLongW") 53 procEnableWindow = moduser32.NewProc("EnableWindow") 54 procIsWindowEnabled = moduser32.NewProc("IsWindowEnabled") 55 procIsWindowVisible = moduser32.NewProc("IsWindowVisible") 56 procSetFocus = moduser32.NewProc("SetFocus") 57 procGetFocus = moduser32.NewProc("GetFocus") 58 procSetActiveWindow = moduser32.NewProc("SetActiveWindow") 59 procSetForegroundWindow = moduser32.NewProc("SetForegroundWindow") 60 procBringWindowToTop = moduser32.NewProc("BringWindowToTop") 61 procInvalidateRect = moduser32.NewProc("InvalidateRect") 62 procGetClientRect = moduser32.NewProc("GetClientRect") 63 procGetDC = moduser32.NewProc("GetDC") 64 procReleaseDC = moduser32.NewProc("ReleaseDC") 65 procSetCapture = moduser32.NewProc("SetCapture") 66 procReleaseCapture = moduser32.NewProc("ReleaseCapture") 67 procGetWindowThreadProcessId = moduser32.NewProc("GetWindowThreadProcessId") 68 procMessageBox = moduser32.NewProc("MessageBoxW") 69 procGetSystemMetrics = moduser32.NewProc("GetSystemMetrics") 70 procPostThreadMessageW = moduser32.NewProc("PostThreadMessageW") 71 procRegisterWindowMessageA = moduser32.NewProc("RegisterWindowMessageA") 72 //procSysColorBrush = moduser32.NewProc("GetSysColorBrush") 73 procCopyRect = moduser32.NewProc("CopyRect") 74 procEqualRect = moduser32.NewProc("EqualRect") 75 procInflateRect = moduser32.NewProc("InflateRect") 76 procIntersectRect = moduser32.NewProc("IntersectRect") 77 procIsRectEmpty = moduser32.NewProc("IsRectEmpty") 78 procOffsetRect = moduser32.NewProc("OffsetRect") 79 procPtInRect = moduser32.NewProc("PtInRect") 80 procSetRect = moduser32.NewProc("SetRect") 81 procSetRectEmpty = moduser32.NewProc("SetRectEmpty") 82 procSubtractRect = moduser32.NewProc("SubtractRect") 83 procUnionRect = moduser32.NewProc("UnionRect") 84 procCreateDialogParam = moduser32.NewProc("CreateDialogParamW") 85 procDialogBoxParam = moduser32.NewProc("DialogBoxParamW") 86 procGetDlgItem = moduser32.NewProc("GetDlgItem") 87 procDrawIcon = moduser32.NewProc("DrawIcon") 88 procCreateMenu = moduser32.NewProc("CreateMenu") 89 //procSetMenu = moduser32.NewProc("SetMenu") 90 procDestroyMenu = moduser32.NewProc("DestroyMenu") 91 procCreatePopupMenu = moduser32.NewProc("CreatePopupMenu") 92 procCheckMenuRadioItem = moduser32.NewProc("CheckMenuRadioItem") 93 //procDrawMenuBar = moduser32.NewProc("DrawMenuBar") 94 //procInsertMenuItem = moduser32.NewProc("InsertMenuItemW") // FIXIT: 95 96 procClientToScreen = moduser32.NewProc("ClientToScreen") 97 procIsDialogMessage = moduser32.NewProc("IsDialogMessageW") 98 procIsWindow = moduser32.NewProc("IsWindow") 99 procEndDialog = moduser32.NewProc("EndDialog") 100 procPeekMessage = moduser32.NewProc("PeekMessageW") 101 procTranslateAccelerator = moduser32.NewProc("TranslateAcceleratorW") 102 procSetWindowPos = moduser32.NewProc("SetWindowPos") 103 procFillRect = moduser32.NewProc("FillRect") 104 procDrawText = moduser32.NewProc("DrawTextW") 105 procAddClipboardFormatListener = moduser32.NewProc("AddClipboardFormatListener") 106 procRemoveClipboardFormatListener = moduser32.NewProc("RemoveClipboardFormatListener") 107 procOpenClipboard = moduser32.NewProc("OpenClipboard") 108 procCloseClipboard = moduser32.NewProc("CloseClipboard") 109 procEnumClipboardFormats = moduser32.NewProc("EnumClipboardFormats") 110 procGetClipboardData = moduser32.NewProc("GetClipboardData") 111 procSetClipboardData = moduser32.NewProc("SetClipboardData") 112 procEmptyClipboard = moduser32.NewProc("EmptyClipboard") 113 procGetClipboardFormatName = moduser32.NewProc("GetClipboardFormatNameW") 114 procIsClipboardFormatAvailable = moduser32.NewProc("IsClipboardFormatAvailable") 115 procBeginPaint = moduser32.NewProc("BeginPaint") 116 procEndPaint = moduser32.NewProc("EndPaint") 117 procGetKeyboardState = moduser32.NewProc("GetKeyboardState") 118 procMapVirtualKey = moduser32.NewProc("MapVirtualKeyExW") 119 procGetAsyncKeyState = moduser32.NewProc("GetAsyncKeyState") 120 procToAscii = moduser32.NewProc("ToAscii") 121 procSwapMouseButton = moduser32.NewProc("SwapMouseButton") 122 procGetCursorPos = moduser32.NewProc("GetCursorPos") 123 procSetCursorPos = moduser32.NewProc("SetCursorPos") 124 procSetCursor = moduser32.NewProc("SetCursor") 125 procCreateIcon = moduser32.NewProc("CreateIcon") 126 procDestroyIcon = moduser32.NewProc("DestroyIcon") 127 procMonitorFromPoint = moduser32.NewProc("MonitorFromPoint") 128 procMonitorFromRect = moduser32.NewProc("MonitorFromRect") 129 procMonitorFromWindow = moduser32.NewProc("MonitorFromWindow") 130 procGetMonitorInfo = moduser32.NewProc("GetMonitorInfoW") 131 procGetDpiForSystem = moduser32.NewProc("GetDpiForSystem") 132 procGetDpiForWindow = moduser32.NewProc("GetDpiForWindow") 133 procEnumDisplayMonitors = moduser32.NewProc("EnumDisplayMonitors") 134 procEnumDisplaySettingsEx = moduser32.NewProc("EnumDisplaySettingsExW") 135 procChangeDisplaySettingsEx = moduser32.NewProc("ChangeDisplaySettingsExW") 136 procSendInput = moduser32.NewProc("SendInput") 137 procSetWindowsHookEx = moduser32.NewProc("SetWindowsHookExW") 138 procUnhookWindowsHookEx = moduser32.NewProc("UnhookWindowsHookEx") 139 procCallNextHookEx = moduser32.NewProc("CallNextHookEx") 140 141 libuser32, _ = syscall.LoadLibrary("user32.dll") 142 insertMenuItem, _ = syscall.GetProcAddress(libuser32, "InsertMenuItemW") 143 setMenuItemInfo, _ = syscall.GetProcAddress(libuser32, "SetMenuItemInfoW") 144 setMenu, _ = syscall.GetProcAddress(libuser32, "SetMenu") 145 drawMenuBar, _ = syscall.GetProcAddress(libuser32, "DrawMenuBar") 146 trackPopupMenuEx, _ = syscall.GetProcAddress(libuser32, "TrackPopupMenuEx") 147 getKeyState, _ = syscall.GetProcAddress(libuser32, "GetKeyState") 148 getSysColorBrush, _ = syscall.GetProcAddress(libuser32, "GetSysColorBrush") 149 150 getWindowPlacement, _ = syscall.GetProcAddress(libuser32, "GetWindowPlacement") 151 setWindowPlacement, _ = syscall.GetProcAddress(libuser32, "SetWindowPlacement") 152 153 setScrollInfo, _ = syscall.GetProcAddress(libuser32, "SetScrollInfo") 154 getScrollInfo, _ = syscall.GetProcAddress(libuser32, "GetScrollInfo") 155 156 mainThread HANDLE 157 ) 158 159 func init() { 160 runtime.LockOSThread() 161 mainThread = GetCurrentThreadId() 162 } 163 164 func GET_X_LPARAM(lp uintptr) int32 { 165 return int32(int16(LOWORD(uint32(lp)))) 166 } 167 168 func GET_Y_LPARAM(lp uintptr) int32 { 169 return int32(int16(HIWORD(uint32(lp)))) 170 } 171 172 func RegisterClassEx(wndClassEx *WNDCLASSEX) ATOM { 173 ret, _, _ := procRegisterClassEx.Call(uintptr(unsafe.Pointer(wndClassEx))) 174 return ATOM(ret) 175 } 176 177 func LoadIcon(instance HINSTANCE, iconName *uint16) HICON { 178 ret, _, _ := procLoadIcon.Call( 179 uintptr(instance), 180 uintptr(unsafe.Pointer(iconName))) 181 182 return HICON(ret) 183 } 184 185 func LoadIconWithResourceID(instance HINSTANCE, res uint16) HICON { 186 ret, _, _ := procLoadIcon.Call( 187 uintptr(instance), 188 uintptr(res)) 189 190 return HICON(ret) 191 } 192 193 func LoadCursor(instance HINSTANCE, cursorName *uint16) HCURSOR { 194 ret, _, _ := procLoadCursor.Call( 195 uintptr(instance), 196 uintptr(unsafe.Pointer(cursorName))) 197 198 return HCURSOR(ret) 199 } 200 201 func LoadCursorWithResourceID(instance HINSTANCE, res uint16) HCURSOR { 202 ret, _, _ := procLoadCursor.Call( 203 uintptr(instance), 204 uintptr(res)) 205 206 return HCURSOR(ret) 207 } 208 209 func ShowWindow(hwnd HWND, cmdshow int) bool { 210 ret, _, _ := procShowWindow.Call( 211 uintptr(hwnd), 212 uintptr(cmdshow)) 213 214 return ret != 0 215 } 216 217 func ShowWindowAsync(hwnd HWND, cmdshow int) bool { 218 ret, _, _ := procShowWindowAsync.Call( 219 uintptr(hwnd), 220 uintptr(cmdshow)) 221 222 return ret != 0 223 } 224 225 func UpdateWindow(hwnd HWND) bool { 226 ret, _, _ := procUpdateWindow.Call( 227 uintptr(hwnd)) 228 return ret != 0 229 } 230 231 func PostThreadMessage(threadID HANDLE, msg int, wp, lp uintptr) { 232 procPostThreadMessageW.Call(threadID, uintptr(msg), wp, lp) 233 } 234 235 func RegisterWindowMessage(name *uint16) uint32 { 236 ret, _, _ := procRegisterWindowMessageA.Call( 237 uintptr(unsafe.Pointer(name))) 238 239 return uint32(ret) 240 } 241 242 func PostMainThreadMessage(msg uint32, wp, lp uintptr) bool { 243 ret, _, _ := procPostThreadMessageW.Call(mainThread, uintptr(msg), wp, lp) 244 return ret != 0 245 } 246 247 func CreateWindowEx(exStyle uint, className, windowName *uint16, 248 style uint, x, y, width, height int, parent HWND, menu HMENU, 249 instance HINSTANCE, param unsafe.Pointer) HWND { 250 ret, _, _ := procCreateWindowEx.Call( 251 uintptr(exStyle), 252 uintptr(unsafe.Pointer(className)), 253 uintptr(unsafe.Pointer(windowName)), 254 uintptr(style), 255 uintptr(x), 256 uintptr(y), 257 uintptr(width), 258 uintptr(height), 259 uintptr(parent), 260 uintptr(menu), 261 uintptr(instance), 262 uintptr(param)) 263 264 return HWND(ret) 265 } 266 267 func FindWindowW(className, windowName *uint16) HWND { 268 ret, _, _ := procFindWindowW.Call( 269 uintptr(unsafe.Pointer(className)), 270 uintptr(unsafe.Pointer(windowName))) 271 272 return HWND(ret) 273 } 274 275 func AdjustWindowRectEx(rect *RECT, style uint, menu bool, exStyle uint) bool { 276 ret, _, _ := procAdjustWindowRectEx.Call( 277 uintptr(unsafe.Pointer(rect)), 278 uintptr(style), 279 uintptr(BoolToBOOL(menu)), 280 uintptr(exStyle)) 281 282 return ret != 0 283 } 284 285 func AdjustWindowRect(rect *RECT, style uint, menu bool) bool { 286 ret, _, _ := procAdjustWindowRect.Call( 287 uintptr(unsafe.Pointer(rect)), 288 uintptr(style), 289 uintptr(BoolToBOOL(menu))) 290 291 return ret != 0 292 } 293 294 func DestroyWindow(hwnd HWND) bool { 295 ret, _, _ := procDestroyWindow.Call(hwnd) 296 return ret != 0 297 } 298 299 func HasGetDpiForWindowFunc() bool { 300 err := procGetDpiForWindow.Find() 301 return err == nil 302 } 303 304 func GetDpiForWindow(hwnd HWND) UINT { 305 dpi, _, _ := procGetDpiForWindow.Call(hwnd) 306 return uint(dpi) 307 } 308 309 func SetWindowCompositionAttribute(hwnd HWND, data *WINDOWCOMPOSITIONATTRIBDATA) bool { 310 if procSetWindowCompositionAttribute != nil { 311 ret, _, _ := procSetWindowCompositionAttribute.Call( 312 hwnd, 313 uintptr(unsafe.Pointer(data)), 314 ) 315 return ret != 0 316 } 317 return false 318 } 319 320 func DefWindowProc(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr { 321 ret, _, _ := procDefWindowProc.Call( 322 uintptr(hwnd), 323 uintptr(msg), 324 wParam, 325 lParam) 326 327 return ret 328 } 329 330 func DefDlgProc(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr { 331 ret, _, _ := procDefDlgProc.Call( 332 uintptr(hwnd), 333 uintptr(msg), 334 wParam, 335 lParam) 336 337 return ret 338 } 339 340 func PostQuitMessage(exitCode int) { 341 procPostQuitMessage.Call( 342 uintptr(exitCode)) 343 } 344 345 func GetMessage(msg *MSG, hwnd HWND, msgFilterMin, msgFilterMax uint32) int { 346 ret, _, _ := procGetMessage.Call( 347 uintptr(unsafe.Pointer(msg)), 348 uintptr(hwnd), 349 uintptr(msgFilterMin), 350 uintptr(msgFilterMax)) 351 352 return int(ret) 353 } 354 355 func TranslateMessage(msg *MSG) bool { 356 ret, _, _ := procTranslateMessage.Call( 357 uintptr(unsafe.Pointer(msg))) 358 359 return ret != 0 360 361 } 362 363 func DispatchMessage(msg *MSG) uintptr { 364 ret, _, _ := procDispatchMessage.Call( 365 uintptr(unsafe.Pointer(msg))) 366 367 return ret 368 369 } 370 371 func SendMessage(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr { 372 ret, _, _ := procSendMessage.Call( 373 uintptr(hwnd), 374 uintptr(msg), 375 wParam, 376 lParam) 377 378 return ret 379 } 380 381 func PostMessage(hwnd HWND, msg uint32, wParam, lParam uintptr) bool { 382 ret, _, _ := procPostMessage.Call( 383 uintptr(hwnd), 384 uintptr(msg), 385 wParam, 386 lParam) 387 388 return ret != 0 389 } 390 391 func WaitMessage() bool { 392 ret, _, _ := procWaitMessage.Call() 393 return ret != 0 394 } 395 396 func SetWindowText(hwnd HWND, text string) { 397 procSetWindowText.Call( 398 uintptr(hwnd), 399 uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text)))) 400 } 401 402 func GetWindowTextLength(hwnd HWND) int { 403 ret, _, _ := procGetWindowTextLength.Call( 404 uintptr(hwnd)) 405 406 return int(ret) 407 } 408 409 func GetWindowInfo(hwnd HWND, info *WINDOWINFO) int { 410 ret, _, _ := procGetWindowInfo.Call( 411 hwnd, 412 uintptr(unsafe.Pointer(info)), 413 ) 414 return int(ret) 415 } 416 417 func GetWindowText(hwnd HWND) string { 418 textLen := GetWindowTextLength(hwnd) + 1 419 420 buf := make([]uint16, textLen) 421 procGetWindowText.Call( 422 uintptr(hwnd), 423 uintptr(unsafe.Pointer(&buf[0])), 424 uintptr(textLen)) 425 426 return syscall.UTF16ToString(buf) 427 } 428 429 func GetWindowRect(hwnd HWND) *RECT { 430 var rect RECT 431 procGetWindowRect.Call( 432 hwnd, 433 uintptr(unsafe.Pointer(&rect))) 434 435 return &rect 436 } 437 438 func MoveWindow(hwnd HWND, x, y, width, height int, repaint bool) bool { 439 ret, _, _ := procMoveWindow.Call( 440 uintptr(hwnd), 441 uintptr(x), 442 uintptr(y), 443 uintptr(width), 444 uintptr(height), 445 uintptr(BoolToBOOL(repaint))) 446 447 return ret != 0 448 449 } 450 451 func ScreenToClient(hwnd HWND, x, y int) (X, Y int, ok bool) { 452 pt := POINT{X: int32(x), Y: int32(y)} 453 ret, _, _ := procScreenToClient.Call( 454 uintptr(hwnd), 455 uintptr(unsafe.Pointer(&pt))) 456 457 return int(pt.X), int(pt.Y), ret != 0 458 } 459 460 func CallWindowProc(preWndProc uintptr, hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr { 461 ret, _, _ := procCallWindowProc.Call( 462 preWndProc, 463 uintptr(hwnd), 464 uintptr(msg), 465 wParam, 466 lParam) 467 468 return ret 469 } 470 471 func SetWindowLong(hwnd HWND, index int, value uint32) uint32 { 472 ret, _, _ := procSetWindowLong.Call( 473 uintptr(hwnd), 474 uintptr(index), 475 uintptr(value)) 476 477 return uint32(ret) 478 } 479 480 func SetWindowLongPtr(hwnd HWND, index int, value uintptr) uintptr { 481 ret, _, _ := procSetWindowLongPtr.Call( 482 uintptr(hwnd), 483 uintptr(index), 484 value) 485 486 return ret 487 } 488 489 func GetWindowLong(hwnd HWND, index int) int32 { 490 ret, _, _ := procGetWindowLong.Call( 491 uintptr(hwnd), 492 uintptr(index)) 493 494 return int32(ret) 495 } 496 497 func GetWindowLongPtr(hwnd HWND, index int) uintptr { 498 ret, _, _ := procGetWindowLongPtr.Call( 499 uintptr(hwnd), 500 uintptr(index)) 501 502 return ret 503 } 504 505 func EnableWindow(hwnd HWND, b bool) bool { 506 ret, _, _ := procEnableWindow.Call( 507 uintptr(hwnd), 508 uintptr(BoolToBOOL(b))) 509 return ret != 0 510 } 511 512 func IsWindowEnabled(hwnd HWND) bool { 513 ret, _, _ := procIsWindowEnabled.Call( 514 uintptr(hwnd)) 515 516 return ret != 0 517 } 518 519 func IsWindowVisible(hwnd HWND) bool { 520 ret, _, _ := procIsWindowVisible.Call( 521 uintptr(hwnd)) 522 523 return ret != 0 524 } 525 526 func SetFocus(hwnd HWND) HWND { 527 ret, _, _ := procSetFocus.Call( 528 uintptr(hwnd)) 529 530 return HWND(ret) 531 } 532 533 func SetActiveWindow(hwnd HWND) HWND { 534 ret, _, _ := procSetActiveWindow.Call( 535 uintptr(hwnd)) 536 537 return HWND(ret) 538 } 539 540 func BringWindowToTop(hwnd HWND) bool { 541 ret, _, _ := procBringWindowToTop.Call(uintptr(hwnd)) 542 return ret != 0 543 } 544 545 func SetForegroundWindow(hwnd HWND) HWND { 546 ret, _, _ := procSetForegroundWindow.Call( 547 uintptr(hwnd)) 548 549 return HWND(ret) 550 } 551 552 func GetFocus() HWND { 553 ret, _, _ := procGetFocus.Call() 554 return HWND(ret) 555 } 556 557 func InvalidateRect(hwnd HWND, rect *RECT, erase bool) bool { 558 ret, _, _ := procInvalidateRect.Call( 559 uintptr(hwnd), 560 uintptr(unsafe.Pointer(rect)), 561 uintptr(BoolToBOOL(erase))) 562 563 return ret != 0 564 } 565 566 func GetClientRect(hwnd HWND) *RECT { 567 var rect RECT 568 ret, _, _ := procGetClientRect.Call( 569 uintptr(hwnd), 570 uintptr(unsafe.Pointer(&rect))) 571 572 if ret == 0 { 573 panic(fmt.Sprintf("GetClientRect(%d) failed", hwnd)) 574 } 575 576 return &rect 577 } 578 579 func GetDC(hwnd HWND) HDC { 580 ret, _, _ := procGetDC.Call( 581 uintptr(hwnd)) 582 583 return HDC(ret) 584 } 585 586 func ReleaseDC(hwnd HWND, hDC HDC) bool { 587 ret, _, _ := procReleaseDC.Call( 588 uintptr(hwnd), 589 uintptr(hDC)) 590 591 return ret != 0 592 } 593 594 func SetCapture(hwnd HWND) HWND { 595 ret, _, _ := procSetCapture.Call( 596 uintptr(hwnd)) 597 598 return HWND(ret) 599 } 600 601 func ReleaseCapture() bool { 602 ret, _, _ := procReleaseCapture.Call() 603 604 return ret != 0 605 } 606 607 func GetWindowThreadProcessId(hwnd HWND) (HANDLE, int) { 608 var processId int 609 ret, _, _ := procGetWindowThreadProcessId.Call( 610 uintptr(hwnd), 611 uintptr(unsafe.Pointer(&processId))) 612 613 return HANDLE(ret), processId 614 } 615 616 func MessageBox(hwnd HWND, title, caption string, flags uint) int { 617 ret, _, _ := procMessageBox.Call( 618 uintptr(hwnd), 619 uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))), 620 uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))), 621 uintptr(flags)) 622 623 return int(ret) 624 } 625 626 func GetSystemMetrics(index int) int { 627 ret, _, _ := procGetSystemMetrics.Call( 628 uintptr(index)) 629 630 return int(ret) 631 } 632 633 func GetSysColorBrush(nIndex int) HBRUSH { 634 /* 635 ret, _, _ := procSysColorBrush.Call(1, 636 uintptr(nIndex), 637 0, 638 0) 639 640 return HBRUSH(ret) 641 */ 642 ret, _, _ := syscall.Syscall(getSysColorBrush, 1, 643 uintptr(nIndex), 644 0, 645 0) 646 647 return HBRUSH(ret) 648 } 649 650 func CopyRect(dst, src *RECT) bool { 651 ret, _, _ := procCopyRect.Call( 652 uintptr(unsafe.Pointer(dst)), 653 uintptr(unsafe.Pointer(src))) 654 655 return ret != 0 656 } 657 658 func EqualRect(rect1, rect2 *RECT) bool { 659 ret, _, _ := procEqualRect.Call( 660 uintptr(unsafe.Pointer(rect1)), 661 uintptr(unsafe.Pointer(rect2))) 662 663 return ret != 0 664 } 665 666 func InflateRect(rect *RECT, dx, dy int) bool { 667 ret, _, _ := procInflateRect.Call( 668 uintptr(unsafe.Pointer(rect)), 669 uintptr(dx), 670 uintptr(dy)) 671 672 return ret != 0 673 } 674 675 func IntersectRect(dst, src1, src2 *RECT) bool { 676 ret, _, _ := procIntersectRect.Call( 677 uintptr(unsafe.Pointer(dst)), 678 uintptr(unsafe.Pointer(src1)), 679 uintptr(unsafe.Pointer(src2))) 680 681 return ret != 0 682 } 683 684 func IsRectEmpty(rect *RECT) bool { 685 ret, _, _ := procIsRectEmpty.Call( 686 uintptr(unsafe.Pointer(rect))) 687 688 return ret != 0 689 } 690 691 func OffsetRect(rect *RECT, dx, dy int) bool { 692 ret, _, _ := procOffsetRect.Call( 693 uintptr(unsafe.Pointer(rect)), 694 uintptr(dx), 695 uintptr(dy)) 696 697 return ret != 0 698 } 699 700 func PtInRect(rect *RECT, x, y int) bool { 701 pt := POINT{X: int32(x), Y: int32(y)} 702 ret, _, _ := procPtInRect.Call( 703 uintptr(unsafe.Pointer(rect)), 704 uintptr(unsafe.Pointer(&pt))) 705 706 return ret != 0 707 } 708 709 func SetRect(rect *RECT, left, top, right, bottom int) bool { 710 ret, _, _ := procSetRect.Call( 711 uintptr(unsafe.Pointer(rect)), 712 uintptr(left), 713 uintptr(top), 714 uintptr(right), 715 uintptr(bottom)) 716 717 return ret != 0 718 } 719 720 func SetRectEmpty(rect *RECT) bool { 721 ret, _, _ := procSetRectEmpty.Call( 722 uintptr(unsafe.Pointer(rect))) 723 724 return ret != 0 725 } 726 727 func SubtractRect(dst, src1, src2 *RECT) bool { 728 ret, _, _ := procSubtractRect.Call( 729 uintptr(unsafe.Pointer(dst)), 730 uintptr(unsafe.Pointer(src1)), 731 uintptr(unsafe.Pointer(src2))) 732 733 return ret != 0 734 } 735 736 func UnionRect(dst, src1, src2 *RECT) bool { 737 ret, _, _ := procUnionRect.Call( 738 uintptr(unsafe.Pointer(dst)), 739 uintptr(unsafe.Pointer(src1)), 740 uintptr(unsafe.Pointer(src2))) 741 742 return ret != 0 743 } 744 745 func CreateDialog(hInstance HINSTANCE, lpTemplate *uint16, hWndParent HWND, lpDialogProc uintptr) HWND { 746 ret, _, _ := procCreateDialogParam.Call( 747 uintptr(hInstance), 748 uintptr(unsafe.Pointer(lpTemplate)), 749 uintptr(hWndParent), 750 lpDialogProc, 751 0) 752 753 return HWND(ret) 754 } 755 756 func DialogBox(hInstance HINSTANCE, lpTemplateName *uint16, hWndParent HWND, lpDialogProc uintptr) int { 757 ret, _, _ := procDialogBoxParam.Call( 758 uintptr(hInstance), 759 uintptr(unsafe.Pointer(lpTemplateName)), 760 uintptr(hWndParent), 761 lpDialogProc, 762 0) 763 764 return int(ret) 765 } 766 767 func GetDlgItem(hDlg HWND, nIDDlgItem int) HWND { 768 ret, _, _ := procGetDlgItem.Call( 769 uintptr(unsafe.Pointer(hDlg)), 770 uintptr(nIDDlgItem)) 771 772 return HWND(ret) 773 } 774 775 func DrawIcon(hDC HDC, x, y int, hIcon HICON) bool { 776 ret, _, _ := procDrawIcon.Call( 777 uintptr(unsafe.Pointer(hDC)), 778 uintptr(x), 779 uintptr(y), 780 uintptr(unsafe.Pointer(hIcon))) 781 782 return ret != 0 783 } 784 785 func CreateMenu() HMENU { 786 ret, _, _ := procCreateMenu.Call(0, 787 0, 788 0, 789 0) 790 791 return HMENU(ret) 792 } 793 794 func SetMenu(hWnd HWND, hMenu HMENU) bool { 795 ret, _, _ := syscall.Syscall(setMenu, 2, 796 uintptr(hWnd), 797 uintptr(hMenu), 798 0) 799 800 return ret != 0 801 } 802 803 // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-checkmenuradioitem 804 func SelectRadioMenuItem(menuID uint16, startID uint16, endID uint16, hwnd HWND) bool { 805 ret, _, _ := procCheckMenuRadioItem.Call( 806 hwnd, 807 uintptr(startID), 808 uintptr(endID), 809 uintptr(menuID), 810 MF_BYCOMMAND) 811 return ret != 0 812 813 } 814 815 func CreatePopupMenu() HMENU { 816 ret, _, _ := procCreatePopupMenu.Call(0, 817 0, 818 0, 819 0) 820 821 return HMENU(ret) 822 } 823 824 func TrackPopupMenuEx(hMenu HMENU, fuFlags uint32, x, y int32, hWnd HWND, lptpm *TPMPARAMS) BOOL { 825 ret, _, _ := syscall.Syscall6(trackPopupMenuEx, 6, 826 uintptr(hMenu), 827 uintptr(fuFlags), 828 uintptr(x), 829 uintptr(y), 830 uintptr(hWnd), 831 uintptr(unsafe.Pointer(lptpm))) 832 833 return BOOL(ret) 834 } 835 836 func DrawMenuBar(hWnd HWND) bool { 837 ret, _, _ := syscall.Syscall(drawMenuBar, 1, 838 uintptr(hWnd), 839 0, 840 0) 841 842 return ret != 0 843 } 844 845 func InsertMenuItem(hMenu HMENU, uItem uint32, fByPosition bool, lpmii *MENUITEMINFO) bool { 846 ret, _, _ := syscall.Syscall6(insertMenuItem, 4, 847 uintptr(hMenu), 848 uintptr(uItem), 849 uintptr(BoolToBOOL(fByPosition)), 850 uintptr(unsafe.Pointer(lpmii)), 851 0, 852 0) 853 854 return ret != 0 855 } 856 857 func SetMenuItemInfo(hMenu HMENU, uItem uint32, fByPosition bool, lpmii *MENUITEMINFO) bool { 858 ret, _, _ := syscall.Syscall6(setMenuItemInfo, 4, 859 uintptr(hMenu), 860 uintptr(uItem), 861 uintptr(BoolToBOOL(fByPosition)), 862 uintptr(unsafe.Pointer(lpmii)), 863 0, 864 0) 865 866 return ret != 0 867 } 868 869 func ClientToScreen(hwnd HWND, x, y int) (int, int) { 870 pt := POINT{X: int32(x), Y: int32(y)} 871 872 procClientToScreen.Call( 873 uintptr(hwnd), 874 uintptr(unsafe.Pointer(&pt))) 875 876 return int(pt.X), int(pt.Y) 877 } 878 879 func IsDialogMessage(hwnd HWND, msg *MSG) bool { 880 ret, _, _ := procIsDialogMessage.Call( 881 uintptr(hwnd), 882 uintptr(unsafe.Pointer(msg))) 883 884 return ret != 0 885 } 886 887 func IsWindow(hwnd HWND) bool { 888 ret, _, _ := procIsWindow.Call( 889 uintptr(hwnd)) 890 891 return ret != 0 892 } 893 894 func EndDialog(hwnd HWND, nResult uintptr) bool { 895 ret, _, _ := procEndDialog.Call( 896 uintptr(hwnd), 897 nResult) 898 899 return ret != 0 900 } 901 902 func PeekMessage(lpMsg *MSG, hwnd HWND, wMsgFilterMin, wMsgFilterMax, wRemoveMsg uint32) bool { 903 ret, _, _ := procPeekMessage.Call( 904 uintptr(unsafe.Pointer(lpMsg)), 905 uintptr(hwnd), 906 uintptr(wMsgFilterMin), 907 uintptr(wMsgFilterMax), 908 uintptr(wRemoveMsg)) 909 910 return ret != 0 911 } 912 913 func TranslateAccelerator(hwnd HWND, hAccTable HACCEL, lpMsg *MSG) bool { 914 ret, _, _ := procTranslateMessage.Call( 915 uintptr(hwnd), 916 uintptr(hAccTable), 917 uintptr(unsafe.Pointer(lpMsg))) 918 919 return ret != 0 920 } 921 922 func SetWindowPos(hwnd, hWndInsertAfter HWND, x, y, cx, cy int, uFlags uint) bool { 923 ret, _, _ := procSetWindowPos.Call( 924 uintptr(hwnd), 925 uintptr(hWndInsertAfter), 926 uintptr(x), 927 uintptr(y), 928 uintptr(cx), 929 uintptr(cy), 930 uintptr(uFlags)) 931 932 return ret != 0 933 } 934 935 func FillRect(hDC HDC, lprc *RECT, hbr HBRUSH) bool { 936 ret, _, _ := procFillRect.Call( 937 uintptr(hDC), 938 uintptr(unsafe.Pointer(lprc)), 939 uintptr(hbr)) 940 941 return ret != 0 942 } 943 944 func DrawText(hDC HDC, text string, uCount int, lpRect *RECT, uFormat uint) int { 945 ret, _, _ := procDrawText.Call( 946 uintptr(hDC), 947 uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), 948 uintptr(uCount), 949 uintptr(unsafe.Pointer(lpRect)), 950 uintptr(uFormat)) 951 952 return int(ret) 953 } 954 955 func AddClipboardFormatListener(hwnd HWND) bool { 956 ret, _, _ := procAddClipboardFormatListener.Call( 957 uintptr(hwnd)) 958 return ret != 0 959 } 960 961 func RemoveClipboardFormatListener(hwnd HWND) bool { 962 ret, _, _ := procRemoveClipboardFormatListener.Call( 963 uintptr(hwnd)) 964 return ret != 0 965 } 966 967 func OpenClipboard(hWndNewOwner HWND) bool { 968 ret, _, _ := procOpenClipboard.Call( 969 uintptr(hWndNewOwner)) 970 return ret != 0 971 } 972 973 func CloseClipboard() bool { 974 ret, _, _ := procCloseClipboard.Call() 975 return ret != 0 976 } 977 978 func EnumClipboardFormats(format uint) uint { 979 ret, _, _ := procEnumClipboardFormats.Call( 980 uintptr(format)) 981 return uint(ret) 982 } 983 984 func GetClipboardData(uFormat uint) HANDLE { 985 ret, _, _ := procGetClipboardData.Call( 986 uintptr(uFormat)) 987 return HANDLE(ret) 988 } 989 990 func SetClipboardData(uFormat uint, hMem HANDLE) HANDLE { 991 ret, _, _ := procSetClipboardData.Call( 992 uintptr(uFormat), 993 uintptr(hMem)) 994 return HANDLE(ret) 995 } 996 997 func EmptyClipboard() bool { 998 ret, _, _ := procEmptyClipboard.Call() 999 return ret != 0 1000 } 1001 1002 func GetClipboardFormatName(format uint) (string, bool) { 1003 cchMaxCount := 255 1004 buf := make([]uint16, cchMaxCount) 1005 ret, _, _ := procGetClipboardFormatName.Call( 1006 uintptr(format), 1007 uintptr(unsafe.Pointer(&buf[0])), 1008 uintptr(cchMaxCount)) 1009 1010 if ret > 0 { 1011 return syscall.UTF16ToString(buf), true 1012 } 1013 1014 return "Requested format does not exist or is predefined", false 1015 } 1016 1017 func IsClipboardFormatAvailable(format uint) bool { 1018 ret, _, _ := procIsClipboardFormatAvailable.Call(uintptr(format)) 1019 return ret != 0 1020 } 1021 1022 func BeginPaint(hwnd HWND, paint *PAINTSTRUCT) HDC { 1023 ret, _, _ := procBeginPaint.Call( 1024 uintptr(hwnd), 1025 uintptr(unsafe.Pointer(paint))) 1026 return HDC(ret) 1027 } 1028 1029 func EndPaint(hwnd HWND, paint *PAINTSTRUCT) { 1030 procEndPaint.Call( 1031 uintptr(hwnd), 1032 uintptr(unsafe.Pointer(paint))) 1033 } 1034 1035 func GetKeyboardState(keyState []byte) bool { 1036 if len(keyState) != 256 { 1037 panic("keyState slice must have a size of 256 bytes") 1038 } 1039 ret, _, _ := procGetKeyboardState.Call(uintptr(unsafe.Pointer(&keyState[0]))) 1040 return ret != 0 1041 } 1042 1043 func MapVirtualKeyEx(uCode, uMapType uint, dwhkl HKL) uint { 1044 ret, _, _ := procMapVirtualKey.Call( 1045 uintptr(uCode), 1046 uintptr(uMapType), 1047 uintptr(dwhkl)) 1048 return uint(ret) 1049 } 1050 1051 func GetAsyncKeyState(vKey int) uint16 { 1052 ret, _, _ := procGetAsyncKeyState.Call(uintptr(vKey)) 1053 return uint16(ret) 1054 } 1055 1056 func ToAscii(uVirtKey, uScanCode uint, lpKeyState *byte, lpChar *uint16, uFlags uint) int { 1057 ret, _, _ := procToAscii.Call( 1058 uintptr(uVirtKey), 1059 uintptr(uScanCode), 1060 uintptr(unsafe.Pointer(lpKeyState)), 1061 uintptr(unsafe.Pointer(lpChar)), 1062 uintptr(uFlags)) 1063 return int(ret) 1064 } 1065 1066 func SwapMouseButton(fSwap bool) bool { 1067 ret, _, _ := procSwapMouseButton.Call( 1068 uintptr(BoolToBOOL(fSwap))) 1069 return ret != 0 1070 } 1071 1072 func GetCursorPos() (x, y int, ok bool) { 1073 pt := POINT{} 1074 ret, _, _ := procGetCursorPos.Call(uintptr(unsafe.Pointer(&pt))) 1075 return int(pt.X), int(pt.Y), ret != 0 1076 } 1077 1078 func SetCursorPos(x, y int) bool { 1079 ret, _, _ := procSetCursorPos.Call( 1080 uintptr(x), 1081 uintptr(y), 1082 ) 1083 return ret != 0 1084 } 1085 1086 func SetCursor(cursor HCURSOR) HCURSOR { 1087 ret, _, _ := procSetCursor.Call( 1088 uintptr(cursor), 1089 ) 1090 return HCURSOR(ret) 1091 } 1092 1093 func CreateIcon(instance HINSTANCE, nWidth, nHeight int, cPlanes, cBitsPerPixel byte, ANDbits, XORbits *byte) HICON { 1094 ret, _, _ := procCreateIcon.Call( 1095 uintptr(instance), 1096 uintptr(nWidth), 1097 uintptr(nHeight), 1098 uintptr(cPlanes), 1099 uintptr(cBitsPerPixel), 1100 uintptr(unsafe.Pointer(ANDbits)), 1101 uintptr(unsafe.Pointer(XORbits)), 1102 ) 1103 return HICON(ret) 1104 } 1105 1106 func DestroyIcon(icon HICON) bool { 1107 ret, _, _ := procDestroyIcon.Call( 1108 uintptr(icon), 1109 ) 1110 return ret != 0 1111 } 1112 1113 func MonitorFromPoint(x, y int, dwFlags uint32) HMONITOR { 1114 ret, _, _ := procMonitorFromPoint.Call( 1115 uintptr(x), 1116 uintptr(y), 1117 uintptr(dwFlags), 1118 ) 1119 return HMONITOR(ret) 1120 } 1121 1122 func MonitorFromRect(rc *RECT, dwFlags uint32) HMONITOR { 1123 ret, _, _ := procMonitorFromRect.Call( 1124 uintptr(unsafe.Pointer(rc)), 1125 uintptr(dwFlags), 1126 ) 1127 return HMONITOR(ret) 1128 } 1129 1130 func MonitorFromWindow(hwnd HWND, dwFlags uint32) HMONITOR { 1131 ret, _, _ := procMonitorFromWindow.Call( 1132 uintptr(hwnd), 1133 uintptr(dwFlags), 1134 ) 1135 return HMONITOR(ret) 1136 } 1137 1138 func GetMonitorInfo(hMonitor HMONITOR, lmpi *MONITORINFO) bool { 1139 ret, _, _ := procGetMonitorInfo.Call( 1140 uintptr(hMonitor), 1141 uintptr(unsafe.Pointer(lmpi)), 1142 ) 1143 return ret != 0 1144 } 1145 1146 func EnumDisplayMonitors(hdc HDC, clip *RECT, fnEnum uintptr, dwData unsafe.Pointer) bool { 1147 ret, _, _ := procEnumDisplayMonitors.Call( 1148 hdc, 1149 uintptr(unsafe.Pointer(clip)), 1150 fnEnum, 1151 uintptr(dwData), 1152 ) 1153 return ret != 0 1154 } 1155 1156 func EnumDisplaySettingsEx(szDeviceName *uint16, iModeNum uint32, devMode *DEVMODE, dwFlags uint32) bool { 1157 ret, _, _ := procEnumDisplaySettingsEx.Call( 1158 uintptr(unsafe.Pointer(szDeviceName)), 1159 uintptr(iModeNum), 1160 uintptr(unsafe.Pointer(devMode)), 1161 uintptr(dwFlags), 1162 ) 1163 return ret != 0 1164 } 1165 1166 func ChangeDisplaySettingsEx(szDeviceName *uint16, devMode *DEVMODE, hwnd HWND, dwFlags uint32, lParam uintptr) int32 { 1167 ret, _, _ := procChangeDisplaySettingsEx.Call( 1168 uintptr(unsafe.Pointer(szDeviceName)), 1169 uintptr(unsafe.Pointer(devMode)), 1170 uintptr(hwnd), 1171 uintptr(dwFlags), 1172 lParam, 1173 ) 1174 return int32(ret) 1175 } 1176 1177 /* 1178 func SendInput(inputs []INPUT) uint32 { 1179 var validInputs []C.INPUT 1180 1181 for _, oneInput := range inputs { 1182 input := C.INPUT{_type: C.DWORD(oneInput.Type)} 1183 1184 switch oneInput.Type { 1185 case INPUT_MOUSE: 1186 (*MouseInput)(unsafe.Pointer(&input)).mi = oneInput.Mi 1187 case INPUT_KEYBOARD: 1188 (*KbdInput)(unsafe.Pointer(&input)).ki = oneInput.Ki 1189 case INPUT_HARDWARE: 1190 (*HardwareInput)(unsafe.Pointer(&input)).hi = oneInput.Hi 1191 default: 1192 panic("unkown type") 1193 } 1194 1195 validInputs = append(validInputs, input) 1196 } 1197 1198 ret, _, _ := procSendInput.Call( 1199 uintptr(len(validInputs)), 1200 uintptr(unsafe.Pointer(&validInputs[0])), 1201 uintptr(unsafe.Sizeof(C.INPUT{})), 1202 ) 1203 return uint32(ret) 1204 }*/ 1205 1206 func SetWindowsHookEx(idHook int, lpfn HOOKPROC, hMod HINSTANCE, dwThreadId DWORD) HHOOK { 1207 ret, _, _ := procSetWindowsHookEx.Call( 1208 uintptr(idHook), 1209 uintptr(syscall.NewCallback(lpfn)), 1210 uintptr(hMod), 1211 uintptr(dwThreadId), 1212 ) 1213 return HHOOK(ret) 1214 } 1215 1216 func UnhookWindowsHookEx(hhk HHOOK) bool { 1217 ret, _, _ := procUnhookWindowsHookEx.Call( 1218 uintptr(hhk), 1219 ) 1220 return ret != 0 1221 } 1222 1223 func CallNextHookEx(hhk HHOOK, nCode int, wParam WPARAM, lParam LPARAM) LRESULT { 1224 ret, _, _ := procCallNextHookEx.Call( 1225 uintptr(hhk), 1226 uintptr(nCode), 1227 uintptr(wParam), 1228 uintptr(lParam), 1229 ) 1230 return LRESULT(ret) 1231 } 1232 1233 func GetKeyState(nVirtKey int32) int16 { 1234 ret, _, _ := syscall.Syscall(getKeyState, 1, 1235 uintptr(nVirtKey), 1236 0, 1237 0) 1238 1239 return int16(ret) 1240 } 1241 1242 func DestroyMenu(hMenu HMENU) bool { 1243 ret, _, _ := procDestroyMenu.Call(1, 1244 uintptr(hMenu), 1245 0, 1246 0) 1247 1248 return ret != 0 1249 } 1250 1251 func GetWindowPlacement(hWnd HWND, lpwndpl *WINDOWPLACEMENT) bool { 1252 ret, _, _ := syscall.Syscall(getWindowPlacement, 2, 1253 uintptr(hWnd), 1254 uintptr(unsafe.Pointer(lpwndpl)), 1255 0) 1256 1257 return ret != 0 1258 } 1259 1260 func SetWindowPlacement(hWnd HWND, lpwndpl *WINDOWPLACEMENT) bool { 1261 ret, _, _ := syscall.Syscall(setWindowPlacement, 2, 1262 uintptr(hWnd), 1263 uintptr(unsafe.Pointer(lpwndpl)), 1264 0) 1265 1266 return ret != 0 1267 } 1268 1269 func SetScrollInfo(hwnd HWND, fnBar int32, lpsi *SCROLLINFO, fRedraw bool) int32 { 1270 ret, _, _ := syscall.Syscall6(setScrollInfo, 4, 1271 hwnd, 1272 uintptr(fnBar), 1273 uintptr(unsafe.Pointer(lpsi)), 1274 uintptr(BoolToBOOL(fRedraw)), 1275 0, 1276 0) 1277 1278 return int32(ret) 1279 } 1280 1281 func GetScrollInfo(hwnd HWND, fnBar int32, lpsi *SCROLLINFO) bool { 1282 ret, _, _ := syscall.Syscall(getScrollInfo, 3, 1283 hwnd, 1284 uintptr(fnBar), 1285 uintptr(unsafe.Pointer(lpsi))) 1286 1287 return ret != 0 1288 }