C# Windows用户界面自动化

C# Windows用户界面自动化,c#,user-interface,automation,C#,User Interface,Automation,我正在测试下面的示例代码,无论何时尝试运行它,都会出现如下所示的错误。但是,calc.exe进程已成功执行,因此句柄如何可能为null或零?我希望你明白我想表达的意思。谢谢代码示例来自 类型的未处理异常 中发生“System.ArgumentException” UIAutomationClient.dll 其他信息:hwnd不能为IntPtr.Zero或null 你误解了(这对于该函数目前的功能来说是一个可怕的坏名字)。在创建主窗口之前,您正在询问主窗口的地址。因此,您最终会将无效的窗口句柄传

我正在测试下面的示例代码,无论何时尝试运行它,都会出现如下所示的错误。但是,calc.exe进程已成功执行,因此句柄如何可能为null或零?我希望你明白我想表达的意思。谢谢代码示例来自

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

你误解了(这对于该函数目前的功能来说是一个可怕的坏名字)。在创建主窗口之前,您正在询问主窗口的地址。因此,您最终会将无效的窗口句柄传递给其他函数


编辑:我强烈建议您使用UI自动化库,例如如果您打算认真使用它。

如果您使用白色,请注意它有一些问题;我发现这些在我当前的项目中非常重要。您可以在以下位置看到白色问题列表:顺便问一句,这是使用UI自动化进行监控的建议吗?我有一个第三方应用程序,其中包含我想要监控的值,并且基于这些值,我希望执行一些操作,即UI操作、单击按钮、更改文本字段值等。至于监控值,我是否必须单独监控每个控件?谢谢
//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);
}