def _get_control_infos(self, seconds=3): """获取焦点所在应用的所有窗体控件,并便利输出控件信息 Args: seconds (int, optional): 点击后n秒开始分析. Defaults to 3. Yields: [tuple]: 窗体上所有控件的信息 """ time.sleep(seconds) control = auto.GetFocusedControl() if control: for c, d in auto.WalkControl(control, True): mlog.instance().logger.info(c.ControlTypeName + " " + str(d)) yield ( c.ControlType, c.ClassName, "[{},{},{},{}]".format( c.BoundingRectangle.left, c.BoundingRectangle.top, c.BoundingRectangle.right, c.BoundingRectangle.bottom, ), c.Name, "0x{0:X}({0})".format(c.NativeWindowHandle), str(d), )
def CalcOnXP(): chars = '0123456789.+-*/=()' calcWindow = auto.WindowControl(searchDepth=1, ClassName='SciCalc') if not calcWindow.Exists(0, 0): subprocess.Popen('calc') calcWindow.SetActive() calcWindow.SendKeys('{Alt}vs', 0.5) clearBtn = calcWindow.ButtonControl(Name='CE') # clearBtn.Click() if 0: #Desc it not a valid search property, but it can be used for debug printing char2Button = { key: calcWindow.ButtonControl(Name=key, Desc='Button ' + key) for key in chars } else: #Run faster because it only walk calc window once char2Button = {} for c, d in auto.WalkControl(calcWindow, maxDepth=1): if c.Name in chars: char2Button[c.Name] = c Calc(calcWindow, char2Button, '1234 * (4 + 5 + 6) - 78 / 90.8') Calc(calcWindow, char2Button, '3*3+4*4') Calc(calcWindow, char2Button, '2*3.14159*10') calcWindow.Disappears(1) calcWindow.GetWindowPattern().Close() calcWindow.Exists(1)
def GetPersonDetail(): detailWindow = automation.WindowControl(searchDepth= 1, ClassName = 'TXGuiFoundation', SubName = '的资料') details = '' for control, depth in automation.WalkControl(detailWindow): if isinstance(control, automation.EditControl): details += control.Name + control.CurrentValue() + '\n' details += 'eachpersonend\n' detailWindow.Click(-10, 10) return details
def CalcOnWindows10(): """works on Windows 10.0.19042""" char2Id = { '0': 'num0Button', '1': 'num1Button', '2': 'num2Button', '3': 'num3Button', '4': 'num4Button', '5': 'num5Button', '6': 'num6Button', '7': 'num7Button', '8': 'num8Button', '9': 'num9Button', '.': 'decimalSeparatorButton', '+': 'plusButton', '-': 'minusButton', '*': 'multiplyButton', '/': 'divideButton', '=': 'equalButton', '(': 'openParenthesisButton', ')': 'closeParenthesisButton', } #Desc is not a valid search property, but it can be used for debug printing calcWindow = auto.WindowControl( searchDepth=1, ClassName='ApplicationFrameWindow', Compare=lambda c, d: c.Name == 'Calculator' or c.Name == '计算器', Desc='Calculator Window') if not calcWindow.Exists(0, 0): subprocess.Popen('calc') calcWindow.SetActive() calcWindow.ButtonControl(AutomationId='TogglePaneButton').Click() calcWindow.ListItemControl(AutomationId='Scientific').Click() calcWindow.ButtonControl(AutomationId='clearButton').Click() if 1: char2Button = { key: calcWindow.ButtonControl(AutomationId=char2Id[key], Desc='Button ' + key) for key in char2Id } else: #Run faster because it only walk calc window once id2char = {v: k for k, v in char2Id.items()} char2Button = {} for c, d in auto.WalkControl(calcWindow, maxDepth=4): if c.AutomationId in id2char: char2Button[id2char[c.AutomationId]] = c Calc(calcWindow, char2Button, '1234 * (4 + 5 + 6) - 78 / 90.8') Calc(calcWindow, char2Button, '3*3+4*4') Calc(calcWindow, char2Button, '2*3.14159*10') calcWindow.CaptureToImage( 'calc.png', 7, 0, -14, -7) # on windows 10, 7 pixels of windows border are transparent calcWindow.Disappears(1) calcWindow.GetWindowPattern().Close() calcWindow.Exists(1)
def GetPersonDetail(): detailWindow = auto.WindowControl(searchDepth=1, ClassName='TXGuiFoundation', SubName='的资料') details = '' for control, depth in auto.WalkControl(detailWindow): if isinstance(control, auto.EditControl): details += control.Name + control.GetValuePattern().Value + '\n' details += '\n' * 2 detailWindow.Click(-10, 10) return details
def main(): subprocess.Popen('mmc.exe devmgmt.msc') time.sleep(1) mmcWindow = automation.WindowControl(searchDepth = 1, ClassName = 'MMCMainFrame') tree = mmcWindow.TreeControl() for item, depth in automation.WalkControl(tree, includeTop = True): if isinstance(item, automation.TreeItemControl): #or item.ControlType == automation.ControlType.TreeItemControl item.Select() if automation.ExpandCollapseState.Collapsed == item.CurrentExpandCollapseState(): item.Expand(0) automation.Logger.WriteLine(' ' * (depth - 1) * 4 + item.Name, automation.ConsoleColor.Green) time.sleep(0.1)
def CalcOnWindows7And8(): char2Id = { '0': '130', '1': '131', '2': '132', '3': '133', '4': '134', '5': '135', '6': '136', '7': '137', '8': '138', '9': '139', '.': '84', '+': '93', '-': '94', '*': '92', '/': '91', '=': '121', '(': '128', ')': '129', } #Desc is not a valid search property, but it can be used for debug printing calcWindow = auto.WindowControl(searchDepth=1, ClassName='CalcFrame', Desc='Calculator Window') if not calcWindow.Exists(0, 0): subprocess.Popen('calc') calcWindow.SetActive() calcWindow.SendKeys('{Alt}2') clearBtn = calcWindow.ButtonControl( foundIndex=8, Depth=3) #test foundIndex and Depth, the 8th button is clear if clearBtn.Exists() and clearBtn.AutomationId == '82': clearBtn.Click() if 0: char2Button = { key: calcWindow.ButtonControl(AutomationId=char2Id[key], Desc='Button ' + key) for key in char2Id } else: #Run faster because it only walk calc window once id2char = {v: k for k, v in char2Id.items()} char2Button = {} for c, d in auto.WalkControl(calcWindow): if c.AutomationId in id2char: char2Button[id2char[c.AutomationId]] = c Calc(calcWindow, char2Button, '1234 * (4 + 5 + 6) - 78 / 90.8') Calc(calcWindow, char2Button, '3*3+4*4') Calc(calcWindow, char2Button, '2*3.14159*10') calcWindow.Disappears(1) calcWindow.GetWindowPattern().Close() calcWindow.Exists(1)
def ReadTechnicalNameFromBexQuery(self): if self.QueryWindow is None: self.QueryWindow = auto.WindowControl(searchDepth=1, RegexName="BEx Query Designer*") #self.QueryWindow.MoveCursorToInnerPos() #Mouse move (-1334,886) RCColumn = self.QueryWindow.PaneControl( searchDepth=1, Name='Rows/Columns') for c, d in auto.WalkControl(RCColumn): if isinstance(c, auto.TreeControl): self.ExpandTreeItem(c)
def walk(): wiresharkWindow = None for win in auto.GetRootControl().GetChildren(): if win.ClassName == 'MainWindow' and win.AutomationId == 'MainWindow': if win.ToolBarControl( AutomationId='MainWindow.displayFilterToolBar').Exists( 0, 0): wiresharkWindow = win break if not wiresharkWindow: auto.Logger.WriteLine('Can not find Wireshark', auto.ConsoleColor.Yellow) return console = auto.GetConsoleWindow() if console: sx, sy = auto.GetScreenSize() tp = console.GetTransformPattern() tp.Resize(sx, sy // 4) tp.Move(0, sy - sy // 4 - 50) console.SetTopmost() wiresharkWindow.SetActive(waitTime=0.1) wiresharkWindow.Maximize() auto.Logger.ColorfullyWriteLine('Press <Color=Cyan>F1</Color> to stop', auto.ConsoleColor.Yellow) tree = wiresharkWindow.TreeControl(searchDepth=4, ClassName='PacketList', Name='Packet list') rect = tree.BoundingRectangle tree.Click(y=50, waitTime=0.1) auto.SendKeys('{Home}', waitTime=0.1) columnCount = 0 treeItemCount = 0 for item, depth in auto.WalkControl(tree): if isinstance(item, auto.HeaderControl): columnCount += 1 auto.Logger.Write(item.Name + ' ') elif isinstance(item, auto.TreeItemControl): if treeItemCount % columnCount == 0: auto.Logger.Write('\n') time.sleep(0.1) treeItemCount += 1 auto.Logger.Write(item.Name + ' ') if item.BoundingRectangle.bottom >= rect.bottom: auto.SendKeys('{PageDown}', waitTime=0.1) if auto.IsKeyPressed(auto.Keys.VK_F1): auto.Logger.WriteLine('\nF1 pressed', auto.ConsoleColor.Yellow) break
def OpenQueryByName(self, queryName): if self.QueryWindow is None: self.QueryWindow = auto.WindowControl(searchDepth=1, RegexName="BEx Query Designer*") self.QueryWindow.SetActive() #self.QueryWindow.SetTopmost() b = self.QueryWindow.ToolBarControl(Name="Standard") ctrlOpen = b.MenuItemControl(Name="Open...") if ctrlOpen is None: return ctrlOpen.Click(10) dlgOpen = auto.WindowControl(RegexName="Open Query*") find = dlgOpen.ListItemControl(Name="Find") if find: find.Click(10) ctrlEdit = dlgOpen.EditControl(Name="Search Term") if ctrlEdit: ctrlEdit.SendKeys(self.CurrentQueryName,0.05) btnFind = dlgOpen.ButtonControl(Name="Find") if btnFind: btnFind.Click(5) btnOpen = dlgOpen.ButtonControl(AutomationId = "btnOK") if btnOpen: btnOpen.Click(10) trycnt = 1 while trycnt <10: try: self.QueryWindow.SetActive() trycnt = 11 except: print("screen not response") time.sleep(5) trycnt = trycnt+1 pnlInfoProvider = self.QueryWindow.PaneControl(Name="InfoProvider") if pnlInfoProvider: trycnt = 1 while trycnt < 10: try: for c, d in auto.WalkControl(pnlInfoProvider): if isinstance(c, auto.TreeControl): trycnt = 11 break except: print("screen no response! try again") time.sleep(5) trycnt = trycnt+1 # click Technical name v = self.QueryWindow.ToolBarControl(Name="View") techname = v.MenuItemControl(Name="Rows/Columns") if techname: trycnt = 1 while trycnt < 10: try: techname.Click() trycnt = 11 except: print("screen no response! try again") time.sleep(5) trycnt = trycnt+1 techname = v.MenuControl(Name="Technical Names") if techname: ischecked = False while not ischecked: trycnt = 1 while trycnt < 10: try: techname.Click() trycnt = 11 except: print("screen no response! try again") time.sleep(5) trycnt = trycnt+1 for c in techname.GetChildren(): if c.Name == "[Key] Text": value = c.GetLegacyIAccessiblePattern().State if value == 1048592: ischecked = True break
def WalkCurrentWindow(): window = automation.GetForegroundControl().GetTopWindow() for control, depth in automation.WalkControl(window, True): print(' ' * depth * 4 + str(control))
# -*- coding: utf-8 -*- import os, time, random, uiautomation import uiautomation as auto from pykeyboard import PyKeyboard from pymouse import PyMouse os.popen('Calc.exe') # mouse = PyMouse() # time.sleep(1) # mouse.click(1182,100,button=1,n=1) # for i in range(200): # x = random.randint(0,1536) # y = random.randint(45,800) # bu = random.randint(1,3) # n = random.randint(1,2) # print('x,y',x,y) # mouse.click(x,y,button=bu,n=n) calcWindow = auto.WalkControl()
def main(): sw, sh = auto.GetScreenSize() cmdWindow = auto.GetConsoleWindow() if cmdWindow: cmdTransformPattern = cmdWindow.GetTransformPattern() cmdTransformPattern.Move(sw // 2, 0) cmdTransformPattern.Resize(sw // 2, sh * 3 // 4) subprocess.Popen('mmc.exe devmgmt.msc') mmcWindow = auto.WindowControl(searchDepth=1, ClassName='MMCMainFrame') mmcTransformPattern = mmcWindow.GetTransformPattern() mmcTransformPattern.Move(0, 0) mmcTransformPattern.Resize(sw // 2, sh * 3 // 4) tree = mmcWindow.TreeControl() for item, depth in auto.WalkControl(tree, includeTop=True): if isinstance( item, auto.TreeItemControl ): #or item.ControlType == auto.ControlType.TreeItemControl item.GetSelectionItemPattern().Select(waitTime=0.05) pattern = item.GetExpandCollapsePattern() if pattern.ExpandCollapseState == auto.ExpandCollapseState.Collapsed: pattern.Expand(waitTime=0.05) auto.Logger.WriteLine(' ' * (depth - 1) * 4 + item.Name, auto.ConsoleColor.Green) if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll by <Color=Cyan>SetScrollPercent</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) treeScrollPattern = tree.GetScrollPattern() treeScrollPattern.SetScrollPercent(auto.ScrollPattern.NoScrollValue, 0) treeScrollPattern.SetScrollPercent(auto.ScrollPattern.NoScrollValue, 100) if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll to top by SendKeys <Color=Cyan>Ctrl+Home</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) tree.SendKeys('{Ctrl}{Home}', waitTime=1) if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll to bottom by SendKeys <Color=Cyan>Ctrl+End</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) tree.SendKeys('{Ctrl}{End}', waitTime=1) if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll to top by <Color=Cyan>WheelUp</Color>') cmdWindow.SetActive(waitTime=1) print(tree.NativeWindowHandle, tree.Element, len(tree.GetChildren())) # before expand, tree has no scrollbar. after expand, tree has a scrollbar. # need to Refind on some PCs before find ScrollBarControl from tree # maybe the old element has no scrollbar info tree.Refind() print(tree.NativeWindowHandle, tree.Element, len(tree.GetChildren())) vScrollBar = tree.ScrollBarControl( AutomationId='NonClientVerticalScrollBar') rangeValuePattern = vScrollBar.GetRangeValuePattern() vScrollBarRect = vScrollBar.BoundingRectangle thumb = vScrollBar.ThumbControl() while True: vPercent = treeScrollPattern.VerticalScrollPercent vPercent2 = rangeValuePattern.Value print('ScrollPattern.VerticalScrollPercent', vPercent) print('ValuePattern.Value', vPercent2) if vPercent2 > 0: tree.WheelUp(waitTime=0.05) else: break if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll to bottom by <Color=Cyan>WheelDown</Color>') cmdWindow.SetActive(waitTime=1) while True: vPercent = treeScrollPattern.VerticalScrollPercent vPercent2 = rangeValuePattern.Value print('ScrollPattern.VerticalScrollPercent', vPercent) print('ValuePattern.Value', vPercent2) if vPercent2 < 100: tree.WheelDown(waitTime=0.05) else: break if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll by <Color=Cyan>DragDrop</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) x, y = thumb.MoveCursorToMyCenter() auto.DragDrop(x, y, x, vScrollBarRect.top, waitTime=1) x, y = thumb.MoveCursorToMyCenter() auto.DragDrop(x, y, x, vScrollBarRect.bottom) mmcWindow.GetWindowPattern().Close() if cmdWindow: cmdWindow.SetActive() auto.Logger.Write('\nPress any key to exit', auto.ConsoleColor.Cyan) import msvcrt while not msvcrt.kbhit(): pass
def doStep(taskName, instanceCount=0, run_mode=False): time.sleep(1) global app global dc # global run_mode # Load Action-Object Data ACTION_OBJECT_DB_FILE = './action_object_db/action_object_db.csv' with open(ACTION_OBJECT_DB_FILE, newline='') as csvfile: action_object_data = list(csv.reader(csvfile, delimiter=',')) # print(action_object_data) # while True: # command = getCommand('What is the name of your task?') command = taskName.lower() doc = nlp(command) token_text = [token.text for token in doc] # ['This', 'is', 'a', 'text'] print(token_text) token_pos = [token.pos_ for token in doc] # ['VERB', 'DET', 'NOUN', 'PUNCT'] print(token_pos) token_dep = [token.dep_ for token in doc] # ['ROOT', 'det', 'dobj', 'punct'] print(token_dep) # if commandReceived == "switch to voice": # prompt('Switching to voice mode') # manual_mode = False # if commandReceived == "switch to manual mode": # prompt('Switching to manual mode') # manual_mode = True if command == "switch to instruction mode": prompt('Entering instruction mode...') run_mode = False instructionMode() return '', '', 0 elif command == "help": prompt( "\n\nYou can say:\nswitch to instruction mode\nwhat is this\nhelp\nexit, shut up, shutdown, goodbye, stop it\nshow instruction list\n\n\nopen a/the/my***\nclick on/the/a\nclick on it\ngo to ***\n\n" ) return '', '', 0 # if commandReceived == "listen": # commandReceived = getCommand("What do you want me to do?") # doStep(commandReceived, 0, False) elif command == "show instruction list": instructionList = os.listdir(INSTRUCTIONS_FOLDER) print('\n\nInstruction List') print('======================') for instructionIndex, taskName in enumerate(instructionList): santizedTaskName = taskName.replace('_', ' ').replace('.csv', '') print(santizedTaskName) print('\n\n') return '', '', 0 # if commandReceived == "do this": # commandReceived = getCommand("What is the name of the task?") # prompt('Finding instructions from memory...') # runInstructions(commandReceived) elif command == "what is this": currentMouseX, currentMouseY = auto.GetCursorPos() control = auto.ControlFromPoint(currentMouseX, currentMouseY) for c, d in auto.WalkControl(control, True, 1000): print(c.ControlTypeName, c.ClassName, c.AutomationId, c.BoundingRectangle, c.Name, '0x{0:X}({0})'.format(c.NativeWindowHandle)) return '', '', 0 elif command == "exit" or command == "shut up" or command == "shutdown" or command == "goodbye" or command == "stop it": prompt('Thank you for using virtual employee. Goodbye.') sys.exit() if len(token_text) > 0: verbid = -1 dobjid = -1 first_word = token_text[0].lower().strip() # override behavior during TYPE command: # if first_word == 'type' for textid, text in enumerate(token_text): if (token_pos[textid] == 'VERB' and (token_dep[textid] == 'ROOT' or token_dep[textid] == 'amod')) or (token_pos[textid] == 'ADJ' and token_dep[textid] == 'amod'): verbid = textid if token_dep[textid] == 'dobj' or token_dep[textid] == 'pobj' or ( token_pos[textid] == 'NOUN' and token_dep[textid] == 'ROOT' ) or (token_pos[textid] == 'NOUN' and token_dep[textid] == 'advcl') or (token_pos[textid] == 'VERB' and token_dep[textid] == 'advcl') or ( token_pos[textid] == 'VERB' and token_dep[textid] == 'advmod') or ( token_pos[textid] == 'VERB' and token_dep[textid] == 'xcomp' ) or (token_pos[textid] == 'VERB' and token_dep[textid] == 'pcomp'): dobjid = textid if verbid == -1: prompt( 'There is no action in your instruction step. Please start with a verb.' ) else: if dobjid == -1: prompt( 'There is no object to act on. Please state a recipient of the action.' ) else: action_from_command = str(token_text[verbid].lower().strip()) object_from_command = str(token_text[dobjid].lower().strip()) # prompt('Instruction valid.') objectInstanceCount = 1 # Counts how many times it should match before selecting the right control # Look for built-in actions first for action, dobject, subprocess_command, requirement in action_object_data: # print('token_text[verbid]',token_text[verbid]) # print('action',action) # print('token_text[dobjid]', token_text[dobjid]) # print('dobject', dobject) # print('token_text[verbid] == action', token_text[verbid] == action) # print('token_text[dobjid] == dobject', token_text[dobjid] == dobject) action_from_db = str(action.lower().strip()) object_from_db = str(dobject.lower().strip()) if action_from_command == action_from_db and object_from_command == object_from_db: # prompt('Instruction found. [' + subprocess_command + ']') if action_from_command == 'open': # print(subprocess_command) subprocess.Popen(subprocess_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) time.sleep(2) control = auto.GetRootControl() # Get Target Window detectControls = ['WindowControl', 'PaneControl'] for c, d in auto.WalkControl(control, True, 1000): if c.ControlTypeName in detectControls: # print(c.ControlTypeName, c.ClassName, c.AutomationId, c.BoundingRectangle, c.Name, '0x{0:X}({0})'.format(c.NativeWindowHandle)) if object_from_db.lower() in c.Name.lower( ): dc.Clear() dc.SetPen(wx.Pen("blue")) dc.SetBrush( wx.Brush("blue", wx.TRANSPARENT)) ControlBoundingRect = c.BoundingRectangle # TODO: calculate dynamic width/height based on screensize using wx at header xScale = 1 #1.25 yScale = 1 #1.25 width = ( ControlBoundingRect.right * xScale ) - (ControlBoundingRect.left * xScale) height = ( ControlBoundingRect.bottom * yScale ) - (ControlBoundingRect.top * yScale) dc.DrawRectangle( (ControlBoundingRect.left * xScale), (ControlBoundingRect.top * yScale), width, height) break control = getActiveWindowControls() # Then process all other commands here if action_from_command == 'go': prompt(action_from_command + ' to ' + object_from_command) control = getActiveWindowControls() # Get controls of the target window # detectControls = ['TextControl', 'ButtonControl', 'EditControl'] possibleControlMatch = [] if object_from_command == 'dropdown': possibleControlMatch = ['▾'] detectControls = [ 'HyperlinkControl', 'EditControl', 'ButtonControl', 'ComboBoxControl', 'ListItemControl', 'ListControl', 'TextControl' ] for c, d in auto.WalkControl(control, True, 1000): # print(c.ControlTypeName, c.ClassName, c.AutomationId, c.BoundingRectangle, c.Name, '0x{0:X}({0})'.format(c.NativeWindowHandle)) if object_from_command in c.Name.lower().strip( ) or object_from_command in possibleControlMatch: if c.ControlTypeName in detectControls: # if object_from_db.lower() in c.Name.lower(): dc.Clear() dc.SetPen(wx.Pen("green")) dc.SetBrush(wx.Brush("green", wx.TRANSPARENT)) ControlBoundingRect = c.BoundingRectangle xScale = 1 #1.25 yScale = 1 #1.25 width = (ControlBoundingRect.right * xScale) - (ControlBoundingRect.left * xScale) height = (ControlBoundingRect.bottom * yScale) - (ControlBoundingRect.top * yScale) dc.DrawRectangle( (ControlBoundingRect.left * xScale), (ControlBoundingRect.top * yScale), width, height) controlCenterX = int(ControlBoundingRect.left + (width / 2)) controlCenterY = int(ControlBoundingRect.top + (height / 2)) auto.SetCursorPos(controlCenterX, controlCenterY) if run_mode == False: # confirmAction = getCommand('This one?') commandReceived = '' commandReceived = getCommand("This one?") while commandReceived == '': # print('waiting...') continue print('commandReceived', commandReceived) if commandReceived in yesConfirmations: break else: objectInstanceCount += 1 continue else: if int(objectInstanceCount) == int( instanceCount): break else: objectInstanceCount += 1 continue elif action_from_command == 'click': # prompt(action_from_command + ' on ' + object_from_command) if object_from_command == 'it': currentMouseX, currentMouseY = auto.GetCursorPos() auto.Click(currentMouseX, currentMouseY) else: control = getActiveWindowControls() # Get controls of the target window # detectControls = ['TextControl', 'ButtonControl', 'EditControl'] detectControls = [ 'HyperlinkControl', 'EditControl', 'ButtonControl', 'ComboBoxControl', 'ListItemControl', 'ListControl', 'TextControl' ] for c, d in auto.WalkControl(control, True, 1000): if object_from_command in c.Name.lower().strip(): # print(c.ControlTypeName, c.ClassName, c.AutomationId, c.BoundingRectangle, c.Name, '0x{0:X}({0})'.format(c.NativeWindowHandle)) # if c.ControlTypeName in detectControls: # if object_from_db.lower() in c.Name.lower(): dc.Clear() dc.SetPen(wx.Pen("green")) dc.SetBrush(wx.Brush("green", wx.TRANSPARENT)) ControlBoundingRect = c.BoundingRectangle xScale = 1 #1.25 yScale = 1 #1.25 width = (ControlBoundingRect.right * xScale) - (ControlBoundingRect.left * xScale) height = (ControlBoundingRect.bottom * yScale) - (ControlBoundingRect.top * yScale) dc.DrawRectangle( (ControlBoundingRect.left * xScale), (ControlBoundingRect.top * yScale), width, height) controlCenterX = int(ControlBoundingRect.left + (width / 2)) controlCenterY = int(ControlBoundingRect.top + (height / 2)) auto.SetCursorPos(controlCenterX, controlCenterY) if run_mode == False: commandReceived = getCommand('This one?') if commandReceived in yesConfirmations: auto.Click(controlCenterX, controlCenterY) break else: objectInstanceCount += 1 continue else: print('objectInstanceCount', objectInstanceCount) print('instanceCount', instanceCount) if int(objectInstanceCount) == int( instanceCount): print('It goes here 3') # auto.Click(controlCenterX, controlCenterY) currentMouseX, currentMouseY = auto.GetCursorPos( ) auto.Click(currentMouseX, currentMouseY) # time.sleep(2) break else: print('It goes here 2') objectInstanceCount += 1 continue return action_from_command, object_from_command, objectInstanceCount return '', '', 0
def main(): cmdWindow = automation.GetConsoleWindow() subprocess.Popen('mmc.exe devmgmt.msc') time.sleep(1) mmcWindow = automation.WindowControl(searchDepth=1, ClassName='MMCMainFrame') tree = mmcWindow.TreeControl() for item, depth in automation.WalkControl(tree, includeTop=True): if isinstance( item, automation.TreeItemControl ): #or item.ControlType == automation.ControlType.TreeItemControl item.Select() if automation.ExpandCollapseState.Collapsed == item.CurrentExpandCollapseState( ): item.Expand(0) automation.Logger.WriteLine(' ' * (depth - 1) * 4 + item.Name, automation.ConsoleColor.Green) time.sleep(0.1) if cmdWindow: automation.Logger.ColorfulWriteLine( 'Scroll to top by SendKeys <Color=Cyan>Ctrl+Home</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) tree.SendKeys('{Ctrl}{Home}', waitTime=1) if cmdWindow: automation.Logger.ColorfulWriteLine( 'Scroll to bottom by SendKeys <Color=Cyan>Ctrl+End</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) tree.SendKeys('{Ctrl}{End}', waitTime=1) if cmdWindow: automation.Logger.ColorfulWriteLine( 'Scroll to top by <Color=Cyan>WheelUp</Color>') cmdWindow.SetActive(waitTime=1) print(tree.Handle, tree.Element, len(tree.GetChildren())) # before expand, tree has no scrollbar. after expand, tree has a scrollbar. # need to Refind on some PCs before find ScrollBarControl from tree # maybe the old element has no scrollbar info tree.Refind() print(tree.Handle, tree.Element, len(tree.GetChildren())) vScrollBar = tree.ScrollBarControl( AutomationId='NonClientVerticalScrollBar') vScrollBarRect = vScrollBar.BoundingRectangle thumb = vScrollBar.ThumbControl() while True: vPercent = tree.CurrentVerticalScrollPercent() vPercent2 = vScrollBar.RangeValuePatternCurrentValue() print('TreeControl.CurrentVerticalScrollPercent', vPercent) print('ScrollBarControl.RangeValuePatternCurrentValue', vPercent2) if vPercent2 > 0: tree.WheelUp(waitTime=0.1) else: break if cmdWindow: automation.Logger.ColorfulWriteLine( 'Scroll to bottom by <Color=Cyan>WheelDown</Color>') cmdWindow.SetActive(waitTime=1) while True: vPercent = tree.CurrentVerticalScrollPercent() vPercent2 = vScrollBar.RangeValuePatternCurrentValue() print('TreeControl.CurrentVerticalScrollPercent', vPercent) print('ScrollBarControl.RangeValuePatternCurrentValue', vPercent2) if vPercent2 < 100: tree.WheelDown(waitTime=0.1) else: break if cmdWindow: automation.Logger.ColorfulWriteLine( 'Scroll by <Color=Cyan>DragDrop</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) x, y = thumb.MoveCursorToMyCenter() automation.DragDrop(x, y, x, vScrollBarRect[1], waitTime=1) x, y = thumb.MoveCursorToMyCenter() automation.DragDrop(x, y, x, vScrollBarRect[3]) mmcWindow.Close()