1 Star 0 Fork 20

zhaoqil/PythonUIAutomation4Windows

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
hide_window_with_hotkey.py 3.62 KB
一键复制 编辑 原始数据 按行查看 历史
yinkaisheng 提交于 2016-11-02 10:18 . add ColorfulWrite
#!python3
# -*- coding: utf-8 -*-
# hide a window with hotkey Ctrl+1, show the hidden window with hotkey Ctrl+2
# run notepad.exe first and then press the hotkey for test
import os
import sys
import time
import ctypes
import subprocess
import automation
WindowsWantToHide = ('Warcraft III', 'Valve001', 'Counter-Strike', 'Notepad')
def hide():
root = automation.GetRootControl()
for window in root.GetChildren():
if window.ClassName in WindowsWantToHide:
automation.Logger.WriteLine('hide window, handle {}'.format(window.Handle))
window.Hide()
fin = open('hide_windows.txt', 'wt')
fin.write(str(window.Handle) + '\n')
fin.close()
def show():
fout = open('hide_windows.txt')
lines = fout.readlines()
fout.close()
for line in lines:
handle = int(line)
window = automation.ControlFromHandle(handle)
if window:
automation.Logger.WriteLine('show window: {}'.format(handle))
window.Show()
def HideWindowFunc(stopEvent):
scriptName = os.path.basename(__file__)
cmd = r'py.exe {} hide {}'.format(scriptName, ' '.join(sys.argv[1:]))
automation.Logger.ColorfulWriteLine('HideWindowFunc call <Color=Green>{}</Color>'.format(cmd))
p = subprocess.Popen(cmd)
while True:
if None != p.poll():
break
if stopEvent.is_set():
childProcess = []
for pid, pname in automation.Win32API.EnumProcess():
ppid = automation.Win32API.GetParentProcessId(pid)
if ppid == p.pid or pid == p.pid:
cmd = automation.Win32API.GetProcessCommandLine(pid)
childProcess.append((pid, pname, cmd))
for pid, pname, cmd in childProcess:
automation.Logger.WriteLine('kill process: {}, {}, "{}"'.format(pid, pname, cmd), automation.ConsoleColor.Yellow)
automation.Win32API.TerminateProcess(pid)
break
stopEvent.wait(1)
automation.Logger.WriteLine('HideWindowFunc exit')
def ShowWindowFunc(stopEvent):
scriptName = os.path.basename(__file__)
cmd = r'py.exe {} show {}'.format(scriptName, ' '.join(sys.argv[1:]))
automation.Logger.ColorfulWriteLine('ShowWindowFunc call <Color=Green>{}</Color>'.format(cmd))
p = subprocess.Popen(cmd)
while True:
if None != p.poll():
break
if stopEvent.is_set():
childProcess = []
for pid, pname in automation.Win32API.EnumProcess():
ppid = automation.Win32API.GetParentProcessId(pid)
if ppid == p.pid or pid == p.pid:
cmd = automation.Win32API.GetProcessCommandLine(pid)
childProcess.append((pid, pname, cmd))
for pid, pname, cmd in childProcess:
automation.Logger.WriteLine('kill process: {}, {}, "{}"'.format(pid, pname, cmd), automation.ConsoleColor.Yellow)
automation.Win32API.TerminateProcess(pid)
break
stopEvent.wait(1)
automation.Logger.WriteLine('ShowWindowFunc exit')
if __name__ == '__main__':
if 'hide' in sys.argv[1:]:
hide()
elif 'show' in sys.argv[1:]:
show()
else:
automation.RunWithHotKey({(automation.ModifierKey.MOD_CONTROL, automation.Keys.VK_1) : HideWindowFunc
, (automation.ModifierKey.MOD_CONTROL, automation.Keys.VK_2) : ShowWindowFunc}
, (automation.ModifierKey.MOD_CONTROL, automation.Keys.VK_4)
)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/xxrl2018/PythonUIAutomation4Windows.git
git@gitee.com:xxrl2018/PythonUIAutomation4Windows.git
xxrl2018
PythonUIAutomation4Windows
PythonUIAutomation4Windows
master

搜索帮助