def topWindow(hwnd): hwndList = [hwnd] while True: hwnd = getParentWindow(hwnd) if not hwnd: break hwndList.append(hwnd) topHwnd = None while len(hwndList) > 0: hwnd = hwndList.pop() if not isRawWindow(hwnd): topHwnd = hwnd break if not topHwnd: return False place = autoutil.tryExcept(win32gui.GetWindowPlacement, topHwnd) if autoutil.isExcept(place): return False if place[1] == win32con.SW_SHOWMINIMIZED and not showWindow(topHwnd): return False hwnd = win32gui.GetForegroundWindow() if hwnd == topHwnd: return True rst = autoutil.tryExcept(win32gui.SetForegroundWindow, topHwnd) if not autoutil.isExcept(rst): return True return getWindowClass(hwnd) == 'Progman' and getWindowText(hwnd) == 'Program Manager'
def getProcessGDI(pid): proc = autoutil.tryExcept(win32api.OpenProcess, win32con.PROCESS_ALL_ACCESS, 0, pid) if autoutil.isExcept(proc): return gdi = autoutil.tryExcept(win32process.GetGuiResources, proc.handle, win32con.GR_GDIOBJECTS) autoutil.tryExcept(proc.close) if not autoutil.isExcept(gdi): return gdi
def __getChildWindows__(hwnd, hwnds): hwnds.add(hwnd) rst = set() hcwnd = autoutil.tryExcept(win32gui.GetWindow, hwnd or win32gui.GetDesktopWindow(), win32con.GW_CHILD) while hcwnd and not autoutil.isExcept(hcwnd) and hcwnd not in hwnds: __fillWindowAttrs__(hcwnd, rst) if hwnd: rst.update(__getChildWindows__(hcwnd, hwnds)) hcwnd = autoutil.tryExcept(win32gui.GetWindow, hcwnd, win32con.GW_HWNDNEXT) return rst
def __findChildWindows__(hwnd, hwnds): hwnds.add(hwnd) rst = set() hcwnd = autoutil.tryExcept(win32gui.FindWindowEx, hwnd, None, None, None) while hcwnd and not autoutil.isExcept(hcwnd) and hcwnd not in hwnds: __fillWindowAttrs__(hcwnd, rst) if hwnd: rst.update(__findChildWindows__(hcwnd, hwnds)) hcwnd = autoutil.tryExcept(win32gui.FindWindowEx, hwnd, hcwnd, None, None) return rst
def createProcess(cmd, wait = False): if wait: proc = autoutil.tryExcept(subprocess.Popen, cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE) else: proc = autoutil.tryExcept(subprocess.Popen, cmd) if autoutil.isExcept(proc): print cmd print 'createProcess 发生异常: %s' % str(traceback.format_exc()) return if not wait: return proc.pid proc.communicate()
def __enumChildWindows__(hwnd, hwnds): hwnds.add(hwnd) rst = set() if not hwnd: autoutil.tryExcept(win32gui.EnumWindows, __fillWindowAttrs__, rst) else: autoutil.tryExcept(win32gui.EnumChildWindows, hwnd, __fillWindowAttrs__, rst) crst = set() for hcwnd in rst: if hcwnd not in hwnds: crst.update(__enumChildWindows__(hcwnd, hwnds)) rst.update(crst) return rst
def getUserProcessIds(user): rst, count = autoutil.queryWmi('select * from Win32_Process') pidList = [] for i in range(count): owner = autoutil.tryExcept(rst[i].ExecMethod_, 'GetOwner') if not autoutil.isExcept(owner) and owner.User and owner.User.lower() == user.lower(): pidList.append(rst[i].ProcessId) return pidList
def setWindowText(hwnd, text): rst = autoutil.tryExcept(win32gui.SendMessageTimeout, hwnd, win32con.WM_SETTEXT, 0, text, win32con.SMTO_ABORTIFHUNG, 30) return not autoutil.isExcept(rst)
def getWindowText(hwnd, buf = ctypes.create_string_buffer(1024)): size = ctypes.sizeof(buf) ctypes.memset(buf, 0, size) autoutil.tryExcept(win32gui.SendMessageTimeout, hwnd, win32con.WM_GETTEXT, size, buf, win32con.SMTO_ABORTIFHUNG, 30) return buf.value.strip()
def getWindowRect(hwnd): rect = autoutil.tryExcept(win32gui.GetWindowRect, hwnd) if not autoutil.isExcept(rect): return rect
def getParentWindow(hwnd): hwnd = autoutil.tryExcept(win32gui.GetParent, hwnd) if not autoutil.isExcept(hwnd): return hwnd
def getCmdLineById(pid): rst,count= autoutil.queryWmi('select * from Win32_Process where ProcessId=%s' % pid) if count: cmdLine = autoutil.tryExcept(rst[0].CommandLine.encode, sys.getfilesystemencoding()) if not autoutil.isExcept(cmdLine): return cmdLine
def getProcessNameById(pid): rst, count = autoutil.queryWmi('select * from Win32_Process where ProcessId = %d' % pid) if count: pname = autoutil.tryExcept(rst[0].Name.encode, sys.getfilesystemencoding()) if not autoutil.isExcept(pname): return pname