首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UI自动化切换窗口

UI自动化切换窗口
EN

Stack Overflow用户
提问于 2013-07-15 21:58:31
回答 3查看 5.1K关注 0票数 8

我已经注意到setforegroundwindow窗口可能非常不稳定--不管你怎么做。

我注意到,在可能的情况下,使用UIAutomation似乎可以改善情况。

例如:

获取WindowPattern并使用如下内容:

代码语言:javascript
复制
windowPattern.SetWindowVisualState( WindowVisualState.Normal );

windowPattern.SetWindowVisualState( WindowVisualState.Maximized );

现在我的问题是:

我怎么知道我应该让它最大化还是正常。任务经理和龙自然都知道如何做到这一点。如果它之前是最大化的,然后是最小化的,那么当我切换到窗口时,我希望最大化窗口。如果它以前没有被最大化,我想让它变得“正常”。

有什么想法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-07-16 02:06:12

用于AutomationElement的SetFocus不起作用。

来自以下问题:Get window state of another process

我发现GetPlacement应用程序接口满足了我的需求:

代码语言:javascript
复制
     if ( (windowplacement.flags & WPF_RESTORETOMAXIMIZED) > 0 )
     {
        windowPattern.SetWindowVisualState( WindowVisualState.Maximized );
     }
     else
     {
        windowPattern.SetWindowVisualState( WindowVisualState.Normal );
     }

这样,如果窗口被最大化,则窗口将恢复为最大化,如果未最大化,窗口将恢复为正常。

票数 2
EN

Stack Overflow用户

发布于 2017-06-15 02:33:11

您可以使用WindowPattern将窗口设置为前景,如下所示:

代码语言:javascript
复制
var p =
 (WindowPattern)_w.AutomationElement.GetCurrentPattern(WindowPattern.Pattern);
p.SetWindowVisualState(WindowVisualState.Normal);
票数 2
EN

Stack Overflow用户

发布于 2013-07-15 22:02:45

好吧,我误解了问题的意思。我相信做你想做的事情的关键是使用AutomationElement.SetFocus()方法。

这是一个基本的例子。

代码语言:javascript
复制
//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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17656045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档