def main(): if auto.IsNT6orHigher: oriTitle = auto.GetConsoleOriginalTitle() else: oriTitle = auto.GetConsoleTitle() auto.SetConsoleTitle(auto.GetConsoleTitle() + ' | Ctrl+1,Ctrl+2, stop Ctrl+4') auto.Logger.ColorfullyWriteLine('Press <Color=Cyan>Ctrl+1 or Ctrl+2</Color> to start, press <Color=Cyan>ctrl+4</Color> to stop') auto.Logger.Write('\nUse UIAutomation in main thread:\n', auto.ConsoleColor.Yellow) auto.LogControl(auto.GetRootControl()) auto.Logger.Write('\n') auto.RunByHotKey({(auto.ModifierKey.Control, auto.Keys.VK_1): test1, (auto.ModifierKey.Control, auto.Keys.VK_2): test2}, (auto.ModifierKey.Control, auto.Keys.VK_4)) auto.SetConsoleTitle(oriTitle)
#!python3 # -*- coding: utf-8 -*- # hide windows with hotkey Ctrl+1, show the hidden windows with hotkey Ctrl+2 import os import sys import time import subprocess from threading import Event sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath( __file__)))) # not required after 'pip install uiautomation' import uiautomation as auto def capture(stopEvent: Event): with auto.UIAutomationInitializerInThread(debug=True): control = auto.ControlFromCursor() control.CaptureToImage('control.png') subprocess.Popen('control.png', shell=True) if __name__ == '__main__': auto.SetConsoleTitle('Capture: Ctrl+1, Exit: Ctrl+D') auto.Logger.ColorfullyWriteLine( 'Press <Color=Green>Ctr+1</Color> to capture a control image under mouse corsor' ) auto.RunByHotKey({ (auto.ModifierKey.Control, auto.Keys.VK_1): capture, })
if auto.IsIconic(handle): win.ShowWindow(auto.SW.Restore, 0) if __name__ == '__main__': for i in range(2): subprocess.Popen('notepad.exe') time.sleep(1) notepad = auto.WindowControl(searchDepth=1, ClassName='Notepad') notepad.MoveWindow(i * 400, 0, 400, 300) notepad.SendKeys('notepad {}'.format(i + 1)) auto.SetConsoleTitle('Hide: Ctrl+1, Show: Ctrl+2, Exit: Ctrl+D') cmdWindow = auto.GetConsoleWindow() if cmdWindow: cmdWindow.GetTransformPattern().Move(0, 300) auto.Logger.ColorfullyWriteLine( 'Press <Color=Green>Ctr+1</Color> to hide the windows\nPress <Color=Green>Ctr+2</Color> to show the windows\n' ) handles = [ win.NativeWindowHandle for win in auto.GetRootControl().GetChildren() if win.ClassName in WindowsWantToHide ] auto.RunByHotKey( { (auto.ModifierKey.Control, auto.Keys.VK_1): lambda event: hide(event, handles), (auto.ModifierKey.Control, auto.Keys.VK_2): lambda event: show(event, handles), }, waitHotKeyReleased=False)
break stopEvent.wait(0.01) if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('--main', action='store_true', help='exec main') parser.add_argument('--hide', action='store_true', help='hide window') parser.add_argument('--show', action='store_true', help='show window') args = parser.parse_args() if args.main: if args.hide: hide() elif args.show: show() else: subprocess.Popen('notepad') auto.GetConsoleWindow().SetActive() auto.Logger.ColorfullyWriteLine( 'Press <Color=Green>Ctr+1</Color> to hide\nPress <Color=Green>Ctr+2</Color> to show\n' ) auto.RunByHotKey( { (auto.ModifierKey.Control, auto.Keys.VK_1): lambda e: HotKeyFunc(e, ['--main', '--hide']), (auto.ModifierKey.Control, auto.Keys.VK_2): lambda e: HotKeyFunc(e, ['--main', '--show']), }, (auto.ModifierKey.Control, auto.Keys.VK_9))
parser.add_argument('--main', action='store_true', help='exec main') parser.add_argument('-d', '--depth', type=int, default=0xFFFFFFFF, help='max expand tree depth') parser.add_argument('-r', '--root', action='store_true', help='expand from root') parser.add_argument('-p', '--print', action='store_true', help='print tree node text', dest="print_") args = parser.parse_args() if args.main: ExpandFromRoot = args.root MaxExpandDepth = args.depth PrintTree = args.print_ main() else: auto.Logger.WriteLine('move mouse to a tree control and press Ctrl+1', auto.ConsoleColor.Green) auto.RunByHotKey( { (auto.ModifierKey.Control, auto.Keys.VK_1): lambda event: HotKeyFunc(event, ['--main'] + sys.argv[1:]) }, (auto.ModifierKey.Control, auto.Keys.VK_2))
parser.add_argument('--main', action='store_true', help='exec main') parser.add_argument('-d', '--depth', type=int, default=0xFFFFFFFF, help='max expand tree depth') parser.add_argument('-r', '--root', action='store_true', help='expand from root') # print is a keyword in py2, so send to unambiguous "dest". parser.add_argument('-p', '--print', action='store_true', help='print tree node text', dest="print_") args = parser.parse_args() # auto.Logger.WriteLine(str(args)) if args.main: ExpandFromRoot = args.root MaxExpandDepth = args.depth PrintTree = args.print_ main() else: auto.Logger.WriteLine('move mouse to a tree control and press Ctrl+1', auto.ConsoleColor.Green) auto.RunByHotKey( {(auto.ModifierKey.Control, auto.Keys.VK_1): HotKeyFunc}, (auto.ModifierKey.Control, auto.Keys.VK_2))
args = [sys.executable, __file__] + argv cmd = ' '.join('"{}"'.format(arg) for arg in args) auto.Logger.WriteLine('call {}'.format(cmd)) p = subprocess.Popen(cmd) while True: if None != p.poll(): break if stopEvent.is_set(): childProcesses = [pro for pro in psutil.process_iter() if pro.ppid == p.pid or pro.pid == p.pid] for pro in childProcesses: auto.Logger.WriteLine('kill process: {}, {}'.format(pro.pid, pro.cmdline()), auto.ConsoleColor.Yellow) p.kill() break stopEvent.wait(0.05) auto.Logger.WriteLine('HotKeyFunc exit') if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('--main', action='store_true', help='exec main') args = parser.parse_args() if args.main: main() else: auto.Logger.WriteLine('move mouse to a tree control and press Ctrl+3', auto.ConsoleColor.Green) auto.RunByHotKey({(auto.ModifierKey.Control, auto.Keys.VK_3): lambda event: HotKeyFunc(event, ['--main'])}, (auto.ModifierKey.Control, auto.Keys.VK_4))
childProcesses = [ pro for pro in psutil.process_iter() if pro.ppid == p.pid or pro.pid == p.pid ] for pro in childProcesses: auto.Logger.WriteLine( 'kill process: {}, {}'.format(pro.pid, pro.cmdline()), auto.ConsoleColor.Yellow) p.kill() break stopEvent.wait(0.01) auto.Logger.WriteLine('ShowWindowFunc exit') if __name__ == '__main__': if 'hide' in sys.argv[1:]: hide() elif 'show' in sys.argv[1:]: show() else: subprocess.Popen('notepad') auto.GetConsoleWindow().SetActive() auto.Logger.ColorfullyWriteLine( 'Run Notepad\nPress <Color=Green>Ctr+1</Color> to hide\nPress <Color=Green>Ctr+2</Color> to show\n' ) auto.RunByHotKey( { (auto.ModifierKey.Control, auto.Keys.VK_1): HideWindowFunc, (auto.ModifierKey.Control, auto.Keys.VK_2): ShowWindowFunc }, (auto.ModifierKey.Control, auto.Keys.VK_4))
#_uiobj = auto.UIAutomationInitializerInThread() # Show doesn't call any COM methods, so it doesn't need an UIAutomationInitializerInThread for handle in handles: win = auto.ControlFromHandle(handle) win.Show(0) if auto.IsIconic(handle): win.ShowWindow(auto.SW.Restore, 0) if __name__ == '__main__': for i in range(2): subprocess.Popen('notepad.exe') time.sleep(1) notepad = auto.WindowControl(searchDepth=1, ClassName='Notepad') notepad.MoveWindow(i * 400, 0, 400, 300) notepad.SendKeys('notepad {}'.format(i + 1)) auto.SetConsoleTitle('Hide: Ctrl+1, Show: Ctrl+2, Exit: Ctrl+D') auto.Logger.ColorfullyWriteLine( 'Press <Color=Green>Ctr+1</Color> to hide the windows\nPress <Color=Green>Ctr+2</Color> to show the windows\n' ) handles = [ win.NativeWindowHandle for win in auto.GetRootControl().GetChildren() if win.ClassName in WindowsWantToHide ] auto.RunByHotKey({ (auto.ModifierKey.Control, auto.Keys.VK_1): lambda event: hide(event, handles), (auto.ModifierKey.Control, auto.Keys.VK_2): lambda event: show(event, handles), })