public void MyTestInitialize()
        {
            // Create the factory and register schemas
            _nFactory = new CUIAutomationClass();

            // Start the app
            var curDir = Environment.CurrentDirectory;
            _app = new TargetApp(curDir + "\\WpfAppWithAdvTextControl.exe");
            _app.Start();

            // Find the main control
            var appElement = _nFactory.ElementFromHandle(_app.MainWindow);

            var advTestBoxCondition = _nFactory.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "advTextBox1");
            _nAdvancedTextBoxElement = appElement.FindFirst(NTreeScope.TreeScope_Children, advTestBoxCondition);
            Assert.IsNotNull(_nAdvancedTextBoxElement);

            var testControlCondition = _nFactory.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "testControl");
            _nTestControlElement = appElement.FindFirst(NTreeScope.TreeScope_Children, testControlCondition);
            Assert.IsNotNull(_nTestControlElement);

            var window = AutomationElement.RootElement.FindFirst(WTreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "MainWindow"));
            _wAdvancedTextBoxElement = window.FindFirst(WTreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "advTextBox1"));
            _wTestControlElement = window.FindFirst(WTreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "testControl"));
        }
Exemple #2
0
 public AutomationElement FromHandle(IntPtr hwnd)
 {
     try {
         IUIAutomationElement pElement;
         pAutomation.ElementFromHandle(hwnd, out pElement);
         return(pElement == null ? null : new AutomationElement(pElement, this));
     }
     catch (COMException) {
         return(null);
     }
 }
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // BuildListOfHyperlinksFromWindowInternal()
        //
        // Retrieve a list of hyperlinks from a browser window.
        //
        // Runs on the background thread.
        //
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void BuildListOfHyperlinksFromWindowInternal(bool fUseCache, bool fSearchLinkChildren)
        {
            // If we're already building up a list of links, ignore this request to refresh the list.
            // (A shipping app might consider queueing this request in order to refresh the list again
            // once the in-progress refreshing action is complete.)
            if (_fRefreshInProgress)
            {
                return;
            }

            _fRefreshInProgress = true;

            // Get a handle to the window of interest.
            IntPtr hwnd = Win32.FindWindow(strBrowserWindowClass, null);

            if (hwnd != IntPtr.Zero)
            {
                // Get a UIAutomationElement associated with this browser window. Note that the IUIAutomation
                // interface has other useful functions for retrieving automation elements. For example:
                //
                //   ElementFromPoint() - convenient when getting the UIA element beneath the mouse cursor.
                //   GetFocusedElement() - convenient when you need the UIA element for whatever control
                //                         currently has keyboard focus.
                //
                // All these functions have cache-related equivalents which can reduce the
                // time it takes to work with the element once it's been retrieved.

                IUIAutomationElement element = _automation.ElementFromHandle(hwnd);
                if (element != null)
                {
                    // If all we needed to do is get a few properties of the element we now have, we could
                    // make the get_Current* calls such shown below.) But these would incur the time cost of
                    // additional cross-proc calls.)

                    string strName = element.CurrentName;
                    // Do something with the name...

                    tagRECT rect = element.CurrentBoundingRectangle;
                    // Do something with the bounding rect...

                    // Rather than doing the above, a shipping app might choose to request the name
                    // and bounding rect of the browser when it retrieves the browser element. It
                    // could do this by calling ElementFromHandleBuildCache() supplying a cache
                    // request which included the properties it needs. By doing that, there would
                    // only be one cross-proc call rather than the three involved with the above steps.

                    // For this sample, we'll build up a list of all hyperlinks in the browser window.
                    BuildListOfHyperlinksFromElement(element, fUseCache, fSearchLinkChildren);
                }
            }

            // Allow another refresh to be performed now.
            _fRefreshInProgress = false;
        }
        public Guid?FindElement(Session session, string locator, string value, Guid?elementId)
        {
            var sw = Stopwatch.StartNew();

            while (sw.Elapsed < session.Timeouts.Implicit)
            {
                var conditions = new List <IUIAutomationCondition>();

                switch (locator.ToLowerInvariant())
                {
                case "name":
                    conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ValueValuePropertyId, value));
                    conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, value));
                    break;

                case "id":
                    conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, value));
                    break;

                default:
                    throw new NotSupportedException();
                }

                var container = elementId.HasValue
                    ? GetUIAutomationElement(elementId.Value)
                    : _automation.ElementFromHandle(session.Process.MainWindowHandle);

                var condition = _automation.CreateOrConditionFromArray(conditions.ToArray());
                var element   = container.FindFirst(TreeScope.TreeScope_Descendants, condition);
                if (element != null)
                {
                    return(_elementRepository.AddByHandle(element.CurrentNativeWindowHandle.ToInt32()));
                }

                Thread.Sleep(500);
            }

            return(null);
        }
        /// <summary>
        /// Enumerates the automation elements from the given window
        /// </summary>
        /// <param name="hWnd">The window handle</param>
        /// <returns>All of the automation elements found</returns>
        private List <IUIAutomationElement> EnumElements(IntPtr hWnd)
        {
            var result            = new List <IUIAutomationElement>();
            var automationElement = _automation.ElementFromHandle(hWnd);

            var conditionControlView    = _automation.ControlViewCondition;
            var conditionEnabled        = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_IsEnabledPropertyId, true);
            var enabledControlCondition = _automation.CreateAndCondition(conditionControlView, conditionEnabled);

            var conditionOnScreen = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_IsOffscreenPropertyId, false);
            var condition         = _automation.CreateAndCondition(enabledControlCondition, conditionOnScreen);

            var elementArray = automationElement.FindAll(TreeScope.TreeScope_Descendants, condition);

            for (var i = 0; i < elementArray.Length; ++i)
            {
                result.Add(elementArray.GetElement(i));
            }

            return(result);
        }
        public void MyTestInitialize()
        {
            // Create the factory and register schemas
            _factory = new CUIAutomationClass();
            ReadyStateSchema.GetInstance().Register();
            TestSchema.GetInstance().Register();
            ColorSchema.GetInstance().Register();

            // Start the app
            var curDir = Environment.CurrentDirectory;
            _app = new TargetApp(curDir + "\\UiaControls.exe");
            _app.Start();

            // Find the main control
            var appElement = _factory.ElementFromHandle(_app.MainWindow);
            var condition = _factory.CreatePropertyCondition(
                UIA_PropertyIds.UIA_AutomationIdPropertyId,
                "triColorControl1");
            _customElement = appElement.FindFirst(TreeScope.TreeScope_Children,
                                                  condition);
            Assert.IsNotNull(_customElement);
        }
