繁体   English   中英

Windows UI自动化

[英]Windows UI Automation

我正在测试以下示例代码,并且每当我尝试运行它时,我将在下面显示错误。 但是,calc.exe进程已成功执行,那么句柄如何可能为null或为零? 我希望你能理解我想要解决的问题。 谢谢! 代码示例来自http://www.mathpirate.net/log/tag/system-windows-automation/

UIAutomationClient.dll中发生未处理的“System.ArgumentException”类型异常附加信息:hwnd不能是IntPtr.Zero或null。

//Launches the Windows Calculator and gets the Main Window's Handle.
Process calculatorProcess = Process.Start("calc.exe");
calculatorProcess.WaitForInputIdle();
IntPtr calculatorWindowHandle = calculatorProcess.MainWindowHandle;

//Here I use a window handle to get an AutomationElement for a specific window.
AutomationElement calculatorElement = AutomationElement.FromHandle(calculatorWindowHandle);

if(calculatorElement == null)
{
    throw new Exception("Uh-oh, couldn't find the calculator...");
}

//Walks some of the more interesting properties on the AutomationElement.
Console.WriteLine("--------Element");
Console.WriteLine("AutomationId: {0}", calculatorElement.Current.AutomationId);
Console.WriteLine("Name: {0}", calculatorElement.Current.Name);
Console.WriteLine("ClassName: {0}", calculatorElement.Current.ClassName);
Console.WriteLine("ControlType: {0}", calculatorElement.Current.ControlType.ProgrammaticName);
Console.WriteLine("IsEnabled: {0}", calculatorElement.Current.IsEnabled);
Console.WriteLine("IsOffscreen: {0}", calculatorElement.Current.IsOffscreen);
Console.WriteLine("ProcessId: {0}", calculatorElement.Current.ProcessId);

//Commented out because it requires another library reference. However, it's useful to see that this exists.
//Console.WriteLine("BoundingRectangle: {0}", calculatorElement.Current.BoundingRectangle);

Console.WriteLine("Supported Patterns:");
foreach (AutomationPattern supportedPattern in calculatorElement.GetSupportedPatterns())
{
    Console.WriteLine("\t{0}", supportedPattern.ProgrammaticName);
}

你误解了WaitForInputIdle (这是该函数目前所做的一个非常糟糕的名字)。 在创建主窗口之前,您要求提供主窗口的地址。 因此,您最终会将无效的窗口句柄传递给其他函数。

编辑:我强烈建议使用UI自动化库,如white如果你要用它做认真的工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM