public static AutomationElement FromHandle(IntPtr hwnd) { ArgumentCheck.Assert(hwnd, (h => h != IntPtr.Zero), "hwnd"); if (hwnd == NativeMethods.RootWindowHandle) { return(RootElement); } AutomationElement element = null; foreach (var source in SourceManager.GetAutomationSources()) { var sourceElement = source.GetElementFromHandle(hwnd); if (sourceElement != null) { element = SourceManager.GetOrCreateAutomationElement(sourceElement); break; } } if (element == null) { throw new ElementNotAvailableException(); } return(element); }