Exemple #7
0
        public void MyTestInitialize()
        {
            // Create the factory and register schemas
            _factory = new CUIAutomationClass();
            ReadyStateSchema.GetInstance().Register();
            TestSchema.GetInstance().Register();
            ColorSchema.GetInstance().Register();

            // Start the app
            var curDir = Environment.CurrentDirectory;

            _app = new TargetApp(curDir + "\\UiaControls.exe");
            _app.Start();

            // Find the main control
            var appElement = _factory.ElementFromHandle(_app.MainWindow);
            var condition  = _factory.CreatePropertyCondition(
                UIA_PropertyIds.UIA_AutomationIdPropertyId,
                "triColorControl1");

            _customElement = appElement.FindFirst(TreeScope.TreeScope_Children,
                                                  condition);
            Assert.IsNotNull(_customElement);
        }
Exemple #8
0
        public List <DrawItem> execute()
        {
            List <DrawItem> items = new List <DrawItem>();

            IUIAutomationElement boxToBorder;
            IUIAutomationElement tmpElement;
            Stopwatch            watch = new Stopwatch();

            watch.Reset();
            watch.Start();
            IntPtr storeHwnd = IntPtr.Zero;

            foreach (Process proc in Process.GetProcesses())
            {
                if (proc.MainWindowTitle.StartsWith("Simplify3D"))
                {
                    storeHwnd = proc.MainWindowHandle;
                }
            }

            // IntPtr storeHwnd = NativeMethods.FindWindow ("Qt5QWindowIcon", "Simplify3D (Licensed to ...)");
            IntPtr fffSetings = NativeMethods.FindWindow("Qt5QWindowIcon", "FFF settings");

            bool exit = false;

            if (storeHwnd == IntPtr.Zero)
            {
                exit = true;
            }
            if (storeHwnd != NativeMethods.GetForegroundWindow() && fffSetings != NativeMethods.GetForegroundWindow())
            {
                exit = true;
            }

            if (exit)
            {
                //Highlighter.BufferList(new List<DrawItem>());
                return(items);
            }

            IUIAutomationElement windowApp = _automation.ElementFromHandle(storeHwnd);

            IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[1];
            conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "FFF Settings");
            IUIAutomationCondition conditions    = _automation.CreateAndConditionFromArray(conditionArray);
            IUIAutomationElement   settingsPanel = windowApp.FindFirst(TreeScope.TreeScope_Children, conditionArray[0]);

            //can u see ?
            if (settingsPanel != null)
            {
                try
                {
                    conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Auto-Configure for Material");
                    boxToBorder       = settingsPanel.FindFirst(TreeScope.TreeScope_Descendants, conditionArray[0]);
                    items.Add(new DrawItem(false, boxToBorder.CurrentBoundingRectangle, Color.Orange));
                    conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Auto-Configure for Print Quality");
                    boxToBorder       = settingsPanel.FindFirst(TreeScope.TreeScope_Descendants, conditionArray[0]);
                    items.Add(new DrawItem(false, boxToBorder.CurrentBoundingRectangle, Color.Blue));

                    pane = this.GetSettingsMainPane(settingsPanel);
                    var rowElement = GetRowOfTabs(settingsPanel);

                    var columnElement = GetColumnFromTabRow(rowElement, EXTRUDER);
                    items.Add(new DrawItem(false, columnElement.CurrentBoundingRectangle, Color.Orange));
                    columnElement = GetColumnFromTabRow(rowElement, LAYER);
                    items.Add(new DrawItem(false, columnElement.CurrentBoundingRectangle, Color.Blue));
                    columnElement = GetColumnFromTabRow(rowElement, ADDITIONS);
                    items.Add(new DrawItem(false, columnElement.CurrentBoundingRectangle, Color.Blue));
                    columnElement = GetColumnFromTabRow(rowElement, INFILL);
                    items.Add(new DrawItem(false, columnElement.CurrentBoundingRectangle, Color.Blue));
                    columnElement = GetColumnFromTabRow(rowElement, SUPPORT);
                    items.Add(new DrawItem(false, columnElement.CurrentBoundingRectangle, Color.Blue));
                    columnElement = GetColumnFromTabRow(rowElement, TEMPERATURE);
                    items.Add(new DrawItem(false, columnElement.CurrentBoundingRectangle, Color.Orange));
                    columnElement = GetColumnFromTabRow(rowElement, COOLING);
                    items.Add(new DrawItem(false, columnElement.CurrentBoundingRectangle, Color.Orange));

                    if (rowElement.CurrentName == EXTRUDER)
                    {
                        tmpElement = this.GetElementFromPane(pane, "Extrusion Multiplier");
                        items.Add(new DrawItem(true, tmpElement.CurrentBoundingRectangle, Color.Orange));
                    }
                    else if (rowElement.CurrentName == LAYER)
                    {
                        tmpElement = this.GetElementFromPane(pane, "Primary Layer Height");
                        items.Add(new DrawItem(true, tmpElement.CurrentBoundingRectangle, Color.Blue));
                        tmpElement = this.GetElementFromPane(pane, "Top Solid Layers");
                        items.Add(new DrawItem(true, tmpElement.CurrentBoundingRectangle, Color.Blue));
                        tmpElement = this.GetElementFromPane(pane, "Bottom Solid Layers");
                        items.Add(new DrawItem(true, tmpElement.CurrentBoundingRectangle, Color.Blue));
                    }
                    else if (rowElement.CurrentName == ADDITIONS)
                    {
                        tmpElement = this.GetElementFromPane(pane, "Skirt Layers");
                        items.Add(new DrawItem(true, tmpElement.CurrentBoundingRectangle, Color.Blue));
                    }
                    else if (rowElement.CurrentName == INFILL)
                    {
                        tmpElement = this.GetElementFromPane(pane, "Interior Fill Percentage");
                        items.Add(new DrawItem(true, tmpElement.CurrentBoundingRectangle, Color.Blue));
                    }
                    else if (rowElement.CurrentName == SUPPORT)
                    {
                        tmpElement = this.GetElementFromPane(pane, "Support Infill Percentage");
                        items.Add(new DrawItem(true, tmpElement.CurrentBoundingRectangle, Color.Blue));
                    }
                    else if (rowElement.CurrentName == TEMPERATURE)
                    {
                        tmpElement = this.GetBoxFromPane(pane, UIA_ControlTypeIds.UIA_ListControlTypeId);
                        items.Add(new DrawItem(true, tmpElement.CurrentBoundingRectangle, Color.Orange));
                    }
                    else if (rowElement.CurrentName == COOLING)
                    {
                        tmpElement = this.GetBoxFromPane(pane, UIA_ControlTypeIds.UIA_TreeControlTypeId);
                        items.Add(new DrawItem(true, tmpElement.CurrentBoundingRectangle, Color.Orange));
                    }
                }
                catch (NullReferenceException)
                {
                    Console.WriteLine("shit happens");
                }

                watch.Stop();
                int timeout = (1000 / 75) - (int)watch.ElapsedMilliseconds;

                //Thread.Sleep(timeout);
            }
            //Highlighter.BufferList(items);
            return(items);
            // grab the context from the state
        }
