def DeferWindows(self, *windows): handle = user32.BeginDeferWindowPos(len(windows)) if handle: try: for wnd, x, y, w, h in windows: flag = 4 | 16 # SWP_NOZORDER|SWP_NOACTIVATE if x == None or y == None: flag |= 2 # SWP_NOSIZE if w == None or h == None: flag |= 1 # SWP_NOMOVE if isinstance(wnd, (int, long)): newHandle = user32.DeferWindowPos( handle, wnd, 0, x, y, w, h, flag) else: newHandle = user32.DeferWindowPos( handle, wnd.Hwnd, 0, x, y, w, h, flag) if newHandle: handle = newHandle else: raise '' except Exception, d: user32.EndDeferWindowPos(handle) if GetLastError(): raise WinError(GetLastError()) else: raise RuntimeError, d if user32.EndDeferWindowPos(handle): return
def GetWindow(self, flag): ## never use GetWindow in a loop !! It will most likely cause infinite recursion ## not implemented #GW_OWNER = 4 #GW_CHILD = 5 #GW_ENABLEDPOPUP = 6 try: flag = ('first', 'last', 'next', 'prev').index(flag) except: raise ValueError("invalid flag: %s" % flag) result = user32.GetWindow(self.Hwnd, flag) if result: return result if GetLastError(): raise WinError(GetLastError())
def Register(self): # pretty useless to return the atom here, cos XP does not accept # class atoms in CreateWindowEx, we'll see self.atom = user32.RegisterClassExA(byref(self)) if not self.atom: raise WinError(GetLastError()) return self.atom
def HitTest(self): pt = POINT() if not user32.GetCursorPos(byref(pt)): raise WinError(GetLastError()) hittest = self.DefWindowProc(self.Hwnd, self.Msg.WM_NCHITTEST, 0, MAKELONG(pt.x, pt.y)) try: return HT_FLAGS[hittest] except: return 'unknown'
def SetTimer(self, ID, nTimeout): # TODO: find out about timer ID's used by the system # if not ID: raise ValueError("invalid ID: %s" % ID) if ID in self._base_timers: raise RuntimeError("timer alreaddy set: %s" % ID) else: self._base_timers.append(ID) if not user32.SetTimer(self.Hwnd, ID, nTimeout, None): raise WinError(GetLastError())
def RegisterHotkey(self, ID, vk, *modkeys): flags = {'alt': 1, 'control': 2, 'shift': 4, 'win': 8} modkey = 0 if modkeys: for i in modkeys: try: modkey |= flags[i] except: raise ValueError, "invalid modkey flag: %s" % i result = user32.RegisterHotKey(self.Hwnd, ID, modkey, vk) if not result: raise WinError(GetLastError())
def KillTimer(self, ID): # TODO # # would ne nice to dump all open timers to TrackHandler # when the window is closed for a status report result = user32.KillTimer(self.Hwnd, ID) try: self._base_timers.remove(ID) except: raise IndexError, "no such timer registered: %s" % ID if not result: raise WinError(GetLastError())
def ShowWindows(self, *windows): # SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_SHOWWINDOW|SWP_NOCOPYBITS|SWP_SHOWWINDOW flag = 1 | 2 | 4 | 128 | 256 | 64 handle = user32.BeginDeferWindowPos(len(windows)) if handle: try: for i in windows: if isinstance(i, (int, long)): newHandle = user32.DeferWindowPos( handle, i, 0, 0, 0, 0, 0, flag) else: newHandle = user32.DeferWindowPos( handle, i.Hwnd, 0, 0, 0, 0, 0, flag) if newHandle: handle = newHandle else: raise '' except Exception, d: user32.EndDeferWindowPos(handle) if GetLastError(): raise WinError(GetLastError()) else: raise RuntimeError, d if user32.EndDeferWindowPos(handle): return
def Beep(self, msg='default'): try: msg = { 'ok': 0, 'hand': 16, 'question': 32, 'exclamation': 48, 'asterisk': 64, 'default': 0xFFFFFFFF }[msg] except: raise ValueError("invalid beep message: %s" % msg) if not user32.MessageBeep(msg): raise WinError(GetLastError())
def ForceSingleInstance(self, Bool, restore=True, flag=None): if Bool: if self.GetGUID(): ERROR_ACCESS_DENIED = 5 ERROR_ALREADY_EXISTS = 183 fRunning = False if self._base_mutext_singleinst: fRunning = True else: if flag: if flag in ('desktop', 'session', 'trustee'): self._base_mutext_singleinst = kernel32.CreateMutexA( None, 0, CreateExclusionName(self._base_guid, flag)) else: raise ValueError, "invalid flag: %s" % flag else: self._base_mutext_singleinst = kernel32.CreateMutexA( None, 0, self._base_guid) if GetLastError() in (ERROR_ALREADY_EXISTS, ERROR_ACCESS_DENIED): kernel32.CloseHandle(self._base_mutext_singleinst) self._base_mutext_singleinst = None fRunning = True if fRunning: if restore: ## take advantage of copydata FindGUID method here from wnd.api import copydata cd = copydata.CopyData() hwnd = cd.FindGUID(self.Hwnd, self.GetGUID()) if hwnd: user32.SetForegroundWindow(hwnd) if user32.IsIconic(hwnd): user32.ShowWindow(hwnd, 9) # SW_RESTORE self.Close() raise RuntimeError, "single instance enforced" else: raise RuntimeError, "GUID is required" else: if self._base_mutext_singleinst: kernel32.CloseHandle(self._base_mutext_singleinst) self._base_mutext_singleinst = None
def SetWindowSize(self, w, h): # SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE if not user32.SetWindowPos(self.Hwnd, 0, 0, 0, w, h, 2 | 4 | 16): raise WinError(GetLastError())
def SetWindowPos(self, x, y): # SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE if not user32.SetWindowPos(self.Hwnd, 0, x, y, 0, 0, 1 | 4 | 16): raise WinError(GetLastError())
def SetWindowPosAndSize(self, x, y, w, h): if not user32.MoveWindow(self.Hwnd, x, y, w, h, 1): raise WinError(GetLastError())
def GetWindowRect(self): rc = RECT() if user32.GetWindowRect(self.Hwnd, byref(rc)): return rc raise WinError(GetLastError())
def ReleaseMouseCapture(self): if not user32.ReleaseCapture(): raise WinError(GetLastError())
def SetText(self, text): # Todo: Limit text lenght ??""" if not user32.SetWindowTextA(self.Hwnd, text): raise WinError(GetLastError())
if isinstance(i, (int, long)): newHandle = user32.DeferWindowPos( handle, i, 0, 0, 0, 0, 0, flag) else: newHandle = user32.DeferWindowPos( handle, i.Hwnd, 0, 0, 0, 0, 0, flag) if newHandle: handle = newHandle else: raise '' except Exception, d: user32.EndDeferWindowPos(handle) if GetLastError(): raise WinError(GetLastError()) else: raise RuntimeError, d if user32.EndDeferWindowPos(handle): return raise WinError(GetLastError()) def ShowWindows(self, *windows): # SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_SHOWWINDOW|SWP_NOCOPYBITS|SWP_SHOWWINDOW flag = 1 | 2 | 4 | 128 | 256 | 64 handle = user32.BeginDeferWindowPos(len(windows)) if handle: try: for i in windows: if isinstance(i, (int, long)): newHandle = user32.DeferWindowPos( handle, i, 0, 0, 0, 0, 0, flag) else: newHandle = user32.DeferWindowPos( handle, i.Hwnd, 0, 0, 0, 0, 0, flag) if newHandle: handle = newHandle
def UnregisterHotkey(ID): if not user32.UnregisterHotKey(self.Hwnd, ID): raise WinError(GetLastError())
def RedrawFrame(self): # SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE| SWP_NOZORDER if not user32.SetWindowPos(self.Hwnd, 0, 0, 0, 0, 0, 1 | 2 | 4 | 16 | 32): raise WinError(GetLastError())
def GetClassName(self): p = create_string_buffer(fw.WND_MAX_CLASSNAME + 1) result = user32.GetClassNameA(self.Hwnd, p, fw.WND_MAX_CLASSNAME + 1) if not result: if GetLastError(): raise WinError(GetLastError()) return p.value
def GetClassInfo(self, classname): if not user32.GetClassInfoExA(None, classname, byref(self)): raise WinError(GetLastError())
class _CommonMethods: CallWindowProc = user32.CallWindowProcA SendMessage = user32.SendMessageA PostMessage = user32.PostMessageA ## NEW METHOD #******************************************************************** # Helper method to reflect a message to a control sending it # # The control will receive a WND_MSGREFLECT mesage with wParam # set to the hwnd of the caller and lParam set to the address of a # MSGREFLECT structure. # The control the message is send to should set the 'fReturn' member # to 1 to indicate that the return value is to be returned. # If its 0 this method calls DefWindowProc. # # This mechanism enshures default processing for anonymous # system classes ASAP # #******************************************************************* def GetGUID(self): return self._base_guid def SetGUID(self, guid): self._base_guid = guid def ThreadWindows(self, flag=None): return _EnumWindows.ThreadWindows(self.Hwnd, flag) def ChildWindows(self, flag=None): return _EnumWindows.ChildWindows(self.Hwnd) def WalkGui(self, hwnd=None, topdown=True): # get top level window if hwnd == None: hwndParent = user32.GetParent(self.Hwnd) if not hwndParent: hwnd = self.Hwnd while hwndParent: hwnd = hwndParent hwndParent = user32.GetParent(hwndParent) children= [i for i in _EnumWindows.ChildWindows(hwnd) \ if user32.GetParent(i)==hwnd] if topdown: yield hwnd, children for i in children: for x in self.WalkGui(i): yield x if not topdown: yield hwnd, children def DeferWindows(self, *windows): handle = user32.BeginDeferWindowPos(len(windows)) if handle: try: for wnd, x, y, w, h in windows: flag = 4 | 16 # SWP_NOZORDER|SWP_NOACTIVATE if x == None or y == None: flag |= 2 # SWP_NOSIZE if w == None or h == None: flag |= 1 # SWP_NOMOVE if isinstance(wnd, (int, long)): newHandle = user32.DeferWindowPos( handle, wnd, 0, x, y, w, h, flag) else: newHandle = user32.DeferWindowPos( handle, wnd.Hwnd, 0, x, y, w, h, flag) if newHandle: handle = newHandle else: raise '' except Exception, d: user32.EndDeferWindowPos(handle) if GetLastError(): raise WinError(GetLastError()) else: raise RuntimeError, d if user32.EndDeferWindowPos(handle): return raise WinError(GetLastError())
def SetBackgroundWindow(self, hwnd=None): if not hwnd: hwnd = self.Hwnd HWND_BOTTOM = 1 #SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE if not user32.SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, 1 | 2 | 16): raise WinError(GetLastError())
def SetCursorPos(self, x, y): if not user32.SetCursorPos(x, y): raise WinError(GetLastError())
def GetDlgItem(self, ID): result = user32.GetDlgItem(self.Hwnd, ID) if result: return result raise WinError(GetLastError())
def GetCursorPos(self): pt = POINT() if user32.GetCursorPos(byref(pt)): return pt.x, pt.y raise WinError(GetLastError())