【问题标题】:Minimize Process from powershell/python using Process ID (PID)使用进程 ID (PID) 从 powershell/python 最小化进程
【发布时间】:2022-01-27 18:02:21
【问题描述】:
我正在为应用锁编写一个 python 脚本。为此,我正在使用 python 子进程执行 Get-Process -Name "notepad++" PowerShell 命令以获取进程 ID。
现在,使用psutil 我可以终止该进程。但我的目标是使用 powershell/python 在 while 循环中最小化窗口。因此,在用户输入密码之前,程序将无法使用。
【问题讨论】:
标签:
python
powershell
ctypes
pywin32
win32gui
【解决方案1】:
使用 Powershell,您可以使用 UIAutomationClient 方法来完成此操作,而无需依赖本机调用。
这里有一个小例子来演示如何检查窗口状态,如果没有则最小化窗口。
Add-Type -AssemblyName UIAutomationClient
$MyProcess = Get-Process -Name "notepad++"
$ae = [System.Windows.Automation.AutomationElement]::FromHandle($MyProcess.MainWindowHandle)
$wp = $ae.GetCurrentPattern([System.Windows.Automation.WindowPatternIdentifiers]::Pattern)
# Your loop to make sure the window stay minimized would be here
# While...
$IsMinimized = $wp.Current.WindowVisualState -eq 'Minimized'
if (! $IsMinimized) { $wp.SetWindowVisualState('Minimized') }
# End While
参考
How to switch minimized application to normal state