Exemple #9
0
 public virtual IUIAutomationElement GetUIAutomationElement(IUIAutomation automation)
 {
     return(automation.ElementFromHandle(new IntPtr(_handle)));
 }
Exemple #10
0
        public string GetMail()
        {
            string strMailContent = "";

            // Try to find a Windows Live Mail window for composing and reading e-mails.
            // Using the Spy tool, the class of the main window can be found. This test
            // app assumes there's only one Windows Live Mail window of interest.
            IntPtr hwnd = Win32.FindWindow("ATH_Note", null);

            if (hwnd != IntPtr.Zero)
            {
                // We found a window, so get the UIA element associated with the window.
                IUIAutomationElement elementMailAppWindow = _automation.ElementFromHandle(
                    hwnd);

                // Find an element somewhere beneath the main window element in the UIA
                // tree which represents the main area where the e-mail content is shown.
                // Using the Inspect SDK tool, we can see that the main e-mail content
                // is contained within an element whose accessible name is "NoteWindow".
                // So create a condition such that the FindFirst() call below will only
                // return an element if its name is "NoteWindow".
                IUIAutomationCondition conditionNote = _automation.CreatePropertyCondition(
                    _propertyIdName, "NoteWindow");

                IUIAutomationElement elementNoteWindow = elementMailAppWindow.FindFirst(
                    TreeScope.TreeScope_Descendants,
                    conditionNote);

                // As it happens, the actual element that supports the Text Pattern is
                // somewhere beneath the "NoteWindow" element in the UIA tree. Using
                // Inspect we can see that there is an element that supports the
                // Text Pattern. Once we have that element, we can avoid a cross-process
                // call to access the Text Pattern object by using cache request.
                IUIAutomationCacheRequest cacheRequest = _automation.CreateCacheRequest();
                cacheRequest.AddPattern(_patternIdTextPattern);

                // Now find the element that supports the Text Pattern. This test app assumes
                // there’s only one element that can be returned which supports the Text Pattern.
                bool fTextPatternSupported = true;
                IUIAutomationCondition conditionTextPattern = _automation.CreatePropertyCondition(
                    _propertyIdIsTextPatternAvailable,
                    fTextPatternSupported);

                IUIAutomationElement elementMailContent = elementMailAppWindow.FindFirstBuildCache(
                    TreeScope.TreeScope_Descendants,
                    conditionTextPattern,
                    cacheRequest);

                // Because the Text Pattern object is cached, we don't have to make a cross-process
                // call here to get object. Later calls which actually use methods and properties
                // on the Text Pattern object will incur cross-proc calls.
                IUIAutomationTextPattern textPattern = (IUIAutomationTextPattern)
                                                       elementMailContent.GetCachedPattern(
                    _patternIdTextPattern);

                // This test app is only interested in getting all the e-mail text, so we get that through
                // the DocumentRange property. A more fully featured app might be interested in getting a
                // collection of Text Ranges from the e-mail. Each range might relate to an individual
                // word or paragraph. Given that a provider which supports the Text Pattern allows a
                // client to find the bounding rectangles of these ranges, the client could choose to use
                // its own method of highlighting the text as the text is being spoken.
                IUIAutomationTextRange rangeDocument = textPattern.DocumentRange;

                // Pass -1 here because we're not interested in limiting the amount of text here.
                strMailContent = rangeDocument.GetText(-1);
            }

            return(strMailContent);
        }
