static void CheckProviderFromHandle(int handle, string name) { var provider = AutomationInteropProvider.HostProviderFromHandle(new IntPtr(handle)); Assert.IsNotNull(provider, string.Format("{0}'s provider", name)); Assert.AreEqual( provider.GetPropertyValue(AEIds.NativeWindowHandleProperty.Id), new IntPtr(handle), string.Format("{0}'s handle", name)); }
private IRawElementProviderSimple GetHostHelper(HostedWindowWrapper hwndWrapper) { return(AutomationInteropProvider.HostProviderFromHandle(hwndWrapper.Handle)); }
// Process all the Element Properties internal override object GetElementProperty(AutomationProperty idProp) { // if the hwnd is a winform, then return the Winform id otherwise let // UIAutomation do the job if (idProp == AutomationElement.AutomationIdProperty) { // Winforms have a special way to obtain the id if (WindowsFormsHelper.IsWindowsFormsControl(_hwnd, ref _windowsForms)) { string sPersistentID = WindowsFormsHelper.WindowsFormsID(_hwnd); return(string.IsNullOrEmpty(sPersistentID) ? null : sPersistentID); } } else if (idProp == AutomationElement.NameProperty) { string name; // If this is a winforms control and the AccessibleName is set, use it. if (WindowsFormsHelper.IsWindowsFormsControl(_hwnd, ref _windowsForms)) { name = GetAccessibleName(NativeMethods.CHILD_SELF); if (!string.IsNullOrEmpty(name)) { return(name); } } // Only hwnd's can be labeled. name = LocalizedName; // PerSharp/PreFast will flag this as a warning 6507/56507: Prefer 'string.IsNullOrEmpty(name)' over checks for null and/or emptiness. // It is valid to set LocalizedName to an empty string. LocalizedName being an // empty string will prevent the SendMessage(WM_GETTEXT) call. #pragma warning suppress 6507 if (name == null && GetParent() == null) { if (_fControlHasLabel) { IntPtr label = Misc.GetLabelhwnd(_hwnd); name = Misc.GetControlName(label, true); if (!string.IsNullOrEmpty(name)) { _controlLabel = label; } } else { name = Misc.ProxyGetText(_hwnd); } } // If name is still null, and we have an IAccessible, try it: // this picks up names on HWNDs set through Dynamic Annotation // (eg. on the richedits in Windows Mail), and holds us over till // we add DA support to UIA properly. if (String.IsNullOrEmpty(name)) { name = GetAccessibleName(NativeMethods.CHILD_SELF); } return(name); } // Only hwnd's can be labeled. else if (idProp == AutomationElement.LabeledByProperty && _fControlHasLabel) { // This is called to make sure that _controlLabel gets set. object name = GetElementProperty(AutomationElement.NameProperty); // If a control has a LocalizedName, the _controlLabel will not get set. // So look for it now. if (_controlLabel == IntPtr.Zero && name != null && GetParent() == null) { _controlLabel = Misc.GetLabelhwnd(_hwnd); } // If we have a cached _controlLabel that means that the name property we just got // was retreived from the label of the control and not its text or something else. If this // is the case expose it as the label. if (_controlLabel != IntPtr.Zero) { return(AutomationInteropProvider.HostProviderFromHandle(_controlLabel)); } } else if (idProp == AutomationElement.IsOffscreenProperty) { if (!SafeNativeMethods.IsWindowVisible(_hwnd)) { return(true); } IntPtr hwndParent = Misc.GetParent(_hwnd); // Check if rect is within rect of parent. Don't do this for top-level windows, // however, since the win32 desktop hwnd claims to have a rect only as large as the // primary monitor, making hwnds on other monitors seem clipped. if (hwndParent != IntPtr.Zero && hwndParent != UnsafeNativeMethods.GetDesktopWindow()) { NativeMethods.Win32Rect parentRect = NativeMethods.Win32Rect.Empty; if (Misc.GetClientRectInScreenCoordinates(hwndParent, ref parentRect) && !parentRect.IsEmpty) { Rect itemRect = BoundingRectangle; if (!itemRect.IsEmpty && !Misc.IsItemVisible(ref parentRect, ref itemRect)) { return(true); } } } } return(base.GetElementProperty(idProp)); }