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