user32.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. // Copyright 2010-2012 The W32 Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build windows
  5. package w32
  6. import (
  7. "fmt"
  8. "syscall"
  9. "unsafe"
  10. )
  11. var (
  12. moduser32 = syscall.NewLazyDLL("user32.dll")
  13. procRegisterClassEx = moduser32.NewProc("RegisterClassExW")
  14. procLoadIcon = moduser32.NewProc("LoadIconW")
  15. procLoadCursor = moduser32.NewProc("LoadCursorW")
  16. procShowWindow = moduser32.NewProc("ShowWindow")
  17. procUpdateWindow = moduser32.NewProc("UpdateWindow")
  18. procCreateWindowEx = moduser32.NewProc("CreateWindowExW")
  19. procAdjustWindowRect = moduser32.NewProc("AdjustWindowRect")
  20. procAdjustWindowRectEx = moduser32.NewProc("AdjustWindowRectEx")
  21. procDestroyWindow = moduser32.NewProc("DestroyWindow")
  22. procDefWindowProc = moduser32.NewProc("DefWindowProcW")
  23. procDefDlgProc = moduser32.NewProc("DefDlgProcW")
  24. procPostQuitMessage = moduser32.NewProc("PostQuitMessage")
  25. procGetMessage = moduser32.NewProc("GetMessageW")
  26. procTranslateMessage = moduser32.NewProc("TranslateMessage")
  27. procDispatchMessage = moduser32.NewProc("DispatchMessageW")
  28. procSendMessage = moduser32.NewProc("SendMessageW")
  29. procPostMessage = moduser32.NewProc("PostMessageW")
  30. procWaitMessage = moduser32.NewProc("WaitMessage")
  31. procSetWindowText = moduser32.NewProc("SetWindowTextW")
  32. procGetWindowTextLength = moduser32.NewProc("GetWindowTextLengthW")
  33. procGetWindowText = moduser32.NewProc("GetWindowTextW")
  34. procGetWindowRect = moduser32.NewProc("GetWindowRect")
  35. procMoveWindow = moduser32.NewProc("MoveWindow")
  36. procScreenToClient = moduser32.NewProc("ScreenToClient")
  37. procCallWindowProc = moduser32.NewProc("CallWindowProcW")
  38. procSetWindowLong = moduser32.NewProc("SetWindowLongW")
  39. procSetWindowLongPtr = moduser32.NewProc("SetWindowLongW")
  40. procGetWindowLong = moduser32.NewProc("GetWindowLongW")
  41. procGetWindowLongPtr = moduser32.NewProc("GetWindowLongW")
  42. procEnableWindow = moduser32.NewProc("EnableWindow")
  43. procIsWindowEnabled = moduser32.NewProc("IsWindowEnabled")
  44. procIsWindowVisible = moduser32.NewProc("IsWindowVisible")
  45. procSetFocus = moduser32.NewProc("SetFocus")
  46. procInvalidateRect = moduser32.NewProc("InvalidateRect")
  47. procGetClientRect = moduser32.NewProc("GetClientRect")
  48. procGetDC = moduser32.NewProc("GetDC")
  49. procReleaseDC = moduser32.NewProc("ReleaseDC")
  50. procSetCapture = moduser32.NewProc("SetCapture")
  51. procReleaseCapture = moduser32.NewProc("ReleaseCapture")
  52. procGetWindowThreadProcessId = moduser32.NewProc("GetWindowThreadProcessId")
  53. procMessageBox = moduser32.NewProc("MessageBoxW")
  54. procGetSystemMetrics = moduser32.NewProc("GetSystemMetrics")
  55. procCopyRect = moduser32.NewProc("CopyRect")
  56. procEqualRect = moduser32.NewProc("EqualRect")
  57. procInflateRect = moduser32.NewProc("InflateRect")
  58. procIntersectRect = moduser32.NewProc("IntersectRect")
  59. procIsRectEmpty = moduser32.NewProc("IsRectEmpty")
  60. procOffsetRect = moduser32.NewProc("OffsetRect")
  61. procPtInRect = moduser32.NewProc("PtInRect")
  62. procSetRect = moduser32.NewProc("SetRect")
  63. procSetRectEmpty = moduser32.NewProc("SetRectEmpty")
  64. procSubtractRect = moduser32.NewProc("SubtractRect")
  65. procUnionRect = moduser32.NewProc("UnionRect")
  66. procCreateDialogParam = moduser32.NewProc("CreateDialogParamW")
  67. procDialogBoxParam = moduser32.NewProc("DialogBoxParamW")
  68. procGetDlgItem = moduser32.NewProc("GetDlgItem")
  69. procDrawIcon = moduser32.NewProc("DrawIcon")
  70. procClientToScreen = moduser32.NewProc("ClientToScreen")
  71. procIsDialogMessage = moduser32.NewProc("IsDialogMessageW")
  72. procIsWindow = moduser32.NewProc("IsWindow")
  73. procEndDialog = moduser32.NewProc("EndDialog")
  74. procPeekMessage = moduser32.NewProc("PeekMessageW")
  75. procTranslateAccelerator = moduser32.NewProc("TranslateAcceleratorW")
  76. procSetWindowPos = moduser32.NewProc("SetWindowPos")
  77. procFillRect = moduser32.NewProc("FillRect")
  78. procDrawText = moduser32.NewProc("DrawTextW")
  79. procAddClipboardFormatListener = moduser32.NewProc("AddClipboardFormatListener")
  80. procRemoveClipboardFormatListener = moduser32.NewProc("RemoveClipboardFormatListener")
  81. procOpenClipboard = moduser32.NewProc("OpenClipboard")
  82. procCloseClipboard = moduser32.NewProc("CloseClipboard")
  83. procEnumClipboardFormats = moduser32.NewProc("EnumClipboardFormats")
  84. procGetClipboardData = moduser32.NewProc("GetClipboardData")
  85. procSetClipboardData = moduser32.NewProc("SetClipboardData")
  86. procEmptyClipboard = moduser32.NewProc("EmptyClipboard")
  87. procGetClipboardFormatName = moduser32.NewProc("GetClipboardFormatNameW")
  88. procIsClipboardFormatAvailable = moduser32.NewProc("IsClipboardFormatAvailable")
  89. procBeginPaint = moduser32.NewProc("BeginPaint")
  90. procEndPaint = moduser32.NewProc("EndPaint")
  91. procGetKeyboardState = moduser32.NewProc("GetKeyboardState")
  92. procMapVirtualKey = moduser32.NewProc("MapVirtualKeyExW")
  93. procGetAsyncKeyState = moduser32.NewProc("GetAsyncKeyState")
  94. procToAscii = moduser32.NewProc("ToAscii")
  95. procSwapMouseButton = moduser32.NewProc("SwapMouseButton")
  96. procGetCursorPos = moduser32.NewProc("GetCursorPos")
  97. procSetCursorPos = moduser32.NewProc("SetCursorPos")
  98. procSetCursor = moduser32.NewProc("SetCursor")
  99. procCreateIcon = moduser32.NewProc("CreateIcon")
  100. procDestroyIcon = moduser32.NewProc("DestroyIcon")
  101. procMonitorFromPoint = moduser32.NewProc("MonitorFromPoint")
  102. procMonitorFromRect = moduser32.NewProc("MonitorFromRect")
  103. procMonitorFromWindow = moduser32.NewProc("MonitorFromWindow")
  104. procGetMonitorInfo = moduser32.NewProc("GetMonitorInfoW")
  105. procEnumDisplayMonitors = moduser32.NewProc("EnumDisplayMonitors")
  106. procEnumDisplaySettingsEx = moduser32.NewProc("EnumDisplaySettingsExW")
  107. procChangeDisplaySettingsEx = moduser32.NewProc("ChangeDisplaySettingsExW")
  108. procSendInput = moduser32.NewProc("SendInput")
  109. )
  110. func RegisterClassEx(wndClassEx *WNDCLASSEX) ATOM {
  111. ret, _, _ := procRegisterClassEx.Call(uintptr(unsafe.Pointer(wndClassEx)))
  112. return ATOM(ret)
  113. }
  114. func LoadIcon(instance HINSTANCE, iconName *uint16) HICON {
  115. ret, _, _ := procLoadIcon.Call(
  116. uintptr(instance),
  117. uintptr(unsafe.Pointer(iconName)))
  118. return HICON(ret)
  119. }
  120. func LoadCursor(instance HINSTANCE, cursorName *uint16) HCURSOR {
  121. ret, _, _ := procLoadCursor.Call(
  122. uintptr(instance),
  123. uintptr(unsafe.Pointer(cursorName)))
  124. return HCURSOR(ret)
  125. }
  126. func ShowWindow(hwnd HWND, cmdshow int) bool {
  127. ret, _, _ := procShowWindow.Call(
  128. uintptr(hwnd),
  129. uintptr(cmdshow))
  130. return ret != 0
  131. }
  132. func UpdateWindow(hwnd HWND) bool {
  133. ret, _, _ := procUpdateWindow.Call(
  134. uintptr(hwnd))
  135. return ret != 0
  136. }
  137. func CreateWindowEx(exStyle uint, className, windowName *uint16,
  138. style uint, x, y, width, height int, parent HWND, menu HMENU,
  139. instance HINSTANCE, param unsafe.Pointer) HWND {
  140. ret, _, _ := procCreateWindowEx.Call(
  141. uintptr(exStyle),
  142. uintptr(unsafe.Pointer(className)),
  143. uintptr(unsafe.Pointer(windowName)),
  144. uintptr(style),
  145. uintptr(x),
  146. uintptr(y),
  147. uintptr(width),
  148. uintptr(height),
  149. uintptr(parent),
  150. uintptr(menu),
  151. uintptr(instance),
  152. uintptr(param))
  153. return HWND(ret)
  154. }
  155. func AdjustWindowRectEx(rect *RECT, style uint, menu bool, exStyle uint) bool {
  156. ret, _, _ := procAdjustWindowRectEx.Call(
  157. uintptr(unsafe.Pointer(rect)),
  158. uintptr(style),
  159. uintptr(BoolToBOOL(menu)),
  160. uintptr(exStyle))
  161. return ret != 0
  162. }
  163. func AdjustWindowRect(rect *RECT, style uint, menu bool) bool {
  164. ret, _, _ := procAdjustWindowRect.Call(
  165. uintptr(unsafe.Pointer(rect)),
  166. uintptr(style),
  167. uintptr(BoolToBOOL(menu)))
  168. return ret != 0
  169. }
  170. func DestroyWindow(hwnd HWND) bool {
  171. ret, _, _ := procDestroyWindow.Call(
  172. uintptr(hwnd))
  173. return ret != 0
  174. }
  175. func DefWindowProc(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr {
  176. ret, _, _ := procDefWindowProc.Call(
  177. uintptr(hwnd),
  178. uintptr(msg),
  179. wParam,
  180. lParam)
  181. return ret
  182. }
  183. func DefDlgProc(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr {
  184. ret, _, _ := procDefDlgProc.Call(
  185. uintptr(hwnd),
  186. uintptr(msg),
  187. wParam,
  188. lParam)
  189. return ret
  190. }
  191. func PostQuitMessage(exitCode int) {
  192. procPostQuitMessage.Call(
  193. uintptr(exitCode))
  194. }
  195. func GetMessage(msg *MSG, hwnd HWND, msgFilterMin, msgFilterMax uint32) int {
  196. ret, _, _ := procGetMessage.Call(
  197. uintptr(unsafe.Pointer(msg)),
  198. uintptr(hwnd),
  199. uintptr(msgFilterMin),
  200. uintptr(msgFilterMax))
  201. return int(ret)
  202. }
  203. func TranslateMessage(msg *MSG) bool {
  204. ret, _, _ := procTranslateMessage.Call(
  205. uintptr(unsafe.Pointer(msg)))
  206. return ret != 0
  207. }
  208. func DispatchMessage(msg *MSG) uintptr {
  209. ret, _, _ := procDispatchMessage.Call(
  210. uintptr(unsafe.Pointer(msg)))
  211. return ret
  212. }
  213. func SendMessage(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr {
  214. ret, _, _ := procSendMessage.Call(
  215. uintptr(hwnd),
  216. uintptr(msg),
  217. wParam,
  218. lParam)
  219. return ret
  220. }
  221. func PostMessage(hwnd HWND, msg uint32, wParam, lParam uintptr) bool {
  222. ret, _, _ := procPostMessage.Call(
  223. uintptr(hwnd),
  224. uintptr(msg),
  225. wParam,
  226. lParam)
  227. return ret != 0
  228. }
  229. func WaitMessage() bool {
  230. ret, _, _ := procWaitMessage.Call()
  231. return ret != 0
  232. }
  233. func SetWindowText(hwnd HWND, text string) {
  234. procSetWindowText.Call(
  235. uintptr(hwnd),
  236. uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))))
  237. }
  238. func GetWindowTextLength(hwnd HWND) int {
  239. ret, _, _ := procGetWindowTextLength.Call(
  240. uintptr(hwnd))
  241. return int(ret)
  242. }
  243. func GetWindowText(hwnd HWND) string {
  244. textLen := GetWindowTextLength(hwnd) + 1
  245. buf := make([]uint16, textLen)
  246. procGetWindowText.Call(
  247. uintptr(hwnd),
  248. uintptr(unsafe.Pointer(&buf[0])),
  249. uintptr(textLen))
  250. return syscall.UTF16ToString(buf)
  251. }
  252. func GetWindowRect(hwnd HWND) *RECT {
  253. var rect RECT
  254. procGetWindowRect.Call(
  255. uintptr(hwnd),
  256. uintptr(unsafe.Pointer(&rect)))
  257. return &rect
  258. }
  259. func MoveWindow(hwnd HWND, x, y, width, height int, repaint bool) bool {
  260. ret, _, _ := procMoveWindow.Call(
  261. uintptr(hwnd),
  262. uintptr(x),
  263. uintptr(y),
  264. uintptr(width),
  265. uintptr(height),
  266. uintptr(BoolToBOOL(repaint)))
  267. return ret != 0
  268. }
  269. func ScreenToClient(hwnd HWND, x, y int) (X, Y int, ok bool) {
  270. pt := POINT{X: int32(x), Y: int32(y)}
  271. ret, _, _ := procScreenToClient.Call(
  272. uintptr(hwnd),
  273. uintptr(unsafe.Pointer(&pt)))
  274. return int(pt.X), int(pt.Y), ret != 0
  275. }
  276. func CallWindowProc(preWndProc uintptr, hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr {
  277. ret, _, _ := procCallWindowProc.Call(
  278. preWndProc,
  279. uintptr(hwnd),
  280. uintptr(msg),
  281. wParam,
  282. lParam)
  283. return ret
  284. }
  285. func SetWindowLong(hwnd HWND, index int, value uint32) uint32 {
  286. ret, _, _ := procSetWindowLong.Call(
  287. uintptr(hwnd),
  288. uintptr(index),
  289. uintptr(value))
  290. return uint32(ret)
  291. }
  292. func SetWindowLongPtr(hwnd HWND, index int, value uintptr) uintptr {
  293. ret, _, _ := procSetWindowLongPtr.Call(
  294. uintptr(hwnd),
  295. uintptr(index),
  296. value)
  297. return ret
  298. }
  299. func GetWindowLong(hwnd HWND, index int) int32 {
  300. ret, _, _ := procGetWindowLong.Call(
  301. uintptr(hwnd),
  302. uintptr(index))
  303. return int32(ret)
  304. }
  305. func GetWindowLongPtr(hwnd HWND, index int) uintptr {
  306. ret, _, _ := procGetWindowLongPtr.Call(
  307. uintptr(hwnd),
  308. uintptr(index))
  309. return ret
  310. }
  311. func EnableWindow(hwnd HWND, b bool) bool {
  312. ret, _, _ := procEnableWindow.Call(
  313. uintptr(hwnd),
  314. uintptr(BoolToBOOL(b)))
  315. return ret != 0
  316. }
  317. func IsWindowEnabled(hwnd HWND) bool {
  318. ret, _, _ := procIsWindowEnabled.Call(
  319. uintptr(hwnd))
  320. return ret != 0
  321. }
  322. func IsWindowVisible(hwnd HWND) bool {
  323. ret, _, _ := procIsWindowVisible.Call(
  324. uintptr(hwnd))
  325. return ret != 0
  326. }
  327. func SetFocus(hwnd HWND) HWND {
  328. ret, _, _ := procSetFocus.Call(
  329. uintptr(hwnd))
  330. return HWND(ret)
  331. }
  332. func InvalidateRect(hwnd HWND, rect *RECT, erase bool) bool {
  333. ret, _, _ := procInvalidateRect.Call(
  334. uintptr(hwnd),
  335. uintptr(unsafe.Pointer(rect)),
  336. uintptr(BoolToBOOL(erase)))
  337. return ret != 0
  338. }
  339. func GetClientRect(hwnd HWND) *RECT {
  340. var rect RECT
  341. ret, _, _ := procGetClientRect.Call(
  342. uintptr(hwnd),
  343. uintptr(unsafe.Pointer(&rect)))
  344. if ret == 0 {
  345. panic(fmt.Sprintf("GetClientRect(%d) failed", hwnd))
  346. }
  347. return &rect
  348. }
  349. func GetDC(hwnd HWND) HDC {
  350. ret, _, _ := procGetDC.Call(
  351. uintptr(hwnd))
  352. return HDC(ret)
  353. }
  354. func ReleaseDC(hwnd HWND, hDC HDC) bool {
  355. ret, _, _ := procReleaseDC.Call(
  356. uintptr(hwnd),
  357. uintptr(hDC))
  358. return ret != 0
  359. }
  360. func SetCapture(hwnd HWND) HWND {
  361. ret, _, _ := procSetCapture.Call(
  362. uintptr(hwnd))
  363. return HWND(ret)
  364. }
  365. func ReleaseCapture() bool {
  366. ret, _, _ := procReleaseCapture.Call()
  367. return ret != 0
  368. }
  369. func GetWindowThreadProcessId(hwnd HWND) (HANDLE, int) {
  370. var processId int
  371. ret, _, _ := procGetWindowThreadProcessId.Call(
  372. uintptr(hwnd),
  373. uintptr(unsafe.Pointer(&processId)))
  374. return HANDLE(ret), processId
  375. }
  376. func MessageBox(hwnd HWND, title, caption string, flags uint) int {
  377. ret, _, _ := procMessageBox.Call(
  378. uintptr(hwnd),
  379. uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))),
  380. uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))),
  381. uintptr(flags))
  382. return int(ret)
  383. }
  384. func GetSystemMetrics(index int) int {
  385. ret, _, _ := procGetSystemMetrics.Call(
  386. uintptr(index))
  387. return int(ret)
  388. }
  389. func CopyRect(dst, src *RECT) bool {
  390. ret, _, _ := procCopyRect.Call(
  391. uintptr(unsafe.Pointer(dst)),
  392. uintptr(unsafe.Pointer(src)))
  393. return ret != 0
  394. }
  395. func EqualRect(rect1, rect2 *RECT) bool {
  396. ret, _, _ := procEqualRect.Call(
  397. uintptr(unsafe.Pointer(rect1)),
  398. uintptr(unsafe.Pointer(rect2)))
  399. return ret != 0
  400. }
  401. func InflateRect(rect *RECT, dx, dy int) bool {
  402. ret, _, _ := procInflateRect.Call(
  403. uintptr(unsafe.Pointer(rect)),
  404. uintptr(dx),
  405. uintptr(dy))
  406. return ret != 0
  407. }
  408. func IntersectRect(dst, src1, src2 *RECT) bool {
  409. ret, _, _ := procIntersectRect.Call(
  410. uintptr(unsafe.Pointer(dst)),
  411. uintptr(unsafe.Pointer(src1)),
  412. uintptr(unsafe.Pointer(src2)))
  413. return ret != 0
  414. }
  415. func IsRectEmpty(rect *RECT) bool {
  416. ret, _, _ := procIsRectEmpty.Call(
  417. uintptr(unsafe.Pointer(rect)))
  418. return ret != 0
  419. }
  420. func OffsetRect(rect *RECT, dx, dy int) bool {
  421. ret, _, _ := procOffsetRect.Call(
  422. uintptr(unsafe.Pointer(rect)),
  423. uintptr(dx),
  424. uintptr(dy))
  425. return ret != 0
  426. }
  427. func PtInRect(rect *RECT, x, y int) bool {
  428. pt := POINT{X: int32(x), Y: int32(y)}
  429. ret, _, _ := procPtInRect.Call(
  430. uintptr(unsafe.Pointer(rect)),
  431. uintptr(unsafe.Pointer(&pt)))
  432. return ret != 0
  433. }
  434. func SetRect(rect *RECT, left, top, right, bottom int) bool {
  435. ret, _, _ := procSetRect.Call(
  436. uintptr(unsafe.Pointer(rect)),
  437. uintptr(left),
  438. uintptr(top),
  439. uintptr(right),
  440. uintptr(bottom))
  441. return ret != 0
  442. }
  443. func SetRectEmpty(rect *RECT) bool {
  444. ret, _, _ := procSetRectEmpty.Call(
  445. uintptr(unsafe.Pointer(rect)))
  446. return ret != 0
  447. }
  448. func SubtractRect(dst, src1, src2 *RECT) bool {
  449. ret, _, _ := procSubtractRect.Call(
  450. uintptr(unsafe.Pointer(dst)),
  451. uintptr(unsafe.Pointer(src1)),
  452. uintptr(unsafe.Pointer(src2)))
  453. return ret != 0
  454. }
  455. func UnionRect(dst, src1, src2 *RECT) bool {
  456. ret, _, _ := procUnionRect.Call(
  457. uintptr(unsafe.Pointer(dst)),
  458. uintptr(unsafe.Pointer(src1)),
  459. uintptr(unsafe.Pointer(src2)))
  460. return ret != 0
  461. }
  462. func CreateDialog(hInstance HINSTANCE, lpTemplate *uint16, hWndParent HWND, lpDialogProc uintptr) HWND {
  463. ret, _, _ := procCreateDialogParam.Call(
  464. uintptr(hInstance),
  465. uintptr(unsafe.Pointer(lpTemplate)),
  466. uintptr(hWndParent),
  467. lpDialogProc,
  468. 0)
  469. return HWND(ret)
  470. }
  471. func DialogBox(hInstance HINSTANCE, lpTemplateName *uint16, hWndParent HWND, lpDialogProc uintptr) int {
  472. ret, _, _ := procDialogBoxParam.Call(
  473. uintptr(hInstance),
  474. uintptr(unsafe.Pointer(lpTemplateName)),
  475. uintptr(hWndParent),
  476. lpDialogProc,
  477. 0)
  478. return int(ret)
  479. }
  480. func GetDlgItem(hDlg HWND, nIDDlgItem int) HWND {
  481. ret, _, _ := procGetDlgItem.Call(
  482. uintptr(unsafe.Pointer(hDlg)),
  483. uintptr(nIDDlgItem))
  484. return HWND(ret)
  485. }
  486. func DrawIcon(hDC HDC, x, y int, hIcon HICON) bool {
  487. ret, _, _ := procDrawIcon.Call(
  488. uintptr(unsafe.Pointer(hDC)),
  489. uintptr(x),
  490. uintptr(y),
  491. uintptr(unsafe.Pointer(hIcon)))
  492. return ret != 0
  493. }
  494. func ClientToScreen(hwnd HWND, x, y int) (int, int) {
  495. pt := POINT{X: int32(x), Y: int32(y)}
  496. procClientToScreen.Call(
  497. uintptr(hwnd),
  498. uintptr(unsafe.Pointer(&pt)))
  499. return int(pt.X), int(pt.Y)
  500. }
  501. func IsDialogMessage(hwnd HWND, msg *MSG) bool {
  502. ret, _, _ := procIsDialogMessage.Call(
  503. uintptr(hwnd),
  504. uintptr(unsafe.Pointer(msg)))
  505. return ret != 0
  506. }
  507. func IsWindow(hwnd HWND) bool {
  508. ret, _, _ := procIsWindow.Call(
  509. uintptr(hwnd))
  510. return ret != 0
  511. }
  512. func EndDialog(hwnd HWND, nResult uintptr) bool {
  513. ret, _, _ := procEndDialog.Call(
  514. uintptr(hwnd),
  515. nResult)
  516. return ret != 0
  517. }
  518. func PeekMessage(lpMsg *MSG, hwnd HWND, wMsgFilterMin, wMsgFilterMax, wRemoveMsg uint32) bool {
  519. ret, _, _ := procPeekMessage.Call(
  520. uintptr(unsafe.Pointer(lpMsg)),
  521. uintptr(hwnd),
  522. uintptr(wMsgFilterMin),
  523. uintptr(wMsgFilterMax),
  524. uintptr(wRemoveMsg))
  525. return ret != 0
  526. }
  527. func TranslateAccelerator(hwnd HWND, hAccTable HACCEL, lpMsg *MSG) bool {
  528. ret, _, _ := procTranslateMessage.Call(
  529. uintptr(hwnd),
  530. uintptr(hAccTable),
  531. uintptr(unsafe.Pointer(lpMsg)))
  532. return ret != 0
  533. }
  534. func SetWindowPos(hwnd, hWndInsertAfter HWND, x, y, cx, cy int, uFlags uint) bool {
  535. ret, _, _ := procSetWindowPos.Call(
  536. uintptr(hwnd),
  537. uintptr(hWndInsertAfter),
  538. uintptr(x),
  539. uintptr(y),
  540. uintptr(cx),
  541. uintptr(cy),
  542. uintptr(uFlags))
  543. return ret != 0
  544. }
  545. func FillRect(hDC HDC, lprc *RECT, hbr HBRUSH) bool {
  546. ret, _, _ := procFillRect.Call(
  547. uintptr(hDC),
  548. uintptr(unsafe.Pointer(lprc)),
  549. uintptr(hbr))
  550. return ret != 0
  551. }
  552. func DrawText(hDC HDC, text string, uCount int, lpRect *RECT, uFormat uint) int {
  553. ret, _, _ := procDrawText.Call(
  554. uintptr(hDC),
  555. uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
  556. uintptr(uCount),
  557. uintptr(unsafe.Pointer(lpRect)),
  558. uintptr(uFormat))
  559. return int(ret)
  560. }
  561. func AddClipboardFormatListener(hwnd HWND) bool {
  562. ret, _, _ := procAddClipboardFormatListener.Call(
  563. uintptr(hwnd))
  564. return ret != 0
  565. }
  566. func RemoveClipboardFormatListener(hwnd HWND) bool {
  567. ret, _, _ := procRemoveClipboardFormatListener.Call(
  568. uintptr(hwnd))
  569. return ret != 0
  570. }
  571. func OpenClipboard(hWndNewOwner HWND) bool {
  572. ret, _, _ := procOpenClipboard.Call(
  573. uintptr(hWndNewOwner))
  574. return ret != 0
  575. }
  576. func CloseClipboard() bool {
  577. ret, _, _ := procCloseClipboard.Call()
  578. return ret != 0
  579. }
  580. func EnumClipboardFormats(format uint) uint {
  581. ret, _, _ := procEnumClipboardFormats.Call(
  582. uintptr(format))
  583. return uint(ret)
  584. }
  585. func GetClipboardData(uFormat uint) HANDLE {
  586. ret, _, _ := procGetClipboardData.Call(
  587. uintptr(uFormat))
  588. return HANDLE(ret)
  589. }
  590. func SetClipboardData(uFormat uint, hMem HANDLE) HANDLE {
  591. ret, _, _ := procSetClipboardData.Call(
  592. uintptr(uFormat),
  593. uintptr(hMem))
  594. return HANDLE(ret)
  595. }
  596. func EmptyClipboard() bool {
  597. ret, _, _ := procEmptyClipboard.Call()
  598. return ret != 0
  599. }
  600. func GetClipboardFormatName(format uint) (string, bool) {
  601. cchMaxCount := 255
  602. buf := make([]uint16, cchMaxCount)
  603. ret, _, _ := procGetClipboardFormatName.Call(
  604. uintptr(format),
  605. uintptr(unsafe.Pointer(&buf[0])),
  606. uintptr(cchMaxCount))
  607. if ret > 0 {
  608. return syscall.UTF16ToString(buf), true
  609. }
  610. return "Requested format does not exist or is predefined", false
  611. }
  612. func IsClipboardFormatAvailable(format uint) bool {
  613. ret, _, _ := procIsClipboardFormatAvailable.Call(uintptr(format))
  614. return ret != 0
  615. }
  616. func BeginPaint(hwnd HWND, paint *PAINTSTRUCT) HDC {
  617. ret, _, _ := procBeginPaint.Call(
  618. uintptr(hwnd),
  619. uintptr(unsafe.Pointer(paint)))
  620. return HDC(ret)
  621. }
  622. func EndPaint(hwnd HWND, paint *PAINTSTRUCT) {
  623. procBeginPaint.Call(
  624. uintptr(hwnd),
  625. uintptr(unsafe.Pointer(paint)))
  626. }
  627. func GetKeyboardState(lpKeyState *[]byte) bool {
  628. ret, _, _ := procGetKeyboardState.Call(
  629. uintptr(unsafe.Pointer(&(*lpKeyState)[0])))
  630. return ret != 0
  631. }
  632. func MapVirtualKeyEx(uCode, uMapType uint, dwhkl HKL) uint {
  633. ret, _, _ := procMapVirtualKey.Call(
  634. uintptr(uCode),
  635. uintptr(uMapType),
  636. uintptr(dwhkl))
  637. return uint(ret)
  638. }
  639. func GetAsyncKeyState(vKey int) uint16 {
  640. ret, _, _ := procGetAsyncKeyState.Call(uintptr(vKey))
  641. return uint16(ret)
  642. }
  643. func ToAscii(uVirtKey, uScanCode uint, lpKeyState *byte, lpChar *uint16, uFlags uint) int {
  644. ret, _, _ := procToAscii.Call(
  645. uintptr(uVirtKey),
  646. uintptr(uScanCode),
  647. uintptr(unsafe.Pointer(lpKeyState)),
  648. uintptr(unsafe.Pointer(lpChar)),
  649. uintptr(uFlags))
  650. return int(ret)
  651. }
  652. func SwapMouseButton(fSwap bool) bool {
  653. ret, _, _ := procSwapMouseButton.Call(
  654. uintptr(BoolToBOOL(fSwap)))
  655. return ret != 0
  656. }
  657. func GetCursorPos() (x, y int, ok bool) {
  658. pt := POINT{}
  659. ret, _, _ := procGetCursorPos.Call(uintptr(unsafe.Pointer(&pt)))
  660. return int(pt.X), int(pt.Y), ret != 0
  661. }
  662. func SetCursorPos(x, y int) bool {
  663. ret, _, _ := procSetCursorPos.Call(
  664. uintptr(x),
  665. uintptr(y),
  666. )
  667. return ret != 0
  668. }
  669. func SetCursor(cursor HCURSOR) HCURSOR {
  670. ret, _, _ := procSetCursor.Call(
  671. uintptr(cursor),
  672. )
  673. return HCURSOR(ret)
  674. }
  675. func CreateIcon(instance HINSTANCE, nWidth, nHeight int, cPlanes, cBitsPerPixel byte, ANDbits, XORbits *byte) HICON {
  676. ret, _, _ := procCreateIcon.Call(
  677. uintptr(instance),
  678. uintptr(nWidth),
  679. uintptr(nHeight),
  680. uintptr(cPlanes),
  681. uintptr(cBitsPerPixel),
  682. uintptr(unsafe.Pointer(ANDbits)),
  683. uintptr(unsafe.Pointer(XORbits)),
  684. )
  685. return HICON(ret)
  686. }
  687. func DestroyIcon(icon HICON) bool {
  688. ret, _, _ := procDestroyIcon.Call(
  689. uintptr(icon),
  690. )
  691. return ret != 0
  692. }
  693. func MonitorFromPoint(x, y int, dwFlags uint32) HMONITOR {
  694. ret, _, _ := procMonitorFromPoint.Call(
  695. uintptr(x),
  696. uintptr(y),
  697. uintptr(dwFlags),
  698. )
  699. return HMONITOR(ret)
  700. }
  701. func MonitorFromRect(rc *RECT, dwFlags uint32) HMONITOR {
  702. ret, _, _ := procMonitorFromRect.Call(
  703. uintptr(unsafe.Pointer(rc)),
  704. uintptr(dwFlags),
  705. )
  706. return HMONITOR(ret)
  707. }
  708. func MonitorFromWindow(hwnd HWND, dwFlags uint32) HMONITOR {
  709. ret, _, _ := procMonitorFromWindow.Call(
  710. uintptr(hwnd),
  711. uintptr(dwFlags),
  712. )
  713. return HMONITOR(ret)
  714. }
  715. func GetMonitorInfo(hMonitor HMONITOR, lmpi *MONITORINFO) bool {
  716. ret, _, _ := procGetMonitorInfo.Call(
  717. uintptr(hMonitor),
  718. uintptr(unsafe.Pointer(lmpi)),
  719. )
  720. return ret != 0
  721. }
  722. func EnumDisplayMonitors(hdc HDC, clip *RECT, fnEnum, dwData uintptr) bool {
  723. ret, _, _ := procEnumDisplayMonitors.Call(
  724. uintptr(hdc),
  725. uintptr(unsafe.Pointer(clip)),
  726. fnEnum,
  727. dwData,
  728. )
  729. return ret != 0
  730. }
  731. func EnumDisplaySettingsEx(szDeviceName *uint16, iModeNum uint32, devMode *DEVMODE, dwFlags uint32) bool {
  732. ret, _, _ := procEnumDisplaySettingsEx.Call(
  733. uintptr(unsafe.Pointer(szDeviceName)),
  734. uintptr(iModeNum),
  735. uintptr(unsafe.Pointer(devMode)),
  736. uintptr(dwFlags),
  737. )
  738. return ret != 0
  739. }
  740. func ChangeDisplaySettingsEx(szDeviceName *uint16, devMode *DEVMODE, hwnd HWND, dwFlags uint32, lParam uintptr) int32 {
  741. ret, _, _ := procChangeDisplaySettingsEx.Call(
  742. uintptr(unsafe.Pointer(szDeviceName)),
  743. uintptr(unsafe.Pointer(devMode)),
  744. uintptr(hwnd),
  745. uintptr(dwFlags),
  746. lParam,
  747. )
  748. return int32(ret)
  749. }
  750. /* remove to build without cgo
  751. func SendInput(inputs []INPUT) uint32 {
  752. var validInputs []C.INPUT
  753. for _, oneInput := range inputs {
  754. input := C.INPUT{_type: C.DWORD(oneInput.Type)}
  755. switch oneInput.Type {
  756. case INPUT_MOUSE:
  757. (*MouseInput)(unsafe.Pointer(&input)).mi = oneInput.Mi
  758. case INPUT_KEYBOARD:
  759. (*KbdInput)(unsafe.Pointer(&input)).ki = oneInput.Ki
  760. case INPUT_HARDWARE:
  761. (*HardwareInput)(unsafe.Pointer(&input)).hi = oneInput.Hi
  762. default:
  763. panic("unkown type")
  764. }
  765. validInputs = append(validInputs, input)
  766. }
  767. ret, _, _ := procSendInput.Call(
  768. uintptr(len(validInputs)),
  769. uintptr(unsafe.Pointer(&validInputs[0])),
  770. uintptr(unsafe.Sizeof(C.INPUT{})),
  771. )
  772. return uint32(ret)
  773. }
  774. */