Exemple #11
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (count < 4)
            {
                title = list[count];
                count++;
            }
            else
            {
                count = 0;
                title = list[count];
                count++;
            }
            //找到QQ
            Process[] process = Process.GetProcessesByName("QQ");

            foreach (Process p in process)
            {
                textBox1.Clear();
                //從QQ產生root automation
                IUIAutomationElement _viveportAutomationElement = _automation.ElementFromHandle(
                    p.MainWindowHandle);

                if (_viveportAutomationElement == null)
                {
                    throw new InvalidOperationException("QQ must be running");
                }
                //依據屬性名稱 找出tabbox
                IUIAutomationCondition conditionNote = _automation.CreatePropertyCondition(
                    _propertyIdName, title);

                IUIAutomationElement _tabBoxAutomationElement = _viveportAutomationElement.FindFirst(
                    TreeScope.TreeScope_Descendants,
                    conditionNote);
                if (_tabBoxAutomationElement == null)
                {
                    throw new InvalidOperationException("Could not find " + title);
                }
                //由tab找出按鍵範圍 移動游標點擊
                tagPOINT point = new tagPOINT();
                _tabBoxAutomationElement.GetClickablePoint(out point);
                Cursor.Position = new Point((int)point.x, (int)point.y);
                LeftClick();
                Thread.Sleep(150);
                //找出QQ聊天室顯示框
                conditionNote = _automation.CreatePropertyCondition(
                    _propertyIdName, "消息");

                IUIAutomationElement _resultTextBoxAutomationElement = _viveportAutomationElement.FindFirst(
                    TreeScope.TreeScope_Descendants,
                    conditionNote);

                if (_resultTextBoxAutomationElement == null)
                {
                    throw new InvalidOperationException("Could not find 消息");
                }
                //取出消息
                UIAutomationClient.IUIAutomationLegacyIAccessiblePattern legacyPattern1 = (UIAutomationClient.IUIAutomationLegacyIAccessiblePattern)_resultTextBoxAutomationElement.GetCurrentPattern(patternId);

                //放到texebox
                List <char> list = legacyPattern1.CurrentValue.ToList();
                foreach (char i in list)
                {
                    if (i != '\r')
                    {
                        textBox1.AppendText(i.ToString());
                    }
                    else
                    {
                        textBox1.AppendText(Environment.NewLine);
                    }
                }
            }
        }
Exemple #12
0
 public virtual IUIAutomationElement GetUIAutomationElement(IUIAutomation automation)
 {
     return automation.ElementFromHandle(new IntPtr(_handle));
 }