AutomationElement.FromHandle Method
Retrieves a new AutomationElement object for the user interface (UI) item referenced by the specified window handle.
Assembly: UIAutomationClient (in UIAutomationClient.dll)
Parameters
- hwnd
- Type: System.IntPtr
The handle of the UI element.
Return Value
Type: System.Windows.Automation.AutomationElementAn AutomationElement for the UI item identified by hwnd.
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
How to get the handle in C#
The from handle is a nice way to get the AutomationElement. Using FindFirst can take a long time if you start from the desktop root (i.e. AutomationElement aeDesktop = AutomationElement.RootElement; ) Another advantage of the from handle is that it is very easy to use substring matching on the window title.
Here's some code on how to get the handle:
Here's some code on how to get the handle:
bool foundwindow = false;
IntPtr phdl= new IntPtr();
Process[] procs = System.Diagnostics.Process.GetProcesses();
for (int i = 0; i < procs.Length; i++)
{ if (procs[i].MainWindowTitle.Contains("notepad")) {foundwindow = true;
phdl = procs[i].MainWindowHandle;
Debug.WriteLine("Found window:"+procs[i].MainWindowTitle);break;
}
}
if (!foundwindow)
{ Debug.WriteLine("Could not get a handle for the window");return;
}
AutomationElement aeForm = AutomationElement.FromHandle(phdl);
- 4/8/2008
- Brent Groom