我正在测试下面的示例代码,无论何时我尝试运行它,我都会有一个错误显示在下面。但是,calc.exe进程是成功执行的,那么如何才能使句柄为null或零呢?我希望你能理解我想要传达的信息。谢谢!代码示例来自http://www.mathpirate.net/log/tag/system-windows-automation/
System.ArgumentException类型的未处理异常出现在UIAutomationClient.dll附加信息中: 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);
}发布于 2010-07-02 02:38:33
您误解了WaitForInputIdle (对于该函数当前的功能来说,它是一个非常糟糕的名称)。在创建主窗口之前,您正在询问主窗口的地址。因此,您最终会将一个无效的窗口句柄传递给其他函数。
编辑:如果要对UI自动化库进行认真的工作,我强烈建议您使用UI自动化库,如white。
https://stackoverflow.com/questions/3162958
复制相似问题