def Find(self): winClassMatch = self.winClassMatch winNameMatch = self.winNameMatch programMatch = self.programMatch includeInvisible = self.includeInvisible topWindowsHwnds = [ hwnd for hwnd in GetTopLevelWindowList(includeInvisible) if ( winClassMatch(GetClassName(hwnd)) and winNameMatch(GetWindowText(hwnd)) ) ] if self.program: topWindowsHwnds = [ hwnd for hwnd in topWindowsHwnds if programMatch(GetWindowProcessName(hwnd).upper()) ] if not self.scanChilds: return topWindowsHwnds childClassMatch = self.childClassMatch childNameMatch = self.childNameMatch childHwnds = [ childHwnd for parentHwnd in topWindowsHwnds for childHwnd in GetWindowChildsList( parentHwnd, includeInvisible ) if ( childClassMatch(GetClassName(childHwnd)) and childNameMatch(GetWindowText(childHwnd)) ) ] return childHwnds
def AppendPrograms(self): self.pids.clear() processes = EnumProcesses() # get PID list for pid in processes: self.pids[pid] = [] hwnds = GetTopLevelWindowList(self.includeInvisible) for hwnd in hwnds: pid = GetWindowThreadProcessId(hwnd)[1] if pid == eg.processId: continue self.pids[pid].append(hwnd) for pid in processes: if len(self.pids[pid]) == 0: continue iconIndex = 0 for hwnd in self.pids[pid]: icon = GetHwndIcon(hwnd) if icon: iconIndex = self.imageList.AddIcon(icon) break exe = GetProcessName(pid) item = self.AppendItem(self.root, exe) self.SetItemHasChildren(item, True) self.SetPyData(item, pid) self.SetItemImage(item, iconIndex, which=wx.TreeItemIcon_Normal)
def EnumProcesses(): pids = {} hwnds = {} dwProcessId = DWORD() for hwnd in GetTopLevelWindowList(False): GetWindowThreadProcessId(hwnd, byref(dwProcessId)) pid = dwProcessId.value if pid not in pids: processInfo = ProcessInfo(pid) pids[pid] = processInfo else: processInfo = pids[pid] processInfo.hwnds[hwnd] = WindowInfo(hwnd) hwnds[hwnd] = processInfo return pids, hwnds
def EnumProcesses(): names = {} hwnds = {} dwProcessId = DWORD() for hwnd in GetTopLevelWindowList(False): GetWindowThreadProcessId(hwnd, byref(dwProcessId)) pid = dwProcessId.value name = splitext(GetProcessName(pid))[0] if name not in names: processInfo = ProcessInfo(name) names[name] = processInfo else: processInfo = names[name] processInfo.hwnds.add(hwnd) hwnds[hwnd] = processInfo return names, hwnds
def OnSelectionChanged(self, event): event.Skip() tree = self.tree item = tree.GetSelection() if not item.IsOk(): return hwnd = tree.GetPyData(item) eg.PrintDebugNotice("HWND:", hwnd) if tree.GetItemParent(item) == tree.root: # only selected a program pid = hwnd hwnd = None else: pid = GetWindowThreadProcessId(hwnd)[1] if pid == self.lastPid and hwnd == self.lastHwnd: return self.lastPid = pid self.lastHwnd = hwnd exe = GetProcessName(pid) def SetOption(flag, option, value): checkBox, textCtrl = option checkBox.SetValue(flag) textCtrl.SetValue(value) textCtrl.Enable(flag) rootHwnd = None options = self.options SetOption(bool(exe), options[0], exe) if hwnd is not None: rootHwnd = GetAncestor(hwnd, GA_ROOT) targetWinName = GetWindowText(rootHwnd) targetWinClass = GetClassName(rootHwnd) SetOption(True, options[1], targetWinName) SetOption(True, options[2], targetWinClass) else: SetOption(False, options[1], "") SetOption(False, options[2], "") if rootHwnd is not None and rootHwnd != hwnd: targetWinName = GetWindowText(hwnd) targetWinClass = GetClassName(hwnd) SetOption(True, options[3], targetWinName) SetOption(True, options[4], targetWinClass) searchHwnd = hwnd data = [ child for child in GetWindowChildsList( rootHwnd, tree.includeInvisible ) if ( GetClassName(child) == targetWinClass and GetWindowText(child) == targetWinName ) ] try: count = data.index(searchHwnd) + 1 except: count = 0 else: SetOption(False, options[3], "") SetOption(False, options[4], "") if rootHwnd is not None: data = [ hwnd for hwnd in GetTopLevelWindowList(tree.includeInvisible) # NOQA if ( GetClassName(hwnd) == targetWinClass and GetWindowText(hwnd) != targetWinName and GetWindowThreadProcessId(hwnd)[1] == pid ) ] count = len(data) else: count = 0 SetOption(count > 0, options[5], count or 1)