问题描述
我结合了来自两个不同来源的经验,以使用UIAutomation检测打开的窗口并移动窗口:
几乎每个应用程序窗口(Win32应用程序和UWP应用程序)都可以移动。除了Microsoft Edge浏览器窗口!
问题:有谁知道如何使用UIAutomation移动Edge窗口?有谁知道Edge浏览器有什么特别之处,可以阻止它遵循Microsoft自己的UIAutomation库?
这是我完整的控制台应用程序代码(必需:添加对UIAutomationClient和UIAutomationTypes的引用):
using System;
using System.Threading;
using System.Windows.Automation;
namespace UiAutomationTest
{
class Program
{
static void Main(string[] args)
{
Automation.AddAutomationEventHandler(eventId: WindowPattern.WindowOpenedEvent,element: AutomationElement.RootElement,scope: TreeScope.Children,eventHandler: OnWindowOpened);
Console.ReadLine();
Automation.RemoveAllEventHandlers();
}
private static void OnWindowOpened(object sender,AutomationEventArgs e)
{
try
{
var element = sender as AutomationElement;
if (element != null)
{
var hWnd = new IntPtr(element.Current.NativeWindowHandle);
Console.WriteLine($"Opened: {element.Current.Name} (Pid:{element.Current.ProcessId}),hWnd:{Convert.ToInt32(hWnd.ToString())}");
var _windowPattern = GetControlPattern(element,WindowPattern.Pattern) as WindowPattern;
if (_windowPattern == null)
{
Console.WriteLine("WindowPattern is null! Aborting.");
return;
}
if (false == _windowPattern.WaitForInputIdle(10000))
{
Feedback("Object not responding in a timely manner.");
return;
}
Console.WriteLine("Window is ready for input");
var _transformPattern = GetControlPattern(element,TransformPattern.Pattern) as TransformPattern;
if (_transformPattern == null)
{
Console.WriteLine("TransformPattern is null! Aborting.");
return;
}
// Is the TransformPattern object moveable?
if (_transformPattern.Current.CanMove)
{
Console.WriteLine("Waiting 3 seconds before move...");
// Wait a bit
Thread.Sleep(3000);
// Move element
_transformPattern.Move(250,500);
Console.WriteLine("Window was moved!");
}
else
{
Feedback("Window is not moveable.");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"{ex.GetType()}: {ex.Message}\n{ex.StackTrace}");
}
}
/// <summary>
/// Gets a specified control pattern.
/// </summary>
/// <param name="ae">The automation element we want to obtain the control pattern from.</param>
/// <param name="ap">The control pattern of interest.</param>
/// <returns>A ControlPattern object.</returns>
private static object GetControlPattern(AutomationElement ae,AutomationPattern ap)
{
if (false == ae.TryGetCurrentPattern(ap,out object oPattern))
{
Feedback("Object does not support the " + ap.ProgrammaticName + " Pattern");
return null;
}
Feedback("Object supports the " + ap.ProgrammaticName + " Pattern.");
return oPattern;
}
private static void Feedback(string message)
{
Console.WriteLine(message);
}
}
}
这是一个视频,显示我可以打开Internet Explorer并移动它,Chrome并移动它,但是Edge不会移动!
系统详细信息:
- Windows 10 1809
- Visual Studio 2019 16.7
- .NET Framework 4.7.2 C#控制台应用程序
最后,我什至尝试使用老式的Win32 API,例如EnumWindows,MoveWindow,SetWindowPlacement和SetWindowPos。 Edge浏览器似乎位于ApplicationFrameHost.exe进程窗口的内部。当我尝试移动窗口时,得到的结果与使用UIAutomation库的结果相同:它“说”通过了,但是窗口实际上并没有移动!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)