给定单个实例的桌面应用程序ccTest.exe,如果应用程序已经在最小化状态下运行,如何使用PowerShell (v6或7)以正常状态运行应用程序。
我需要访问主窗口中的控件,所以它必须处于正常状态。Get进程可以检查应用程序是否正在运行。如果没有,那么使用启动进程和-WindowStyle正常参数启动它.但是,如果单实例应用程序已经在运行最小化,启动进程就无法工作。
由于时间限制,我宁愿不关闭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)\Test\ccTest.exe" -WindowStyle Normal
execute needed function
}
那么,如何将状态/样式从最小化更改为正常,以运行所需的函数?
发布于 2022-01-08 03:41:22
这应该能起作用。
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)\Test\ccTest.exe" -WindowStyle Normal
# execute needed function
}
参考资料:
https://stackoverflow.com/questions/70629307
复制相似问题