public ViewTree(IUIAutomationElement element, IUIAutomation automation) { this.root = new TreeNode(element); this.root.parent = null; IUIAutomationElementArray array = element.FindAll(TreeScope.TreeScope_Children, automation.CreateTrueCondition()); if (0 == array.Length) { this.root.children = null; this.root.isLeaf = true; } else { for (int i = 0; i < array.Length; i++) { IUIAutomationElement e = array.GetElement(i); TreeNode n = new TreeNode(e); this.root.children.Add(n); } } }
public static bool ExpandAll(this IUIAutomationElement element, IUIAutomation automation, int level) { if (element.CurrentIsOffscreen != 0) { return(false); } var returnValue = false; if (element.GetCurrentPattern(UIA_PatternIds.UIA_ExpandCollapsePatternId) is IUIAutomationExpandCollapsePattern expandCollapsePattern && expandCollapsePattern.CurrentExpandCollapseState != ExpandCollapseState.ExpandCollapseState_LeafNode) { returnValue = true; // TODO: find timing issue and resolve structurally. This issue shows with CalcVolume Thread.Sleep(100); expandCollapsePattern.Expand(); } var condition = automation.CreatePropertyCondition(UIA_PropertyIds.UIA_IsEnabledPropertyId, true); var item = element.FindAll(TreeScope.TreeScope_Children, condition); for (var i = 0; i < item.Length; i++) { if (ExpandAll(item.GetElement(i), automation, level + 1)) { returnValue = true; } } return(returnValue); }
public static bool CollapseAll(this IUIAutomationElement element, IUIAutomation automation) { if (element.CurrentIsOffscreen != 0) { return(false); } var returnValue = false; var condition = automation.CreatePropertyCondition(UIA_PropertyIds.UIA_IsEnabledPropertyId, true); var item = element.FindAll(TreeScope.TreeScope_Children, condition); for (var i = 0; i < item.Length; i++) { if (CollapseAll(item.GetElement(i), automation)) { returnValue = true; } } if (!(element.GetCurrentPattern(UIA_PatternIds.UIA_ExpandCollapsePatternId) is IUIAutomationExpandCollapsePattern expandCollapsePattern) || expandCollapsePattern.CurrentExpandCollapseState == ExpandCollapseState.ExpandCollapseState_LeafNode) { return(returnValue); } expandCollapsePattern.Collapse(); return(true); }
/// <summary> /// Given an <see cref="IUIAutomationElement"/>, returns all descendants with the given <paramref name="className"/>. /// If none are found, the resulting collection will be empty. /// </summary> /// <returns></returns> public static IUIAutomationElementArray FindDescendantsByClass(this IUIAutomationElement parent, string className) { if (parent == null) { throw new ArgumentNullException(nameof(parent)); } var condition = Helper.Automation.CreatePropertyCondition(AutomationElementIdentifiers.ClassNameProperty.Id, className); return(parent.FindAll(TreeScope.TreeScope_Descendants, condition)); }
public Element[] GetAllElements(TreeScope Scope) { IUIAutomationElementArray elementsToFind = null; elementsToFind = IUIElement.FindAll(Scope, new CUIAutomationClass().CreateTrueCondition()); int size = elementsToFind.Length; Element[] arr = new Element[size]; for (int i = 0; i < size; i++) { arr[i] = new Element(elementsToFind.GetElement(i)); } return(arr); }
//public void RefreshScrollingContainer(IntPtr hwnd) //{ //} public IUIAutomationElement[] GetAppsArray(IUIAutomationElement homeElement) { IUIAutomationCondition condition = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ListItemControlTypeId); IUIAutomationElementArray appsList = homeElement.FindAll(TreeScope.TreeScope_Children, condition); Console.WriteLine(appsList.Length); IUIAutomationElement[] appsArray = new IUIAutomationElement[appsList.Length]; for (int i = 0; i < appsList.Length; i++) { appsArray[i] = appsList.GetElement(i); Console.WriteLine(appsArray[i].CurrentAutomationId); } return(appsArray); }
public IUIAutomationElement[] GetTopFreeHomeArray(IUIAutomationElement element) { IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[2]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Top free"); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ListItemControlTypeId); IUIAutomationCondition conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElementArray topFreeList = element.FindAll(TreeScope.TreeScope_Children, conditions); IUIAutomationElement[] topFreeArray = new IUIAutomationElement[topFreeList.Length]; for (int i = 0; i < topFreeList.Length; i++) { topFreeArray[i] = topFreeList.GetElement(i); } return(topFreeArray); }
/// <summary> /// Get DesktopElements based on Process Id. /// </summary> /// <param name="pid"></param> /// <returns>return null if we fail to get elements by process Id</returns> public static IEnumerable <DesktopElement> ElementsFromProcessId(int pid) { IUIAutomationElement root = null; IEnumerable <DesktopElement> elements = null; IUIAutomationCondition condition = null; IUIAutomationElementArray elementArray = null; try { // check whether process exists first. // if not, it will throw an ArgumentException using (var proc = Process.GetProcessById(pid)) { root = UIAutomation.GetRootElement(); condition = UIAutomation.CreatePropertyCondition(PropertyType.UIA_ProcessIdPropertyId, pid); elementArray = root.FindAll(TreeScope.TreeScope_Children, condition); elements = ElementsFromUIAElements(elementArray); } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { // report and let it return null ex.ReportException(); } #pragma warning restore CA1031 // Do not catch general exception types finally { if (root != null) { Marshal.ReleaseComObject(root); } if (condition != null) { Marshal.ReleaseComObject(condition); } if (elementArray != null) { Marshal.ReleaseComObject(elementArray); } } return(elements); }
private static IUIAutomationElement FindAndWait(IUIAutomationElement rootElement, TreeScope scope, IUIAutomationCondition condition, TimeSpan timeout) { var stopwatch = Stopwatch.StartNew(); do { var result = rootElement.FindAll(scope, condition); if (result.Length > 1) { throw new Exception("Found multiple elements"); } else if (result.Length == 1) { return(result.GetElement(0)); } Thread.Sleep(1); } while (stopwatch.Elapsed < timeout); throw new TimeoutException(); }
public static bool IsDialogWindow() { //Stopwatch diagTimer = new Stopwatch(); //diagTimer.Start(); bool result = false; var array_windows = appElement.FindAll(TreeScope.TreeScope_Children, _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_WindowControlTypeId)); for (int i = 0; i < array_windows.Length; i++) { string s_name = array_windows.GetElement(i).CurrentName; if (s_name.Contains("Игра проиграна") || s_name.Contains("Игра выиграна") || s_name.Contains("Новая игра")) { result = true; } } //diagTimer.Stop(); //Console.WriteLine("IGS {0}",diagTimeToString(diagTimer.Elapsed)); return(result); }
public static IUIAutomationElementArray xtGetAllChildren(this IUIAutomationElement element) { return(element.FindAll(TreeScope.TreeScope_Children, AUTOCLASS.CreateTrueCondition())); }
////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // BuildListOfHyperlinksFromElement() // // Retrieve a list of hyperlinks from a UIAutomation element. Notifies the main // UI thread when it's found all the hyperlinks to be added to the app's list of // hyperlinks. // // Runs on the background thread. // ////////////////////////////////////////////////////////////////////////////////////////////////////////////// private void BuildListOfHyperlinksFromElement(IUIAutomationElement elementBrowser, bool fUseCache, bool fSearchLinkChildren) { IUIAutomationCacheRequest cacheRequest = null; // If a cache is used, then for each of the elements returned to us after a search // for elements, specific properties (and patterns), can be cached with the element. // This means that when we access one of the properties later, a cross-proc call // does not have to be made. (But it also means that when such a call is made, we // don't learn whether the original element still exists.) if (fUseCache) { // Create a cache request containing all the properties and patterns // we will need once we've retrieved the hyperlinks. By using this // cache, when can avoid time-consuming cross-process calls when // getting hyperlink properties later. cacheRequest = _automation.CreateCacheRequest(); // We'll need the hyperlink's name and bounding rectangle later. // A full list of Automation element properties can be found at // http://msdn.microsoft.com/en-us/library/ee684017(v=VS.85).aspx. cacheRequest.AddProperty(_propertyIdName); cacheRequest.AddProperty(_propertyIdBoundingRectangle); // The target of the hyperlink might be stored in the Value property of // the hyperlink. The Value property is only avaliable if an element // supports the Value pattern. This sample doesn't use the Value, but // if it did, it would call the following here. // hr = pCacheRequest->AddProperty(UIA_ValueValuePropertyId); // It's ok to attempt to cache a property on a pattern which might not // exist on the cached elements. In that case, the property just won't // be available when we try to retrieve it from the cache later. // Note that calling AddPattern() does not cache the properties // associated with a pattern. The pattern's properties must be // added explicitly to the cache if required. // Cache the Invoke pattern too. This means when we prepare to invoke a link later, // we won't need to make a cross-proc call during that preparation. (A cross-proc // call will occur at the time Invoke() is actually called.) A full list of patterns // can be found at http://msdn.microsoft.com/en-us/library/ee684023(v=VS.85).aspx. cacheRequest.AddPattern(_patternIdInvoke); // The next step is to specify for which elements we want to have the properties, (and // pattern) cached. By default, caching will occur on each element found in the search // below. But we can also request that the data is cached for direct children of the // elements found, or even all the descendants of the elements founds. (A scope of // Parent or Ancestors cannot be used in a cached request.) // So in this sample, if TreeScope_Element is used as the scope here, (which is the // default), then only properties for the found hyperlinks will be cached. The sample // optionally caches the properties for the direct children of the hyperlinks too. // This means that if we find a hyperlink with no name, we can search the hyperlink's // children to see if one of the child elements has a name we can use. (Searching the // children could be done without using the cache, but it would incur the time cost of // making cross-proc calls.) TreeScope scope = TreeScope.TreeScope_Element; if (fSearchLinkChildren) { scope = scope | TreeScope.TreeScope_Children; } cacheRequest.TreeScope = scope; // Note: By default the cache request has a Mode of Full. This means a reference to the // target element is included in the cache, as well as whatever properties and patterns // we specified should be in the cache. With a reference to the target element, we can: // // (i) Retrieve a property later for an element which we didn't request should be in // the cache. Eg we could call get_CurrentHasKeyboardFocus(). // // (ii) We can call a method of a pattern that the element supports. Eg if Full mode was // not used here, we would not be able to call Invoke() on the hyperlink later. // If we specified a Mode of None for the cache request here, then the results only include // cached data, with no connection at all after the call returns to the source elements. If // only data is required, then it would be preference to use a Mode of None, as less work is // required by UIA. (Also, if a reference to the element is returned in the cache and kept // around for a non-trivial time, then it increases the chances that the target process // attempts to free the element, but it can't do so in a clean manner as it would like, // due to the client app here holding a reference to it.) To specify that we want a Mode of // None, we'd make this call here: // cacheRequest.AutomationElementMode = AutomationElementMode.AutomationElementMode_None; } // Now regardless of whether we're using a cache, we need to specify which elements // we're interested in during our search for elements. We do this by building up a // property condition. This property condition tells UIA which properties must be // satisfied by an element for it to be included in the search results. We can // combine a number of properties with AND and OR logic. // We shall first say that we're only interested in elements that exist in the Control view. // By default, a property condition uses the Raw view, which means that every element in the // target browser's UIA tree will be examined. The Control view is a subset of the Raw view, // and only includes elements which present some useful UI. (The Raw view might include // container elements which simply group elements logically together, but the containers // themselves might have no visual representation on the screen.) IUIAutomationCondition conditionControlView = _automation.ControlViewCondition; IUIAutomationCondition conditionHyperlink = _automation.CreatePropertyCondition(_propertyIdControlType, _controlTypeIdHyperlink); // Now combine these two properties such that the search results only contain // elements that are in the Control view AND are hyperlinks. We would get the // same results here if we didn't include the Control view clause, (as hyperlinks // won't exist only in the Raw view), but by specifying that we're only interested // in the Control view, UIA won't bother checking all the elements that only exist // in the Raw view to see if they're hyperlinks. IUIAutomationCondition condition = _automation.CreateAndCondition(conditionControlView, conditionHyperlink); // Now retrieve all the hyperlinks in the browser. We must specify a scope in the Find calls here, // to control how far UIA will go in looking for elements to include in the search results. For // this sample, we must check all descendants of the browser window. // *** Note: use TreeScope_Descendants with care, as depending on what you're searching for, UIA may // have to process potentially thousands of elements. For example, if you only need to find top level // windows on your desktop, you would search for TreeScope_Children of the root of the UIA tree. (The // root element can be found with a call to IUIAutomation::GetRootElement().) // *** Note: If the following searches included searching for elements in the client app's own UI, // then the calls must be made on a background thread. (ie not your main UI thread.) Once event // handlers are used, then it's preferable to have all UIA calls made on a background thread // regardless of whether the app interacts with its own UI. IUIAutomationElementArray elementArray; if (fUseCache) { elementArray = elementBrowser.FindAllBuildCache(TreeScope.TreeScope_Descendants, condition, cacheRequest); } else { elementArray = elementBrowser.FindAll(TreeScope.TreeScope_Descendants, condition); } // Build up a list of items to be passed to the main thread in order for it to // populate the list of hyperlinks shown in the UI. _linkItems.Clear(); if (elementArray != null) { // Find the number of hyperlinks returned by the search. (The number of hyperlinks // found might be zero if the browser window is minimized.) int cLinks = elementArray.Length; // Process each returned hyperlink element. for (int idxLink = 0; idxLink < cLinks; ++idxLink) { IUIAutomationElement elementLink = elementArray.GetElement(idxLink); // Get the name property for the hyperlink element. How we get this depends // on whether we requested that the property should be cached or not. string strLinkName = null; if (fUseCache) { strLinkName = GetCachedDataFromElement(elementLink, fSearchLinkChildren); } else { strLinkName = GetCurrentDataFromElement(elementLink, fSearchLinkChildren); } // If we have non-null name, add the link to the list. (This sample does not check // for names that only contains whitespace.) if (strLinkName != null) { strLinkName = strLinkName.Trim(); LinkItem item = new LinkItem(); item.linkName = strLinkName; item.element = elementLink; _linkItems.Add(item); } } } // Notify the main UI thread that a list of links is ready for processing. Do not block in this call. _listViewLinks.BeginInvoke(_UIUpdateDelegate, _linkItems); }
static public MSGF_Data ParseMineSweeperGameField() { MSGF_Data result = new MSGF_Data(); result.Xcells = 0; appElement = _automation.ElementFromHandle(MineSweeperHWND); var array_rows = appElement.FindAll(TreeScope.TreeScope_Children, _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_CustomControlTypeId)); if (array_rows.Length > 1) { result.RowsCount = array_rows.Length - 1; /*var elem_texts = array_rows.GetElement(0).FindAll(TreeScope.TreeScope_Children, _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_TextControlTypeId)); * if (elem_texts.Length == 2) * { * result.TimeSecs = Convert.ToInt32(elem_texts.GetElement(0).GetCurrentPropertyValue(UIA_PropertyIds.UIA_ValueValuePropertyId)); * result.RemainingMines = Convert.ToInt32(elem_texts.GetElement(1).GetCurrentPropertyValue(UIA_PropertyIds.UIA_ValueValuePropertyId)); * }*/ // Ищем ячейки for (int j = 1; j < array_rows.Length; j++) { var elem_row = array_rows.GetElement(j); var array_cells = elem_row.FindAll(TreeScope.TreeScope_Children, _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ButtonControlTypeId)); if (j == 1) { result.ColsCount = array_cells.Length; result.cells = new MSGF_Cell[result.ColsCount, result.RowsCount]; } for (int i = 0; i < array_cells.Length; i++) { { string nm = array_cells.GetElement(i).CurrentName; result.cells[i, j - 1].elem = array_cells.GetElement(i); // Runtime Error Out of range if (nm.Contains("помечена")) { result.cells[i, j - 1].state = -2; continue; } if (nm.Contains("неоткрыта")) { result.cells[i, j - 1].state = -1; result.Xcells++; continue; } if (nm.Contains("мин нет")) { result.cells[i, j - 1].state = 0; continue; } if (nm.Contains("ячейки: 1")) { result.cells[i, j - 1].state = 1; continue; } if (nm.Contains("ячейки: 2")) { result.cells[i, j - 1].state = 2; continue; } if (nm.Contains("ячейки: 3")) { result.cells[i, j - 1].state = 3; continue; } if (nm.Contains("ячейки: 4")) { result.cells[i, j - 1].state = 4; continue; } if (nm.Contains("ячейки: 5")) { result.cells[i, j - 1].state = 5; continue; } if (nm.Contains("ячейки: 6")) { result.cells[i, j - 1].state = 6; continue; } if (nm.Contains("ячейки: 7")) { result.cells[i, j - 1].state = 7; continue; } if (nm.Contains("ячейки: 8")) { result.cells[i, j - 1].state = 8; continue; } } } } } return(result); }
public IUIAutomationElement[] GetTopFreeHomeArray(IUIAutomationElement element) { IUIAutomationCondition[] conditionArray = new IUIAutomationCondition[2]; conditionArray[0] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Top free"); conditionArray[1] = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ListItemControlTypeId); IUIAutomationCondition conditions = _automation.CreateAndConditionFromArray(conditionArray); IUIAutomationElementArray topFreeList = element.FindAll(TreeScope.TreeScope_Children, conditions); IUIAutomationElement[] topFreeArray = new IUIAutomationElement[topFreeList.Length]; for (int i = 0; i < topFreeList.Length; i++) { topFreeArray[i] = topFreeList.GetElement(i); } return topFreeArray; }
//public void RefreshScrollingContainer(IntPtr hwnd) //{ //} public IUIAutomationElement[] GetAppsArray(IUIAutomationElement homeElement) { IUIAutomationCondition condition = _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ListItemControlTypeId); IUIAutomationElementArray appsList = homeElement.FindAll(TreeScope.TreeScope_Children, condition); Console.WriteLine(appsList.Length); IUIAutomationElement[] appsArray = new IUIAutomationElement[appsList.Length]; for (int i = 0; i < appsList.Length; i++) { appsArray[i] = appsList.GetElement(i); Console.WriteLine(appsArray[i].CurrentAutomationId); } return appsArray; }