예제 #1
0
    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
예제 #2
0
 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())
예제 #3
0
 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
예제 #4
0
 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'
예제 #5
0
 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())
예제 #6
0
 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())
예제 #7
0
 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())
예제 #8
0
 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
예제 #9
0
 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())
예제 #10
0
    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
예제 #11
0
 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())
예제 #12
0
 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())
예제 #13
0
 def SetWindowPosAndSize(self, x, y, w, h):
     if not user32.MoveWindow(self.Hwnd, x, y, w, h, 1):
         raise WinError(GetLastError())
예제 #14
0
 def GetWindowRect(self):
     rc = RECT()
     if user32.GetWindowRect(self.Hwnd, byref(rc)): return rc
     raise WinError(GetLastError())
예제 #15
0
 def ReleaseMouseCapture(self):
     if not user32.ReleaseCapture():
         raise WinError(GetLastError())
예제 #16
0
 def SetText(self, text):
     # Todo: Limit text lenght ??"""
     if not user32.SetWindowTextA(self.Hwnd, text):
         raise WinError(GetLastError())
예제 #17
0
                    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
예제 #18
0
 def UnregisterHotkey(ID):
     if not user32.UnregisterHotKey(self.Hwnd, ID):
         raise WinError(GetLastError())
예제 #19
0
 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())
예제 #20
0
 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
예제 #21
0
 def GetClassInfo(self, classname):
     if not user32.GetClassInfoExA(None, classname, byref(self)):
         raise WinError(GetLastError())
예제 #22
0
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())
예제 #23
0
 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())
예제 #24
0
 def SetCursorPos(self, x, y):
     if not user32.SetCursorPos(x, y): raise WinError(GetLastError())
예제 #25
0
 def GetDlgItem(self, ID):
     result = user32.GetDlgItem(self.Hwnd, ID)
     if result: return result
     raise WinError(GetLastError())
예제 #26
0
 def GetCursorPos(self):
     pt = POINT()
     if user32.GetCursorPos(byref(pt)): return pt.x, pt.y
     raise WinError(GetLastError())