我已经注意到setforegroundwindow窗口可能非常不稳定--不管你怎么做。
我注意到,在可能的情况下,使用UIAutomation似乎可以改善情况。
例如:
获取WindowPattern并使用如下内容:
windowPattern.SetWindowVisualState( WindowVisualState.Normal );
windowPattern.SetWindowVisualState( WindowVisualState.Maximized );现在我的问题是:
我怎么知道我应该让它最大化还是正常。任务经理和龙自然都知道如何做到这一点。如果它之前是最大化的,然后是最小化的,那么当我切换到窗口时,我希望最大化窗口。如果它以前没有被最大化,我想让它变得“正常”。
有什么想法吗?
发布于 2013-07-16 02:06:12
用于AutomationElement的SetFocus不起作用。
来自以下问题:Get window state of another process
我发现GetPlacement应用程序接口满足了我的需求:
if ( (windowplacement.flags & WPF_RESTORETOMAXIMIZED) > 0 )
{
windowPattern.SetWindowVisualState( WindowVisualState.Maximized );
}
else
{
windowPattern.SetWindowVisualState( WindowVisualState.Normal );
}这样,如果窗口被最大化,则窗口将恢复为最大化,如果未最大化,窗口将恢复为正常。
发布于 2017-06-15 02:33:11
您可以使用WindowPattern将窗口设置为前景,如下所示:
var p =
(WindowPattern)_w.AutomationElement.GetCurrentPattern(WindowPattern.Pattern);
p.SetWindowVisualState(WindowVisualState.Normal);发布于 2013-07-15 22:02:45
好吧,我误解了问题的意思。我相信做你想做的事情的关键是使用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();https://stackoverflow.com/questions/17656045
复制相似问题