1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 """
32 Wrapper for user32.dll in ctypes.
33 """
34
35 __revision__ = "$Id: user32.py 1299 2013-12-20 09:30:55Z qvasimodo $"
36
37 from defines import *
38 from version import bits
39 from kernel32 import GetLastError, SetLastError
40 from gdi32 import POINT, PPOINT, LPPOINT, RECT, PRECT, LPRECT
41
42
43
44 _all = None
45 _all = set(vars().keys())
51 """
52 Convert arguments to the WPARAM type.
53 Used automatically by SendMessage, PostMessage, etc.
54 You shouldn't need to call this function.
55 """
56 wParam = ctypes.cast(wParam, LPVOID).value
57 if wParam is None:
58 wParam = 0
59 return wParam
60
62 """
63 Convert arguments to the LPARAM type.
64 Used automatically by SendMessage, PostMessage, etc.
65 You shouldn't need to call this function.
66 """
67 return ctypes.cast(lParam, LPARAM)
68
70 """
71 Window enumerator class. Used internally by the window enumeration APIs.
72 """
79
80
81
82 WNDENUMPROC = WINFUNCTYPE(BOOL, HWND, PVOID)
83
84
85
86 HWND_DESKTOP = 0
87 HWND_TOP = 1
88 HWND_BOTTOM = 1
89 HWND_TOPMOST = -1
90 HWND_NOTOPMOST = -2
91 HWND_MESSAGE = -3
92
93
94 GWL_WNDPROC = -4
95 GWL_HINSTANCE = -6
96 GWL_HWNDPARENT = -8
97 GWL_ID = -12
98 GWL_STYLE = -16
99 GWL_EXSTYLE = -20
100 GWL_USERDATA = -21
101
102
103 GWLP_WNDPROC = GWL_WNDPROC
104 GWLP_HINSTANCE = GWL_HINSTANCE
105 GWLP_HWNDPARENT = GWL_HWNDPARENT
106 GWLP_STYLE = GWL_STYLE
107 GWLP_EXSTYLE = GWL_EXSTYLE
108 GWLP_USERDATA = GWL_USERDATA
109 GWLP_ID = GWL_ID
110
111
112 SW_HIDE = 0
113 SW_SHOWNORMAL = 1
114 SW_NORMAL = 1
115 SW_SHOWMINIMIZED = 2
116 SW_SHOWMAXIMIZED = 3
117 SW_MAXIMIZE = 3
118 SW_SHOWNOACTIVATE = 4
119 SW_SHOW = 5
120 SW_MINIMIZE = 6
121 SW_SHOWMINNOACTIVE = 7
122 SW_SHOWNA = 8
123 SW_RESTORE = 9
124 SW_SHOWDEFAULT = 10
125 SW_FORCEMINIMIZE = 11
126
127
128 SMTO_NORMAL = 0
129 SMTO_BLOCK = 1
130 SMTO_ABORTIFHUNG = 2
131 SMTO_NOTIMEOUTIFNOTHUNG = 8
132 SMTO_ERRORONEXIT = 0x20
133
134
135 WPF_SETMINPOSITION = 1
136 WPF_RESTORETOMAXIMIZED = 2
137 WPF_ASYNCWINDOWPLACEMENT = 4
138
139
140 GA_PARENT = 1
141 GA_ROOT = 2
142 GA_ROOTOWNER = 3
143
144
145 GW_HWNDFIRST = 0
146 GW_HWNDLAST = 1
147 GW_HWNDNEXT = 2
148 GW_HWNDPREV = 3
149 GW_OWNER = 4
150 GW_CHILD = 5
151 GW_ENABLEDPOPUP = 6
152
153
154
155 WM_USER = 0x400
156 WM_APP = 0x800
157
158 WM_NULL = 0
159 WM_CREATE = 1
160 WM_DESTROY = 2
161 WM_MOVE = 3
162 WM_SIZE = 5
163 WM_ACTIVATE = 6
164 WA_INACTIVE = 0
165 WA_ACTIVE = 1
166 WA_CLICKACTIVE = 2
167 WM_SETFOCUS = 7
168 WM_KILLFOCUS = 8
169 WM_ENABLE = 0x0A
170 WM_SETREDRAW = 0x0B
171 WM_SETTEXT = 0x0C
172 WM_GETTEXT = 0x0D
173 WM_GETTEXTLENGTH = 0x0E
174 WM_PAINT = 0x0F
175 WM_CLOSE = 0x10
176 WM_QUERYENDSESSION = 0x11
177 WM_QUIT = 0x12
178 WM_QUERYOPEN = 0x13
179 WM_ERASEBKGND = 0x14
180 WM_SYSCOLORCHANGE = 0x15
181 WM_ENDSESSION = 0x16
182 WM_SHOWWINDOW = 0x18
183 WM_WININICHANGE = 0x1A
184 WM_SETTINGCHANGE = WM_WININICHANGE
185 WM_DEVMODECHANGE = 0x1B
186 WM_ACTIVATEAPP = 0x1C
187 WM_FONTCHANGE = 0x1D
188 WM_TIMECHANGE = 0x1E
189 WM_CANCELMODE = 0x1F
190 WM_SETCURSOR = 0x20
191 WM_MOUSEACTIVATE = 0x21
192 WM_CHILDACTIVATE = 0x22
193 WM_QUEUESYNC = 0x23
194 WM_GETMINMAXINFO = 0x24
195 WM_PAINTICON = 0x26
196 WM_ICONERASEBKGND = 0x27
197 WM_NEXTDLGCTL = 0x28
198 WM_SPOOLERSTATUS = 0x2A
199 WM_DRAWITEM = 0x2B
200 WM_MEASUREITEM = 0x2C
201 WM_DELETEITEM = 0x2D
202 WM_VKEYTOITEM = 0x2E
203 WM_CHARTOITEM = 0x2F
204 WM_SETFONT = 0x30
205 WM_GETFONT = 0x31
206 WM_SETHOTKEY = 0x32
207 WM_GETHOTKEY = 0x33
208 WM_QUERYDRAGICON = 0x37
209 WM_COMPAREITEM = 0x39
210 WM_GETOBJECT = 0x3D
211 WM_COMPACTING = 0x41
212 WM_OTHERWINDOWCREATED = 0x42
213 WM_OTHERWINDOWDESTROYED = 0x43
214 WM_COMMNOTIFY = 0x44
215
216 CN_RECEIVE = 0x1
217 CN_TRANSMIT = 0x2
218 CN_EVENT = 0x4
219
220 WM_WINDOWPOSCHANGING = 0x46
221 WM_WINDOWPOSCHANGED = 0x47
222 WM_POWER = 0x48
223
224 PWR_OK = 1
225 PWR_FAIL = -1
226 PWR_SUSPENDREQUEST = 1
227 PWR_SUSPENDRESUME = 2
228 PWR_CRITICALRESUME = 3
229
230 WM_COPYDATA = 0x4A
231 WM_CANCELJOURNAL = 0x4B
232 WM_NOTIFY = 0x4E
233 WM_INPUTLANGCHANGEREQUEST = 0x50
234 WM_INPUTLANGCHANGE = 0x51
235 WM_TCARD = 0x52
236 WM_HELP = 0x53
237 WM_USERCHANGED = 0x54
238 WM_NOTIFYFORMAT = 0x55
239 WM_CONTEXTMENU = 0x7B
240 WM_STYLECHANGING = 0x7C
241 WM_STYLECHANGED = 0x7D
242 WM_DISPLAYCHANGE = 0x7E
243 WM_GETICON = 0x7F
244 WM_SETICON = 0x80
245 WM_NCCREATE = 0x81
246 WM_NCDESTROY = 0x82
247 WM_NCCALCSIZE = 0x83
248 WM_NCHITTEST = 0x84
249 WM_NCPAINT = 0x85
250 WM_NCACTIVATE = 0x86
251 WM_GETDLGCODE = 0x87
252 WM_SYNCPAINT = 0x88
253 WM_NCMOUSEMOVE = 0x0A0
254 WM_NCLBUTTONDOWN = 0x0A1
255 WM_NCLBUTTONUP = 0x0A2
256 WM_NCLBUTTONDBLCLK = 0x0A3
257 WM_NCRBUTTONDOWN = 0x0A4
258 WM_NCRBUTTONUP = 0x0A5
259 WM_NCRBUTTONDBLCLK = 0x0A6
260 WM_NCMBUTTONDOWN = 0x0A7
261 WM_NCMBUTTONUP = 0x0A8
262 WM_NCMBUTTONDBLCLK = 0x0A9
263 WM_KEYFIRST = 0x100
264 WM_KEYDOWN = 0x100
265 WM_KEYUP = 0x101
266 WM_CHAR = 0x102
267 WM_DEADCHAR = 0x103
268 WM_SYSKEYDOWN = 0x104
269 WM_SYSKEYUP = 0x105
270 WM_SYSCHAR = 0x106
271 WM_SYSDEADCHAR = 0x107
272 WM_KEYLAST = 0x108
273 WM_INITDIALOG = 0x110
274 WM_COMMAND = 0x111
275 WM_SYSCOMMAND = 0x112
276 WM_TIMER = 0x113
277 WM_HSCROLL = 0x114
278 WM_VSCROLL = 0x115
279 WM_INITMENU = 0x116
280 WM_INITMENUPOPUP = 0x117
281 WM_MENUSELECT = 0x11F
282 WM_MENUCHAR = 0x120
283 WM_ENTERIDLE = 0x121
284 WM_CTLCOLORMSGBOX = 0x132
285 WM_CTLCOLOREDIT = 0x133
286 WM_CTLCOLORLISTBOX = 0x134
287 WM_CTLCOLORBTN = 0x135
288 WM_CTLCOLORDLG = 0x136
289 WM_CTLCOLORSCROLLBAR = 0x137
290 WM_CTLCOLORSTATIC = 0x138
291 WM_MOUSEFIRST = 0x200
292 WM_MOUSEMOVE = 0x200
293 WM_LBUTTONDOWN = 0x201
294 WM_LBUTTONUP = 0x202
295 WM_LBUTTONDBLCLK = 0x203
296 WM_RBUTTONDOWN = 0x204
297 WM_RBUTTONUP = 0x205
298 WM_RBUTTONDBLCLK = 0x206
299 WM_MBUTTONDOWN = 0x207
300 WM_MBUTTONUP = 0x208
301 WM_MBUTTONDBLCLK = 0x209
302 WM_MOUSELAST = 0x209
303 WM_PARENTNOTIFY = 0x210
304 WM_ENTERMENULOOP = 0x211
305 WM_EXITMENULOOP = 0x212
306 WM_MDICREATE = 0x220
307 WM_MDIDESTROY = 0x221
308 WM_MDIACTIVATE = 0x222
309 WM_MDIRESTORE = 0x223
310 WM_MDINEXT = 0x224
311 WM_MDIMAXIMIZE = 0x225
312 WM_MDITILE = 0x226
313 WM_MDICASCADE = 0x227
314 WM_MDIICONARRANGE = 0x228
315 WM_MDIGETACTIVE = 0x229
316 WM_MDISETMENU = 0x230
317 WM_DROPFILES = 0x233
318 WM_MDIREFRESHMENU = 0x234
319 WM_CUT = 0x300
320 WM_COPY = 0x301
321 WM_PASTE = 0x302
322 WM_CLEAR = 0x303
323 WM_UNDO = 0x304
324 WM_RENDERFORMAT = 0x305
325 WM_RENDERALLFORMATS = 0x306
326 WM_DESTROYCLIPBOARD = 0x307
327 WM_DRAWCLIPBOARD = 0x308
328 WM_PAINTCLIPBOARD = 0x309
329 WM_VSCROLLCLIPBOARD = 0x30A
330 WM_SIZECLIPBOARD = 0x30B
331 WM_ASKCBFORMATNAME = 0x30C
332 WM_CHANGECBCHAIN = 0x30D
333 WM_HSCROLLCLIPBOARD = 0x30E
334 WM_QUERYNEWPALETTE = 0x30F
335 WM_PALETTEISCHANGING = 0x310
336 WM_PALETTECHANGED = 0x311
337 WM_HOTKEY = 0x312
338 WM_PRINT = 0x317
339 WM_PRINTCLIENT = 0x318
340 WM_PENWINFIRST = 0x380
341 WM_PENWINLAST = 0x38F
342
343
344
345
346
347
348
349
350
351
352
353 -class WINDOWPLACEMENT(Structure):
354 _fields_ = [
355 ('length', UINT),
356 ('flags', UINT),
357 ('showCmd', UINT),
358 ('ptMinPosition', POINT),
359 ('ptMaxPosition', POINT),
360 ('rcNormalPosition', RECT),
361 ]
362 PWINDOWPLACEMENT = POINTER(WINDOWPLACEMENT)
363 LPWINDOWPLACEMENT = PWINDOWPLACEMENT
364
365
366
367
368
369
370
371
372
373
374
375
376 -class GUITHREADINFO(Structure):
377 _fields_ = [
378 ('cbSize', DWORD),
379 ('flags', DWORD),
380 ('hwndActive', HWND),
381 ('hwndFocus', HWND),
382 ('hwndCapture', HWND),
383 ('hwndMenuOwner', HWND),
384 ('hwndMoveSize', HWND),
385 ('hwndCaret', HWND),
386 ('rcCaret', RECT),
387 ]
388 PGUITHREADINFO = POINTER(GUITHREADINFO)
389 LPGUITHREADINFO = PGUITHREADINFO
390
391
392
393
394
395
396
397
398
399 -class Point(object):
400 """
401 Python wrapper over the L{POINT} class.
402
403 @type x: int
404 @ivar x: Horizontal coordinate
405 @type y: int
406 @ivar y: Vertical coordinate
407 """
408
410 """
411 @see: L{POINT}
412 @type x: int
413 @param x: Horizontal coordinate
414 @type y: int
415 @param y: Vertical coordinate
416 """
417 self.x = x
418 self.y = y
419
422
425
427 return (self.x, self.y) [index]
428
430 if index == 0:
431 self.x = value
432 elif index == 1:
433 self.y = value
434 else:
435 raise IndexError("index out of range")
436
437 @property
439 """
440 Compatibility with ctypes.
441 Allows passing transparently a Point object to an API call.
442 """
443 return POINT(self.x, self.y)
444
446 """
447 Translates window screen coordinates to client coordinates.
448
449 @see: L{client_to_screen}, L{translate}
450
451 @type hWnd: int or L{HWND} or L{system.Window}
452 @param hWnd: Window handle.
453
454 @rtype: L{Point}
455 @return: New object containing the translated coordinates.
456 """
457 return ScreenToClient(hWnd, self)
458
460 """
461 Translates window client coordinates to screen coordinates.
462
463 @see: L{screen_to_client}, L{translate}
464
465 @type hWnd: int or L{HWND} or L{system.Window}
466 @param hWnd: Window handle.
467
468 @rtype: L{Point}
469 @return: New object containing the translated coordinates.
470 """
471 return ClientToScreen(hWnd, self)
472
474 """
475 Translate coordinates from one window to another.
476
477 @note: To translate multiple points it's more efficient to use the
478 L{MapWindowPoints} function instead.
479
480 @see: L{client_to_screen}, L{screen_to_client}
481
482 @type hWndFrom: int or L{HWND} or L{system.Window}
483 @param hWndFrom: Window handle to translate from.
484 Use C{HWND_DESKTOP} for screen coordinates.
485
486 @type hWndTo: int or L{HWND} or L{system.Window}
487 @param hWndTo: Window handle to translate to.
488 Use C{HWND_DESKTOP} for screen coordinates.
489
490 @rtype: L{Point}
491 @return: New object containing the translated coordinates.
492 """
493 return MapWindowPoints(hWndFrom, hWndTo, [self])
494
496 """
497 Python wrapper over the L{RECT} class.
498
499 @type left: int
500 @ivar left: Horizontal coordinate for the top left corner.
501 @type top: int
502 @ivar top: Vertical coordinate for the top left corner.
503 @type right: int
504 @ivar right: Horizontal coordinate for the bottom right corner.
505 @type bottom: int
506 @ivar bottom: Vertical coordinate for the bottom right corner.
507
508 @type width: int
509 @ivar width: Width in pixels. Same as C{right - left}.
510 @type height: int
511 @ivar height: Height in pixels. Same as C{bottom - top}.
512 """
513
514 - def __init__(self, left = 0, top = 0, right = 0, bottom = 0):
515 """
516 @see: L{RECT}
517 @type left: int
518 @param left: Horizontal coordinate for the top left corner.
519 @type top: int
520 @param top: Vertical coordinate for the top left corner.
521 @type right: int
522 @param right: Horizontal coordinate for the bottom right corner.
523 @type bottom: int
524 @param bottom: Vertical coordinate for the bottom right corner.
525 """
526 self.left = left
527 self.top = top
528 self.right = right
529 self.bottom = bottom
530
533
536
539
541 if index == 0:
542 self.left = value
543 elif index == 1:
544 self.top = value
545 elif index == 2:
546 self.right = value
547 elif index == 3:
548 self.bottom = value
549 else:
550 raise IndexError, "index out of range"
551
552 @property
554 """
555 Compatibility with ctypes.
556 Allows passing transparently a Point object to an API call.
557 """
558 return RECT(self.left, self.top, self.right, self.bottom)
559
562
565
568
571
572 width = property(__get_width, __set_width)
573 height = property(__get_height, __set_height)
574
576 """
577 Translates window screen coordinates to client coordinates.
578
579 @see: L{client_to_screen}, L{translate}
580
581 @type hWnd: int or L{HWND} or L{system.Window}
582 @param hWnd: Window handle.
583
584 @rtype: L{Rect}
585 @return: New object containing the translated coordinates.
586 """
587 topleft = ScreenToClient(hWnd, (self.left, self.top))
588 bottomright = ScreenToClient(hWnd, (self.bottom, self.right))
589 return Rect( topleft.x, topleft.y, bottomright.x, bottomright.y )
590
592 """
593 Translates window client coordinates to screen coordinates.
594
595 @see: L{screen_to_client}, L{translate}
596
597 @type hWnd: int or L{HWND} or L{system.Window}
598 @param hWnd: Window handle.
599
600 @rtype: L{Rect}
601 @return: New object containing the translated coordinates.
602 """
603 topleft = ClientToScreen(hWnd, (self.left, self.top))
604 bottomright = ClientToScreen(hWnd, (self.bottom, self.right))
605 return Rect( topleft.x, topleft.y, bottomright.x, bottomright.y )
606
608 """
609 Translate coordinates from one window to another.
610
611 @see: L{client_to_screen}, L{screen_to_client}
612
613 @type hWndFrom: int or L{HWND} or L{system.Window}
614 @param hWndFrom: Window handle to translate from.
615 Use C{HWND_DESKTOP} for screen coordinates.
616
617 @type hWndTo: int or L{HWND} or L{system.Window}
618 @param hWndTo: Window handle to translate to.
619 Use C{HWND_DESKTOP} for screen coordinates.
620
621 @rtype: L{Rect}
622 @return: New object containing the translated coordinates.
623 """
624 points = [ (self.left, self.top), (self.right, self.bottom) ]
625 return MapWindowPoints(hWndFrom, hWndTo, points)
626
628 """
629 Python wrapper over the L{WINDOWPLACEMENT} class.
630 """
631
657
658 @property
677
685 _SetLastErrorEx = windll.user32.SetLastErrorEx
686 _SetLastErrorEx.argtypes = [DWORD, DWORD]
687 _SetLastErrorEx.restype = None
688 _SetLastErrorEx(dwErrCode, dwType)
689
690
691
692
693
694 -def FindWindowA(lpClassName = None, lpWindowName = None):
695 _FindWindowA = windll.user32.FindWindowA
696 _FindWindowA.argtypes = [LPSTR, LPSTR]
697 _FindWindowA.restype = HWND
698
699 hWnd = _FindWindowA(lpClassName, lpWindowName)
700 if not hWnd:
701 errcode = GetLastError()
702 if errcode != ERROR_SUCCESS:
703 raise ctypes.WinError(errcode)
704 return hWnd
705
706 -def FindWindowW(lpClassName = None, lpWindowName = None):
707 _FindWindowW = windll.user32.FindWindowW
708 _FindWindowW.argtypes = [LPWSTR, LPWSTR]
709 _FindWindowW.restype = HWND
710
711 hWnd = _FindWindowW(lpClassName, lpWindowName)
712 if not hWnd:
713 errcode = GetLastError()
714 if errcode != ERROR_SUCCESS:
715 raise ctypes.WinError(errcode)
716 return hWnd
717
718 FindWindow = GuessStringType(FindWindowA, FindWindowW)
719
720
721
722
723
724
725
726 -def FindWindowExA(hwndParent = None, hwndChildAfter = None, lpClassName = None, lpWindowName = None):
727 _FindWindowExA = windll.user32.FindWindowExA
728 _FindWindowExA.argtypes = [HWND, HWND, LPSTR, LPSTR]
729 _FindWindowExA.restype = HWND
730
731 hWnd = _FindWindowExA(hwndParent, hwndChildAfter, lpClassName, lpWindowName)
732 if not hWnd:
733 errcode = GetLastError()
734 if errcode != ERROR_SUCCESS:
735 raise ctypes.WinError(errcode)
736 return hWnd
737
738 -def FindWindowExW(hwndParent = None, hwndChildAfter = None, lpClassName = None, lpWindowName = None):
739 _FindWindowExW = windll.user32.FindWindowExW
740 _FindWindowExW.argtypes = [HWND, HWND, LPWSTR, LPWSTR]
741 _FindWindowExW.restype = HWND
742
743 hWnd = _FindWindowExW(hwndParent, hwndChildAfter, lpClassName, lpWindowName)
744 if not hWnd:
745 errcode = GetLastError()
746 if errcode != ERROR_SUCCESS:
747 raise ctypes.WinError(errcode)
748 return hWnd
749
750 FindWindowEx = GuessStringType(FindWindowExA, FindWindowExW)
758 _GetClassNameA = windll.user32.GetClassNameA
759 _GetClassNameA.argtypes = [HWND, LPSTR, ctypes.c_int]
760 _GetClassNameA.restype = ctypes.c_int
761
762 nMaxCount = 0x1000
763 dwCharSize = sizeof(CHAR)
764 while 1:
765 lpClassName = ctypes.create_string_buffer("", nMaxCount)
766 nCount = _GetClassNameA(hWnd, lpClassName, nMaxCount)
767 if nCount == 0:
768 raise ctypes.WinError()
769 if nCount < nMaxCount - dwCharSize:
770 break
771 nMaxCount += 0x1000
772 return lpClassName.value
773
775 _GetClassNameW = windll.user32.GetClassNameW
776 _GetClassNameW.argtypes = [HWND, LPWSTR, ctypes.c_int]
777 _GetClassNameW.restype = ctypes.c_int
778
779 nMaxCount = 0x1000
780 dwCharSize = sizeof(WCHAR)
781 while 1:
782 lpClassName = ctypes.create_unicode_buffer(u"", nMaxCount)
783 nCount = _GetClassNameW(hWnd, lpClassName, nMaxCount)
784 if nCount == 0:
785 raise ctypes.WinError()
786 if nCount < nMaxCount - dwCharSize:
787 break
788 nMaxCount += 0x1000
789 return lpClassName.value
790
791 GetClassName = GuessStringType(GetClassNameA, GetClassNameW)
792
793
794
795
796
797
798 -def GetWindowTextA(hWnd):
799 _GetWindowTextA = windll.user32.GetWindowTextA
800 _GetWindowTextA.argtypes = [HWND, LPSTR, ctypes.c_int]
801 _GetWindowTextA.restype = ctypes.c_int
802
803 nMaxCount = 0x1000
804 dwCharSize = sizeof(CHAR)
805 while 1:
806 lpString = ctypes.create_string_buffer("", nMaxCount)
807 nCount = _GetWindowTextA(hWnd, lpString, nMaxCount)
808 if nCount == 0:
809 raise ctypes.WinError()
810 if nCount < nMaxCount - dwCharSize:
811 break
812 nMaxCount += 0x1000
813 return lpString.value
814
815 -def GetWindowTextW(hWnd):
816 _GetWindowTextW = windll.user32.GetWindowTextW
817 _GetWindowTextW.argtypes = [HWND, LPWSTR, ctypes.c_int]
818 _GetWindowTextW.restype = ctypes.c_int
819
820 nMaxCount = 0x1000
821 dwCharSize = sizeof(CHAR)
822 while 1:
823 lpString = ctypes.create_string_buffer("", nMaxCount)
824 nCount = _GetWindowTextW(hWnd, lpString, nMaxCount)
825 if nCount == 0:
826 raise ctypes.WinError()
827 if nCount < nMaxCount - dwCharSize:
828 break
829 nMaxCount += 0x1000
830 return lpString.value
831
832 GetWindowText = GuessStringType(GetWindowTextA, GetWindowTextW)
833
834
835
836
837
838 -def SetWindowTextA(hWnd, lpString = None):
839 _SetWindowTextA = windll.user32.SetWindowTextA
840 _SetWindowTextA.argtypes = [HWND, LPSTR]
841 _SetWindowTextA.restype = bool
842 _SetWindowTextA.errcheck = RaiseIfZero
843 _SetWindowTextA(hWnd, lpString)
844
845 -def SetWindowTextW(hWnd, lpString = None):
846 _SetWindowTextW = windll.user32.SetWindowTextW
847 _SetWindowTextW.argtypes = [HWND, LPWSTR]
848 _SetWindowTextW.restype = bool
849 _SetWindowTextW.errcheck = RaiseIfZero
850 _SetWindowTextW(hWnd, lpString)
851
852 SetWindowText = GuessStringType(SetWindowTextA, SetWindowTextW)
870
883
884 GetWindowLong = DefaultStringType(GetWindowLongA, GetWindowLongW)
885
886
887
888
889
890
891 if bits == 32:
892
893 GetWindowLongPtrA = GetWindowLongA
894 GetWindowLongPtrW = GetWindowLongW
895 GetWindowLongPtr = GetWindowLong
896
897 else:
900 _GetWindowLongPtrA = windll.user32.GetWindowLongPtrA
901 _GetWindowLongPtrA.argtypes = [HWND, ctypes.c_int]
902 _GetWindowLongPtrA.restype = SIZE_T
903
904 SetLastError(ERROR_SUCCESS)
905 retval = _GetWindowLongPtrA(hWnd, nIndex)
906 if retval == 0:
907 errcode = GetLastError()
908 if errcode != ERROR_SUCCESS:
909 raise ctypes.WinError(errcode)
910 return retval
911
913 _GetWindowLongPtrW = windll.user32.GetWindowLongPtrW
914 _GetWindowLongPtrW.argtypes = [HWND, ctypes.c_int]
915 _GetWindowLongPtrW.restype = DWORD
916
917 SetLastError(ERROR_SUCCESS)
918 retval = _GetWindowLongPtrW(hWnd, nIndex)
919 if retval == 0:
920 errcode = GetLastError()
921 if errcode != ERROR_SUCCESS:
922 raise ctypes.WinError(errcode)
923 return retval
924
925 GetWindowLongPtr = DefaultStringType(GetWindowLongPtrA, GetWindowLongPtrW)
926
927
928
929
930
931
932
933 -def SetWindowLongA(hWnd, nIndex, dwNewLong):
945
958
959 SetWindowLong = DefaultStringType(SetWindowLongA, SetWindowLongW)
960
961
962
963
964
965
966
967 if bits == 32:
968
969 SetWindowLongPtrA = SetWindowLongA
970 SetWindowLongPtrW = SetWindowLongW
971 SetWindowLongPtr = SetWindowLong
972
973 else:
976 _SetWindowLongPtrA = windll.user32.SetWindowLongPtrA
977 _SetWindowLongPtrA.argtypes = [HWND, ctypes.c_int, SIZE_T]
978 _SetWindowLongPtrA.restype = SIZE_T
979
980 SetLastError(ERROR_SUCCESS)
981 retval = _SetWindowLongPtrA(hWnd, nIndex, dwNewLong)
982 if retval == 0:
983 errcode = GetLastError()
984 if errcode != ERROR_SUCCESS:
985 raise ctypes.WinError(errcode)
986 return retval
987
989 _SetWindowLongPtrW = windll.user32.SetWindowLongPtrW
990 _SetWindowLongPtrW.argtypes = [HWND, ctypes.c_int, SIZE_T]
991 _SetWindowLongPtrW.restype = SIZE_T
992
993 SetLastError(ERROR_SUCCESS)
994 retval = _SetWindowLongPtrW(hWnd, nIndex, dwNewLong)
995 if retval == 0:
996 errcode = GetLastError()
997 if errcode != ERROR_SUCCESS:
998 raise ctypes.WinError(errcode)
999 return retval
1000
1001 SetWindowLongPtr = DefaultStringType(SetWindowLongPtrA, SetWindowLongPtrW)
1005 _GetShellWindow = windll.user32.GetShellWindow
1006 _GetShellWindow.argtypes = []
1007 _GetShellWindow.restype = HWND
1008 _GetShellWindow.errcheck = RaiseIfZero
1009 return _GetShellWindow()
1010
1024
1025
1026
1027
1028
1029 -def GetWindow(hWnd, uCmd):
1041
1057
1074
1075
1076
1077
1078
1079 -def EnableWindow(hWnd, bEnable = True):
1080 _EnableWindow = windll.user32.EnableWindow
1081 _EnableWindow.argtypes = [HWND, BOOL]
1082 _EnableWindow.restype = bool
1083 return _EnableWindow(hWnd, bool(bEnable))
1084
1090 _ShowWindow = windll.user32.ShowWindow
1091 _ShowWindow.argtypes = [HWND, ctypes.c_int]
1092 _ShowWindow.restype = bool
1093 return _ShowWindow(hWnd, nCmdShow)
1094
1100 _ShowWindowAsync = windll.user32.ShowWindowAsync
1101 _ShowWindowAsync.argtypes = [HWND, ctypes.c_int]
1102 _ShowWindowAsync.restype = bool
1103 return _ShowWindowAsync(hWnd, nCmdShow)
1104
1107 _GetDesktopWindow = windll.user32.GetDesktopWindow
1108 _GetDesktopWindow.argtypes = []
1109 _GetDesktopWindow.restype = HWND
1110 _GetDesktopWindow.errcheck = RaiseIfZero
1111 return _GetDesktopWindow()
1112
1115 _GetForegroundWindow = windll.user32.GetForegroundWindow
1116 _GetForegroundWindow.argtypes = []
1117 _GetForegroundWindow.restype = HWND
1118 _GetForegroundWindow.errcheck = RaiseIfZero
1119 return _GetForegroundWindow()
1120
1125 _IsWindow = windll.user32.IsWindow
1126 _IsWindow.argtypes = [HWND]
1127 _IsWindow.restype = bool
1128 return _IsWindow(hWnd)
1129
1134 _IsWindowVisible = windll.user32.IsWindowVisible
1135 _IsWindowVisible.argtypes = [HWND]
1136 _IsWindowVisible.restype = bool
1137 return _IsWindowVisible(hWnd)
1138
1143 _IsWindowEnabled = windll.user32.IsWindowEnabled
1144 _IsWindowEnabled.argtypes = [HWND]
1145 _IsWindowEnabled.restype = bool
1146 return _IsWindowEnabled(hWnd)
1147
1152 _IsZoomed = windll.user32.IsZoomed
1153 _IsZoomed.argtypes = [HWND]
1154 _IsZoomed.restype = bool
1155 return _IsZoomed(hWnd)
1156
1161 _IsIconic = windll.user32.IsIconic
1162 _IsIconic.argtypes = [HWND]
1163 _IsIconic.restype = bool
1164 return _IsIconic(hWnd)
1165
1170 _IsChild = windll.user32.IsChild
1171 _IsChild.argtypes = [HWND]
1172 _IsChild.restype = bool
1173 return _IsChild(hWnd)
1174
1179 _WindowFromPoint = windll.user32.WindowFromPoint
1180 _WindowFromPoint.argtypes = [POINT]
1181 _WindowFromPoint.restype = HWND
1182 _WindowFromPoint.errcheck = RaiseIfZero
1183 if isinstance(point, tuple):
1184 point = POINT(*point)
1185 return _WindowFromPoint(point)
1186
1192 _ChildWindowFromPoint = windll.user32.ChildWindowFromPoint
1193 _ChildWindowFromPoint.argtypes = [HWND, POINT]
1194 _ChildWindowFromPoint.restype = HWND
1195 _ChildWindowFromPoint.errcheck = RaiseIfZero
1196 if isinstance(point, tuple):
1197 point = POINT(*point)
1198 return _ChildWindowFromPoint(hWndParent, point)
1199
1205 _RealChildWindowFromPoint = windll.user32.RealChildWindowFromPoint
1206 _RealChildWindowFromPoint.argtypes = [HWND, POINT]
1207 _RealChildWindowFromPoint.restype = HWND
1208 _RealChildWindowFromPoint.errcheck = RaiseIfZero
1209 if isinstance(ptParentClientCoords, tuple):
1210 ptParentClientCoords = POINT(*ptParentClientCoords)
1211 return _RealChildWindowFromPoint(hWndParent, ptParentClientCoords)
1212
1218 _ScreenToClient = windll.user32.ScreenToClient
1219 _ScreenToClient.argtypes = [HWND, LPPOINT]
1220 _ScreenToClient.restype = bool
1221 _ScreenToClient.errcheck = RaiseIfZero
1222
1223 if isinstance(lpPoint, tuple):
1224 lpPoint = POINT(*lpPoint)
1225 else:
1226 lpPoint = POINT(lpPoint.x, lpPoint.y)
1227 _ScreenToClient(hWnd, byref(lpPoint))
1228 return Point(lpPoint.x, lpPoint.y)
1229
1235 _ClientToScreen = windll.user32.ClientToScreen
1236 _ClientToScreen.argtypes = [HWND, LPPOINT]
1237 _ClientToScreen.restype = bool
1238 _ClientToScreen.errcheck = RaiseIfZero
1239
1240 if isinstance(lpPoint, tuple):
1241 lpPoint = POINT(*lpPoint)
1242 else:
1243 lpPoint = POINT(lpPoint.x, lpPoint.y)
1244 _ClientToScreen(hWnd, byref(lpPoint))
1245 return Point(lpPoint.x, lpPoint.y)
1246
1247
1248
1249
1250
1251
1252
1253 -def MapWindowPoints(hWndFrom, hWndTo, lpPoints):
1254 _MapWindowPoints = windll.user32.MapWindowPoints
1255 _MapWindowPoints.argtypes = [HWND, HWND, LPPOINT, UINT]
1256 _MapWindowPoints.restype = ctypes.c_int
1257
1258 cPoints = len(lpPoints)
1259 lpPoints = (POINT * cPoints)(* lpPoints)
1260 SetLastError(ERROR_SUCCESS)
1261 number = _MapWindowPoints(hWndFrom, hWndTo, byref(lpPoints), cPoints)
1262 if number == 0:
1263 errcode = GetLastError()
1264 if errcode != ERROR_SUCCESS:
1265 raise ctypes.WinError(errcode)
1266 x_delta = number & 0xFFFF
1267 y_delta = (number >> 16) & 0xFFFF
1268 return x_delta, y_delta, [ (Point.x, Point.y) for Point in lpPoints ]
1269
1274 _SetForegroundWindow = windll.user32.SetForegroundWindow
1275 _SetForegroundWindow.argtypes = [HWND]
1276 _SetForegroundWindow.restype = bool
1277 _SetForegroundWindow.errcheck = RaiseIfZero
1278 return _SetForegroundWindow(hWnd)
1279
1294
1308
1314 _GetWindowRect = windll.user32.GetWindowRect
1315 _GetWindowRect.argtypes = [HWND, LPRECT]
1316 _GetWindowRect.restype = bool
1317 _GetWindowRect.errcheck = RaiseIfZero
1318
1319 lpRect = RECT()
1320 _GetWindowRect(hWnd, byref(lpRect))
1321 return Rect(lpRect.left, lpRect.top, lpRect.right, lpRect.bottom)
1322
1328 _GetClientRect = windll.user32.GetClientRect
1329 _GetClientRect.argtypes = [HWND, LPRECT]
1330 _GetClientRect.restype = bool
1331 _GetClientRect.errcheck = RaiseIfZero
1332
1333 lpRect = RECT()
1334 _GetClientRect(hWnd, byref(lpRect))
1335 return Rect(lpRect.left, lpRect.top, lpRect.right, lpRect.bottom)
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345 -def MoveWindow(hWnd, X, Y, nWidth, nHeight, bRepaint = True):
1346 _MoveWindow = windll.user32.MoveWindow
1347 _MoveWindow.argtypes = [HWND, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, BOOL]
1348 _MoveWindow.restype = bool
1349 _MoveWindow.errcheck = RaiseIfZero
1350 _MoveWindow(hWnd, X, Y, nWidth, nHeight, bool(bRepaint))
1351
1357 _GetGUIThreadInfo = windll.user32.GetGUIThreadInfo
1358 _GetGUIThreadInfo.argtypes = [DWORD, LPGUITHREADINFO]
1359 _GetGUIThreadInfo.restype = bool
1360 _GetGUIThreadInfo.errcheck = RaiseIfZero
1361
1362 gui = GUITHREADINFO()
1363 _GetGUIThreadInfo(idThread, byref(gui))
1364 return gui
1365
1372
1389
1396
1414
1421
1440
1441
1442
1443
1444
1445
1446
1447 -def SendMessageA(hWnd, Msg, wParam = 0, lParam = 0):
1448 _SendMessageA = windll.user32.SendMessageA
1449 _SendMessageA.argtypes = [HWND, UINT, WPARAM, LPARAM]
1450 _SendMessageA.restype = LRESULT
1451
1452 wParam = MAKE_WPARAM(wParam)
1453 lParam = MAKE_LPARAM(lParam)
1454 return _SendMessageA(hWnd, Msg, wParam, lParam)
1455
1457 _SendMessageW = windll.user32.SendMessageW
1458 _SendMessageW.argtypes = [HWND, UINT, WPARAM, LPARAM]
1459 _SendMessageW.restype = LRESULT
1460
1461 wParam = MAKE_WPARAM(wParam)
1462 lParam = MAKE_LPARAM(lParam)
1463 return _SendMessageW(hWnd, Msg, wParam, lParam)
1464
1465 SendMessage = GuessStringType(SendMessageA, SendMessageW)
1466
1467
1468
1469
1470
1471
1472
1473 -def PostMessageA(hWnd, Msg, wParam = 0, lParam = 0):
1474 _PostMessageA = windll.user32.PostMessageA
1475 _PostMessageA.argtypes = [HWND, UINT, WPARAM, LPARAM]
1476 _PostMessageA.restype = bool
1477 _PostMessageA.errcheck = RaiseIfZero
1478
1479 wParam = MAKE_WPARAM(wParam)
1480 lParam = MAKE_LPARAM(lParam)
1481 _PostMessageA(hWnd, Msg, wParam, lParam)
1482
1483 -def PostMessageW(hWnd, Msg, wParam = 0, lParam = 0):
1484 _PostMessageW = windll.user32.PostMessageW
1485 _PostMessageW.argtypes = [HWND, UINT, WPARAM, LPARAM]
1486 _PostMessageW.restype = bool
1487 _PostMessageW.errcheck = RaiseIfZero
1488
1489 wParam = MAKE_WPARAM(wParam)
1490 lParam = MAKE_LPARAM(lParam)
1491 _PostMessageW(hWnd, Msg, wParam, lParam)
1492
1493 PostMessage = GuessStringType(PostMessageA, PostMessageW)
1494
1495
1496
1497
1498
1499
1500
1501 -def PostThreadMessageA(idThread, Msg, wParam = 0, lParam = 0):
1502 _PostThreadMessageA = windll.user32.PostThreadMessageA
1503 _PostThreadMessageA.argtypes = [DWORD, UINT, WPARAM, LPARAM]
1504 _PostThreadMessageA.restype = bool
1505 _PostThreadMessageA.errcheck = RaiseIfZero
1506
1507 wParam = MAKE_WPARAM(wParam)
1508 lParam = MAKE_LPARAM(lParam)
1509 _PostThreadMessageA(idThread, Msg, wParam, lParam)
1510
1511 -def PostThreadMessageW(idThread, Msg, wParam = 0, lParam = 0):
1512 _PostThreadMessageW = windll.user32.PostThreadMessageW
1513 _PostThreadMessageW.argtypes = [DWORD, UINT, WPARAM, LPARAM]
1514 _PostThreadMessageW.restype = bool
1515 _PostThreadMessageW.errcheck = RaiseIfZero
1516
1517 wParam = MAKE_WPARAM(wParam)
1518 lParam = MAKE_LPARAM(lParam)
1519 _PostThreadMessageW(idThread, Msg, wParam, lParam)
1520
1521 PostThreadMessage = GuessStringType(PostThreadMessageA, PostThreadMessageW)
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532 -def SendMessageTimeoutA(hWnd, Msg, wParam = 0, lParam = 0, fuFlags = 0, uTimeout = 0):
1533 _SendMessageTimeoutA = windll.user32.SendMessageTimeoutA
1534 _SendMessageTimeoutA.argtypes = [HWND, UINT, WPARAM, LPARAM, UINT, UINT, PDWORD_PTR]
1535 _SendMessageTimeoutA.restype = LRESULT
1536 _SendMessageTimeoutA.errcheck = RaiseIfZero
1537
1538 wParam = MAKE_WPARAM(wParam)
1539 lParam = MAKE_LPARAM(lParam)
1540 dwResult = DWORD(0)
1541 _SendMessageTimeoutA(hWnd, Msg, wParam, lParam, fuFlags, uTimeout, byref(dwResult))
1542 return dwResult.value
1543
1545 _SendMessageTimeoutW = windll.user32.SendMessageTimeoutW
1546 _SendMessageTimeoutW.argtypes = [HWND, UINT, WPARAM, LPARAM, UINT, UINT, PDWORD_PTR]
1547 _SendMessageTimeoutW.restype = LRESULT
1548 _SendMessageTimeoutW.errcheck = RaiseIfZero
1549
1550 wParam = MAKE_WPARAM(wParam)
1551 lParam = MAKE_LPARAM(lParam)
1552 dwResult = DWORD(0)
1553 _SendMessageTimeoutW(hWnd, Msg, wParam, lParam, fuFlags, uTimeout, byref(dwResult))
1554 return dwResult.value
1555
1556 SendMessageTimeout = GuessStringType(SendMessageTimeoutA, SendMessageTimeoutW)
1557
1558
1559
1560
1561
1562
1563
1564 -def SendNotifyMessageA(hWnd, Msg, wParam = 0, lParam = 0):
1565 _SendNotifyMessageA = windll.user32.SendNotifyMessageA
1566 _SendNotifyMessageA.argtypes = [HWND, UINT, WPARAM, LPARAM]
1567 _SendNotifyMessageA.restype = bool
1568 _SendNotifyMessageA.errcheck = RaiseIfZero
1569
1570 wParam = MAKE_WPARAM(wParam)
1571 lParam = MAKE_LPARAM(lParam)
1572 _SendNotifyMessageA(hWnd, Msg, wParam, lParam)
1573
1575 _SendNotifyMessageW = windll.user32.SendNotifyMessageW
1576 _SendNotifyMessageW.argtypes = [HWND, UINT, WPARAM, LPARAM]
1577 _SendNotifyMessageW.restype = bool
1578 _SendNotifyMessageW.errcheck = RaiseIfZero
1579
1580 wParam = MAKE_WPARAM(wParam)
1581 lParam = MAKE_LPARAM(lParam)
1582 _SendNotifyMessageW(hWnd, Msg, wParam, lParam)
1583
1584 SendNotifyMessage = GuessStringType(SendNotifyMessageA, SendNotifyMessageW)
1585
1586
1587
1588
1589
1590
1591
1592
1593 -def SendDlgItemMessageA(hDlg, nIDDlgItem, Msg, wParam = 0, lParam = 0):
1594 _SendDlgItemMessageA = windll.user32.SendDlgItemMessageA
1595 _SendDlgItemMessageA.argtypes = [HWND, ctypes.c_int, UINT, WPARAM, LPARAM]
1596 _SendDlgItemMessageA.restype = LRESULT
1597
1598 wParam = MAKE_WPARAM(wParam)
1599 lParam = MAKE_LPARAM(lParam)
1600 return _SendDlgItemMessageA(hDlg, nIDDlgItem, Msg, wParam, lParam)
1601
1603 _SendDlgItemMessageW = windll.user32.SendDlgItemMessageW
1604 _SendDlgItemMessageW.argtypes = [HWND, ctypes.c_int, UINT, WPARAM, LPARAM]
1605 _SendDlgItemMessageW.restype = LRESULT
1606
1607 wParam = MAKE_WPARAM(wParam)
1608 lParam = MAKE_LPARAM(lParam)
1609 return _SendDlgItemMessageW(hDlg, nIDDlgItem, Msg, wParam, lParam)
1610
1611 SendDlgItemMessage = GuessStringType(SendDlgItemMessageA, SendDlgItemMessageW)
1626
1631 _RegisterWindowMessageA = windll.user32.RegisterWindowMessageA
1632 _RegisterWindowMessageA.argtypes = [LPSTR]
1633 _RegisterWindowMessageA.restype = UINT
1634 _RegisterWindowMessageA.errcheck = RaiseIfZero
1635 return _RegisterWindowMessageA(lpString)
1636
1638 _RegisterWindowMessageW = windll.user32.RegisterWindowMessageW
1639 _RegisterWindowMessageW.argtypes = [LPWSTR]
1640 _RegisterWindowMessageW.restype = UINT
1641 _RegisterWindowMessageW.errcheck = RaiseIfZero
1642 return _RegisterWindowMessageW(lpString)
1643
1644 RegisterWindowMessage = GuessStringType(RegisterWindowMessageA, RegisterWindowMessageW)
1655
1662
1663 RegisterClipboardFormat = GuessStringType(RegisterClipboardFormatA, RegisterClipboardFormatW)
1664
1665
1666
1667
1668
1669 -def GetPropA(hWnd, lpString):
1670 _GetPropA = windll.user32.GetPropA
1671 _GetPropA.argtypes = [HWND, LPSTR]
1672 _GetPropA.restype = HANDLE
1673 return _GetPropA(hWnd, lpString)
1674
1676 _GetPropW = windll.user32.GetPropW
1677 _GetPropW.argtypes = [HWND, LPWSTR]
1678 _GetPropW.restype = HANDLE
1679 return _GetPropW(hWnd, lpString)
1680
1681 GetProp = GuessStringType(GetPropA, GetPropW)
1682
1683
1684
1685
1686
1687
1688 -def SetPropA(hWnd, lpString, hData):
1689 _SetPropA = windll.user32.SetPropA
1690 _SetPropA.argtypes = [HWND, LPSTR, HANDLE]
1691 _SetPropA.restype = BOOL
1692 _SetPropA.errcheck = RaiseIfZero
1693 _SetPropA(hWnd, lpString, hData)
1694
1696 _SetPropW = windll.user32.SetPropW
1697 _SetPropW.argtypes = [HWND, LPWSTR, HANDLE]
1698 _SetPropW.restype = BOOL
1699 _SetPropW.errcheck = RaiseIfZero
1700 _SetPropW(hWnd, lpString, hData)
1701
1702 SetProp = GuessStringType(SetPropA, SetPropW)
1703
1704
1705
1706
1707
1708 -def RemovePropA(hWnd, lpString):
1709 _RemovePropA = windll.user32.RemovePropA
1710 _RemovePropA.argtypes = [HWND, LPSTR]
1711 _RemovePropA.restype = HANDLE
1712 return _RemovePropA(hWnd, lpString)
1713
1715 _RemovePropW = windll.user32.RemovePropW
1716 _RemovePropW.argtypes = [HWND, LPWSTR]
1717 _RemovePropW.restype = HANDLE
1718 return _RemovePropW(hWnd, lpString)
1719
1720 RemoveProp = GuessStringType(RemovePropA, RemovePropW)
1721
1722
1723
1724 _all = set(vars().keys()).difference(_all)
1725 __all__ = [_x for _x in _all if not _x.startswith('_')]
1726 __all__.sort()
1727
1728