Exemple #1
0
        protected override void Dispose(bool disposing)
        {
            if (Pattern != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(Pattern);
                this.Pattern = null;
            }

            base.Dispose(disposing);
        }
Exemple #2
0
        public ValuePattern(A11yElement e, IUIAutomationValuePattern p) : base(e, PatternType.UIA_ValuePatternId)
        {
            Pattern = p;

            PopulateProperties();
        }
Exemple #3
0
 private ValuePattern(IUIAutomationValuePattern pattern)
 {
     _pattern = pattern;
 }
Exemple #4
0
        // Step 2: Find all the links of interest on the loaded page.
        private void buttonFindLinks_Click(object sender, EventArgs e)
        {
            checkedListBoxLinks.Items.Clear();

            if (webBrowserPage.Url == null)
            {
                MessageBox.Show(this,
                                "Please load the web page of interest.",
                                "Link Getter",
                                MessageBoxButtons.OK);

                labelLinkCount.Text = "No links found.";

                return;
            }

            // Get the UIA element for the webBrowser with the page of interest loaded.
            IUIAutomationElement elementBrowser =
                automation.ElementFromHandle(webBrowserPage.Handle);

            // Build up a cache request for all the data we need, to reduce the time it
            // takes to access the link data once with have the collection of links.
            IUIAutomationCacheRequest cacheRequest = automation.CreateCacheRequest();

            cacheRequest.AddProperty(propertyIdName);

            cacheRequest.AddPattern(patternIdValue);
            cacheRequest.AddProperty(propertyIdValueValue);

            // Assume the links have names as expected, and we don't need to
            // search children of the links for names.
            cacheRequest.TreeScope = TreeScope.TreeScope_Element;

            // We want the collection of all links on the page.
            IUIAutomationCondition conditionControlView = automation.ControlViewCondition;
            IUIAutomationCondition conditionHyperlink   =
                automation.CreatePropertyCondition(
                    propertyIdControlType, controlTypeIdHyperlink);

            IUIAutomationCondition finalCondition = automation.CreateAndCondition(
                conditionControlView, conditionHyperlink);

            // TODO: Call FindAllBuildCache() in a background thread in case it takes
            // a while. As it is, the app UI's going to freeze.

            // Now get the collection of links.
            IUIAutomationElementArray elementArray = elementBrowser.FindAllBuildCache(
                TreeScope.TreeScope_Descendants, finalCondition, cacheRequest);

            if (elementArray != null)
            {
                // Process each returned hyperlink element.
                for (int idxLink = 0; idxLink < elementArray.Length; ++idxLink)
                {
                    IUIAutomationElement elementLink = elementArray.GetElement(idxLink);

                    // Despite the fact that we've got the names of the UIA links, don't
                    // use that information here. Perhaps we will use it in the future.

                    IUIAutomationValuePattern valuePattern =
                        (IUIAutomationValuePattern)elementLink.GetCurrentPattern(
                            patternIdValue);
                    if (valuePattern != null)
                    {
                        // We're only interested in references the page makes to itself.
                        string strValueLink = valuePattern.CachedValue;
                        var    index        = strValueLink.IndexOf('#');
                        if ((index > 0) && strValueLink.StartsWith(textBoxURL.Text))
                        {
                            checkedListBoxLinks.Items.Add(new LinkItem()
                            {
                                linkName = elementLink.CachedName,
                                linkURL  = strValueLink
                            });
                        }
                    }
                }
            }

            // Let's assume we'll want most of the links found.
            SetLinkCheckedState(true);

            labelLinkCount.Text = "Count of links found: " +
                                  checkedListBoxLinks.Items.Count;
        }
Exemple #5
0
 private ValuePattern(IUIAutomationValuePattern pattern)
 {
     _pattern = pattern;
 }
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // GetCurrentDataFromElement()
        //
        // Get the current name from a UIA element, (incurring the time cost of various a cross-proc calls).
        // If the element doesn't have a name, optionally try to find a name from the children of the element
        // by using a TreeWalker.
        //
        // Runs on the background thread.
        //
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private string GetCurrentDataFromElement(IUIAutomationElement element, bool fSearchLinkChildren)
        {
            string strName = null;

            // Call back to the target app to retrieve the bounds of the hyperlink element.
            tagRECT rectBounds = element.CurrentBoundingRectangle;

            // If we're the hyperlink has a zero-size bounding rect, ignore the element.
            if ((rectBounds.right > rectBounds.left) && (rectBounds.bottom > rectBounds.top))
            {
                // Get the name of the element, (again incurring a cross-proc call). This name will often
                // be the text shown on the screen.
                string strNameFound = element.CurrentName;
                if ((strNameFound != null) && (strNameFound.Length > 0))
                {
                    // We have a usable name.
                    strName = strNameFound;
                }
                else
                {
                    // The hyperlink has no usable name. Consider using the name of a child element of the hyperlink.
                    if (fSearchLinkChildren)
                    {
                        // Use a Tree Walker here to try to find a child element of the hyperlink
                        // that has a name. Tree walking is a time consuming action, so would be
                        // avoided by a shipping app if alternatives like FindFirst, FindAll, or
                        // BuildUpdatedCache could get the required data.

                        IUIAutomationTreeWalker controlWalker = _automation.ControlViewWalker;

                        IUIAutomationElement elementChild = controlWalker.GetFirstChildElement(element);
                        while (elementChild != null)
                        {
                            string strNameChild = elementChild.CurrentName;
                            if ((strNameChild != null) && (strNameChild.Length > 0))
                            {
                                // Use the name of this child element.
                                strName = strNameChild;
                                break;
                            }

                            // Continue to the next child element.
                            elementChild = controlWalker.GetNextSiblingElement(elementChild);
                        }
                    }
                }
            }

            // While this sample doesn't use the destination of the hyperlink, if it wanted
            // to get the destination, that might be available as the Value property of the
            // element. The Value property is part of the Value pattern, and so is accessed
            // through the Value pattern.

            // Check first whether the element supports the Value pattern.
            IUIAutomationValuePattern valuePattern = (IUIAutomationValuePattern)element.GetCurrentPattern(_patternIdValue);

            if (valuePattern != null)
            {
                string strValue = valuePattern.CurrentValue;

                // This is where the destination of the link would be used...
            }

            return(strName);
        }
Exemple #7
0
 internal static ValuePattern Wrap(
     AutomationElement element,
     IUIAutomationValuePattern valuePattern)
 {
     return(new ValuePattern(element: element, valuePattern: valuePattern));
 }
Exemple #8
0
 ValuePattern(AutomationElement element, IUIAutomationValuePattern valuePattern)
     : base(el: element)
 {
     this._valuePattern = valuePattern;
 }
 private ValuePattern(AutomationElement el, IUIAutomationValuePattern pattern, bool cached)
     : base(el, cached)
 {
     Debug.Assert(pattern != null);
     this._pattern = pattern;
 }