使用PowerShell,如何将最小化的应用程序切换到正常状态



给定一个单实例桌面应用程序ccTest.exe,如果应用程序已在最小化状态下运行,我如何使用PowerShell(v6或7(在正常状态下运行该应用程序。

我需要访问主窗口中的控件,所以它必须处于正常状态。Get-Process用于检查应用程序是否正在运行。如果没有,则使用"启动进程"和-WindowStyle Normal参数启动它。但是,如果单实例应用程序已经在运行Minimized,则启动进程不起作用。

由于时间限制,如果最小化,我宁愿不关闭ccTest.exe,然后用启动进程启动。这是测试代码。

if (Get-Process | Select MainWindowTitle, ProcessName, Id | where {$_.MainWindowTitle -like "ccTest*"})
{ 
if Minimized make it Normal
execute needed function
}
else
{
start-process "C:Program Files (x86)TestccTest.exe" -WindowStyle Normal
execute needed function
}

那么,如何将状态/样式从最小化更改为正常以运行所需的功能呢?

这样就可以了。

Add-Type -AssemblyName UIAutomationClient
$MyProcess = Get-Process | where { $_.MainWindowTitle -like "ccTest*" }
if ($null -ne $MyProcess) { 
# if Minimized make it Normal
$ae = [System.Windows.Automation.AutomationElement]::FromHandle($MyProcess.MainWindowHandle)
$wp = $ae.GetCurrentPattern([System.Windows.Automation.WindowPatternIdentifiers]::Pattern)
if ($wp.Current.WindowVisualState -eq 'Minimized') {
$wp.SetWindowVisualState('Normal') 
}
# execute needed function
}
else {
start-process "C:Program Files (x86)TestccTest.exe" -WindowStyle Normal
# execute needed function
}

参考文献:

使用powershell 最大化窗口并使其处于领先地位

获取另一进程的窗口状态

相关内容

最新更新