[英]UI Automation switch window
我注意到setforegroundwindow可能非常不稳定 - 无论你怎么做。
我注意到在可能的情况下使用UIAutomation似乎可以改善一些事情。
例如:
获取WindowPattern并使用如下内容:
windowPattern.SetWindowVisualState( WindowVisualState.Normal );
windowPattern.SetWindowVisualState( WindowVisualState.Maximized );
现在我的问题是:
我怎么知道我是否应该使它最大化或正常。 任务经理和龙自然都说似乎都知道如何做到这一点。 如果之前最大化,然后最小化,我想在切换到它时最大化窗口。 如果以前没有最大化,我想把它变成“正常”。
有任何想法吗?
SetFocus for AutomationElement不起作用。
从以下问题: 获取另一个进程的窗口状态
我发现GetPlacement api给了我所需要的东西:
if ( (windowplacement.flags & WPF_RESTORETOMAXIMIZED) > 0 )
{
windowPattern.SetWindowVisualState( WindowVisualState.Maximized );
}
else
{
windowPattern.SetWindowVisualState( WindowVisualState.Normal );
}
如果最大化,窗口将恢复到最大化,如果没有最大化,则恢复到正常。
好吧,我误解了这个问题。 我相信做你想做的事情的关键是使用AutomationElement.SetFocus()方法。
这是一个基本的例子。
//I assume you already have some way of getting the window's handle
var wih = new System.Windows.Interop.WindowInteropHelper(window);
IntPtr hWnd = wih.Handle;
//get the automation element from the handle
var ae = AutomationElement.FromHandle(hWnd);
//this will bring the window into focus.
ae.SetFocus();
您可以使用WindowPattern将窗口设置为前景,如下所示:
var p =
(WindowPattern)_w.AutomationElement.GetCurrentPattern(WindowPattern.Pattern);
p.SetWindowVisualState(WindowVisualState.Normal);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.