Testowałem poniższy kod przykładowy i jakkolwiek próbowałem go uruchomić, będę miał błąd pokazany poniżej. Jednak proces calc.exe został wykonany pomyślnie, więc jak to możliwe, że uchwyt jest zerowy lub zero? Mam nadzieję, że rozumiesz, co próbuję ci przekazać. Dzięki! Próbkę kod wynosi http://www.mathpirate.net/log/tag/system-windows-automation/Windows UI Automation
nieobsługiwany wyjątek typu „System.ArgumentException” w module UIAutomationClient.dll dodatkowe: hwnd nie może być IntPtr.Zero lub zerowy.
//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);
}
Jeśli używasz White, pamiętaj, że ma kilka problemów; Znalazłem te całkiem znaczące w moim obecnym projekcie. Listę białych problemów można znaleźć na stronie: http://white.codeplex.com/workitem/list/basic –
Btw, czy jest to zalecany pomysł, aby używać Automatyzacji interfejsu użytkownika do celów monitorowania? Mam aplikację innej firmy, w której wartości, które chcę monitorować i na podstawie wartości, chciałbym wykonać pewne czynności, tj. Działania interfejsu użytkownika, kliknięcie przycisku, zmiana wartości pola tekstowego itp. Co do wartości monitorowania, czy muszę indywidualnie monitorować każdą kontrolę? Dzięki! – user303907