public static string GetComboBoxSelection(this AutomationElement parent, string name) { AutomationElement box = parent.FindFirstWithRetries(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, name)); if (box == null) { throw new Exception("ComboBox '" + name + "' not found"); } if (!box.Current.IsEnabled) { throw new Exception("ComboBox '" + name + "' is not enabled"); } SelectionPattern si = (SelectionPattern)box.GetCurrentPattern(SelectionPattern.Pattern); AutomationElement[] array = si.Current.GetSelection(); if (array == null || array.Length == 0) { return(""); } AutomationElement item = array[0]; ValuePattern sip = (ValuePattern)item.GetCurrentPattern(ValuePattern.Pattern); return(sip.Current.Value); }
//change to get selected item public string GetSelected() { string nameofSelected = ""; if (_UIAElement != null) { object basePattern; if (_UIAElement.TryGetCurrentPattern(SelectionPattern.Pattern, out basePattern)) { SelectionPattern selection = (BasePattern)basePattern as SelectionPattern; if (selection != null) { AutomationElement[] automationElements = selection.Current.GetSelection(); foreach (var automationElement in automationElements) { nameofSelected = automationElement.Current.Name; } } } } else { if (PurpleElement.Current.IsEnabled) { GetSelected(); } } return(nameofSelected); }
private void GetSelection() { if ((comboBoxElement != null) && (comboBoxElement.Current.IsEnabled)) { SelectionPattern selPattern = comboBoxElement .GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern; AutomationElement[] items = selPattern.Current .GetSelection(); foreach (AutomationElement item in items) { int index = 0; foreach (var listItem in listBox1.Items) { if ((string)listItem == item.Current.Name) { listBox1.SelectedIndex = index; break; } index++; } } } SetForegroundWindow(this.Handle); }
void SetSelectedItem(dynamic itemParams, AutomationElement element, System.Drawing.Point point) { //Thread.Sleep(200); SelectionPattern selectionPattern = element.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern; var sel = selectionPattern.Current.GetSelection(); if (sel.Length == 0 || !sel[0].Current.BoundingRectangle.Contains(point.X, point.Y)) { var topLeft = element.Current.BoundingRectangle.TopLeft; itemParams.Point = new Point(point.X - topLeft.X, point.Y - topLeft.Y); return; } itemParams.SubItem = sel[0].Current.Name; //treeview //var sub = selectionPattern.Current.GetSelection()[0]; if (element.Current.ControlType == ControlType.Tree) { //AutomationPattern automationPatternFromElement = play.GetSpecifiedPattern(sel[0], "ExpandCollapsePatternIdentifiers.Pattern"); //ExpandCollapsePattern expandCollapsePattern = sel[0].GetCurrentPattern(automationPatternFromElement) as ExpandCollapsePattern; //if (expandCollapsePattern.Current.ExpandCollapseState != ExpandCollapseState.LeafNode) // itemParams.Expand = expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Expanded; } }
public static bool TryGetSelectionPattern(this AutomationElement element, out SelectionPattern result) { if (element.TryGetCurrentPattern(System.Windows.Automation.SelectionPattern.Pattern, out var pattern)) { result = (SelectionPattern)pattern; return(true); } result = null; return(false); }
/// <summary> /// Handle ElementSelectedEvent on items in the combo box. /// </summary> /// <param name="src">Object that raised the event.</param> /// <param name="e">Event arguments.</param> private void OnListItemSelect(object src, AutomationEventArgs e) { // Update the cache. AutomationElement updatedElement = elementCombo.GetUpdatedCache(comboCacheRequest); // Retrieve the pattern and the selected item from the cache. This code is here only to // demonstrate that the current selection can now be retrieved from the cache. In an application, // this would be done only when the information was needed. SelectionPattern pattern = updatedElement.GetCachedPattern(SelectionPattern.Pattern) as SelectionPattern; AutomationElement[] selectedItems = pattern.Cached.GetSelection(); selectedItem = selectedItems[0]; }
/// <summary> /// /// </summary> /// <returns></returns> public string GetSelectionName() { try { SelectionPattern t = (SelectionPattern)elePara.GetMe().GetCurrentPattern(SelectionPattern.Pattern); return(t.Current.GetSelection()[0].Current.Name); } catch (Exception ex) { throw new Exception(string.Format("Failed to GetSelection. {0}", ex)); } }
// </Snippet104> // <Snippet105> ///-------------------------------------------------------------------- /// <summary> /// Attempts to remove the current item from a collection /// of selected items. /// </summary> /// <param name="selectionItem"> /// An automation element that supports SelectionItemPattern. /// </param> ///-------------------------------------------------------------------- private void RemoveItemFromSelection(AutomationElement selectionItem) { if (selectionItem == null) { throw new ArgumentNullException( "Argument cannot be null or empty."); } AutomationElement selectionContainer = GetSelectionItemContainer(selectionItem); // Selection container cannot be null if (selectionContainer == null) { throw new ElementNotAvailableException(); } SelectionPattern selectionPattern = selectionContainer.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern; if (selectionPattern == null) { return; } // Check if a selection is required if (selectionPattern.Current.IsSelectionRequired && (selectionPattern.Current.GetSelection().GetLength(0) <= 1)) { return; } SelectionItemPattern selectionItemPattern = selectionItem.GetCurrentPattern( SelectionItemPattern.Pattern) as SelectionItemPattern; if ((selectionItemPattern != null) && selectionItemPattern.Current.IsSelected) { try { selectionItemPattern.RemoveFromSelection(); } catch (InvalidOperationException) { // Unable to remove from selection return; } } }
internal AutomationWrapper GetSelectedChild() { SelectionPattern sp = e.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern; if (sp != null) { foreach (AutomationElement selected in sp.Current.GetSelection()) { return(new AutomationWrapper(selected)); } } throw new Exception("No child is selected"); }
// </Snippet103> // <Snippet104> ///-------------------------------------------------------------------- /// <summary> /// Gets the current property values from target. /// </summary> /// <param name="selectionPattern"> /// A SelectionPattern control pattern obtained from /// an automation element representing the selection control. /// </param> /// <param name="automationProperty"> /// The automation property of interest. /// </param> ///-------------------------------------------------------------------- private bool GetSelectionObjectProperty( SelectionPattern selectionPattern, AutomationProperty automationProperty) { if (automationProperty.Id == SelectionPattern.CanSelectMultipleProperty.Id) { return(selectionPattern.Current.CanSelectMultiple); } if (automationProperty.Id == SelectionPattern.IsSelectionRequiredProperty.Id) { return(selectionPattern.Current.IsSelectionRequired); } return(false); }
// </Snippet102> // <Snippet1025> ///-------------------------------------------------------------------- /// <summary> /// Gets the currently selected SelectionItem objects from target. /// </summary> /// <param name="ae">The current Selection container object.</param> ///-------------------------------------------------------------------- private AutomationElement[] GetCurrentSelection( AutomationElement selectionContainer) { try { SelectionPattern selectionPattern = selectionContainer.GetCurrentPattern( SelectionPattern.Pattern) as SelectionPattern; return(selectionPattern.Current.GetSelection()); } // Container is not enabled catch (InvalidOperationException) { return(null); } }
///-------------------------------------------------------------------- /// <summary> /// Gets the currently selected SelectionItem objects from target. /// </summary> /// <param name="selectionContainer"> /// The target Selection container. /// </param> ///-------------------------------------------------------------------- internal AutomationElement[] GetTargetCurrentSelection( AutomationElement selectionContainer) { try { SelectionPattern selectionPattern = selectionContainer.GetCurrentPattern( SelectionPattern.Pattern) as SelectionPattern; return(selectionPattern.Current.GetSelection()); } catch (InvalidOperationException exc) { Feedback(exc.Message); return(null); } }
// </Snippet1035> // <Snippet104> ///-------------------------------------------------------------------- /// <summary> /// Attempts to add the current item to a collection /// of selected items. /// </summary> /// <param name="selectionItem"> /// An automation element that supports SelectionItemPattern. /// </param> ///-------------------------------------------------------------------- private void AddItemToSelection(AutomationElement selectionItem) { if (selectionItem == null) { throw new ArgumentNullException( "Argument cannot be null or empty."); } AutomationElement selectionContainer = GetSelectionItemContainer(selectionItem); // Selection container cannot be null if (selectionContainer == null) { throw new ElementNotAvailableException(); } SelectionPattern selectionPattern = selectionContainer.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern; if (selectionPattern == null) { return; } if (selectionPattern.Current.CanSelectMultiple) { SelectionItemPattern selectionItemPattern = selectionItem.GetCurrentPattern( SelectionItemPattern.Pattern) as SelectionItemPattern; if (selectionItemPattern != null) { try { selectionItemPattern.AddToSelection(); } catch (InvalidOperationException) { // Unable to add to selection return; } } } }
// </Snippet100> // <Snippet101> ///-------------------------------------------------------------------- /// <summary> /// Obtains a SelectionPattern control pattern from an /// automation element. /// </summary> /// <param name="targetControl"> /// The automation element of interest. /// </param> /// <returns> /// A SelectionPattern object. /// </returns> ///-------------------------------------------------------------------- private SelectionPattern GetSelectionPattern( AutomationElement targetControl) { SelectionPattern selectionPattern = null; try { selectionPattern = targetControl.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern; } // Object doesn't support the SelectionPattern control pattern catch (InvalidOperationException) { return(null); } return(selectionPattern); }
/// <summary> /// 获取下拉列表选择项 /// </summary> /// <param name="element">下拉列表元素</param> /// <returns>列表项元素</returns> public static AutomationElement CF_GetSelectListItem(AutomationElement element) { AutomationElement automationElement = null; if (element != null && element.Current.ControlType == ControlType.ComboBox) { try { if (element.TryGetCurrentPattern(SelectionPattern.Pattern, out object pattern)) { SelectionPattern selectionPattern = pattern as SelectionPattern; automationElement = selectionPattern.Current.GetSelection()[0]; } } catch { } } return(automationElement); }
public void SelectionTest() { SelectionPattern pattern = (SelectionPattern)treeView1Element.GetCurrentPattern(SelectionPatternIdentifiers.Pattern); Assert.IsNotNull(pattern, "selectionPattern should not be null"); SelectionPattern.SelectionPatternInformation current = pattern.Current; AutomationElement [] selection = current.GetSelection(); Assert.AreEqual(0, selection.Length, "Selection length"); SelectionItemPattern selectionItemPattern = (SelectionItemPattern)child1Element.GetCurrentPattern(SelectionItemPatternIdentifiers.Pattern); selectionItemPattern.Select(); if (Atspi) { Thread.Sleep(500); } selection = current.GetSelection(); Assert.AreEqual(1, selection.Length, "Selection length"); Assert.AreEqual(child1Element, selection [0], "Selection should contain child1Element"); if (Atspi) { Assert.IsFalse(current.IsSelectionRequired, "IsSelectionRequired"); } else { Assert.IsTrue(current.IsSelectionRequired, "IsSelectionRequired"); try { selectionItemPattern.RemoveFromSelection(); Assert.Fail("expected InvalidOperationException"); } catch (InvalidOperationException) { // expected } } selectionItemPattern = (SelectionItemPattern)child2Element.GetCurrentPattern(SelectionItemPatternIdentifiers.Pattern); selectionItemPattern.Select(); selection = current.GetSelection(); Assert.AreEqual(1, selection.Length, "Selection length"); Assert.AreEqual(child2Element, selection [0], "Selection should contain childElement"); }
private bool _checkvalue(string value) { try { SelectionPattern s = (SelectionPattern)this._condition.AutomationElement.GetCurrentPattern(SelectionPattern.Pattern); if (s.Current.GetSelection()[0].Current.Name == value) { return(true); } } catch { } if (this.Text == value) { return(true); } if (this.Value == value) { return(true); } return(false); }
// </Snippet108> // *** Simple property retrieval *** void MiscPropertyCalls(AutomationElement elementList) { // <Snippet121> // elementList is an AutomationElement. // The following two calls are equivalent. string name = elementList.Current.Name; name = elementList.GetCurrentPropertyValue(AutomationElement.NameProperty) as string; // </Snippet121> // <Snippet122> // elementList is an AutomationElement representing a list box. // Error-checking is omitted. Assume that elementList is known to support SelectionPattern. SelectionPattern selectPattern = elementList.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern; bool isMultipleSelect = selectPattern.Current.CanSelectMultiple; // The following call is equivalent to the one above. isMultipleSelect = (bool) elementList.GetCurrentPropertyValue(SelectionPattern.CanSelectMultipleProperty); // </Snippet122> // <Snippet123> // elementList is an AutomationElement. object help = elementList.GetCurrentPropertyValue(AutomationElement.HelpTextProperty, true); if (help == AutomationElement.NotSupported) { help = "No help available"; } string helpText = (string)help; // </Snippet123> // <Snippet126> // elementList is an AutomationElement. string helpString = elementList.GetCurrentPropertyValue(AutomationElement.HelpTextProperty) as string; // </Snippet126> }
/// <summary> /// Determines whether a selection is required for the specified control. /// </summary> /// <param name="control">The UI Automation element</param> /// <returns> /// <c>true</c> if selection is required; otherwise, <c>false</c>. /// </returns> internal static bool IsSelectionRequired(AutomationElement control) { SelectionPattern pattern = (SelectionPattern)CommonUIAPatternHelpers.CheckPatternSupport(SelectionPattern.Pattern, control); return(pattern.Current.IsSelectionRequired); }
public SelectionPatternPropertyObject(SelectionPattern pattern) { this._pattern = pattern; }
private void MouseClickThread(MouseEventArgs e, AutomationElement element, dynamic itemParams, AutomationElement topElement, System.Drawing.Point point, bool DoubleClick) { var operation = "MouseClick"; if (e.Button == MouseButtons.Right) { operation = "MouseRightClick"; } if (DoubleClick) { operation = "MouseDoubleClick"; } try { if (element.Current.ControlType.In(ControlType.Pane)) //if (element.Current.FrameworkId == "WinForm" && element.Current.ControlType.In(ControlType.Pane)) { element = AutomationElement.FromHandle((IntPtr)element.Current.NativeWindowHandle); } if (!((ExpandoObject)itemParams).Exsits("Control") && !((ExpandoObject)itemParams).Exsits("Name")) { if (string.IsNullOrEmpty(element.Current.AutomationId)) { itemParams.Control = element.Current.Name; } else { itemParams.Control = element.Current.AutomationId; } } var ctlType = element.Current.ControlType.ProgrammaticName.ToString(); itemParams.ControlType = ctlType.Substring("ControlType.".Length); if (topElement.Current.ClassName != "#32770") { int iaid; if (int.TryParse(element.Current.AutomationId, out iaid)) { //for ComboBox TextArea if (iaid > 0) { element = AutomationElement.FromPoint(element.Current.BoundingRectangle.BottomRight); } //itemParams.Control = element.Current.AutomationId; if (element.Current.AutomationId == iaid.ToString()) { itemParams.Framework = element.Current.FrameworkId; } } } if (element.Current.FrameworkId.In("Win32", "DirectUI")) { itemParams.W32ClassName = topElement.Current.ClassName; if (element.Current.ClassName == "TaskListThumbnailWnd") { //! Stopwatch sw = new Stopwatch(); sw.Start(); IntPtr wnd = IntPtr.Zero; do { Thread.Sleep(50); //var p = AutomationElement.FocusedElement.Current.BoundingRectangle.TopLeft; wnd = WinFormsUtilities.NativeMethods.GetForegroundWindow(); //WinFormsUtilities.NativeMethods.GetWindowThreadProcessId(wnd,out prc); //wnd = WinFormsUtilities.NativeMethods.WindowFromPoint((int)p.X, (int)p.Y); } while (wnd == IntPtr.Zero && wnd != (IntPtr)element.Current.NativeWindowHandle && sw.ElapsedMilliseconds < 3000); var target = AutomationElement.FromHandle(wnd); //var p = AutomationElement.FocusedElement.Current.BoundingRectangle.TopLeft; //target = AutomationElement.FromPoint(p); var proc = Process.GetProcessById(target.Current.ProcessId); if (!string.IsNullOrEmpty(target.Current.AutomationId)) { itemParams.Window = target.Current.AutomationId; } if (!string.IsNullOrEmpty(proc.MainWindowTitle)) { itemParams.WindowTitle = proc.MainWindowTitle; } else { itemParams.WindowTitle = target.Current.Name; } itemParams.ProcessName = proc.ProcessName; ((IDictionary <string, Object>)itemParams).Remove("Name"); ((IDictionary <string, Object>)itemParams).Remove("Control"); //itemParams.Name = null; //itemParams.Control = null; } if (itemParams.W32ClassName == "ComboLBox" && element.Current.ControlType.In(ControlType.List)) { //for combobox ▼ string id = element.Current.AutomationId; Stopwatch sw = new Stopwatch(); sw.Start(); while (!string.IsNullOrEmpty(id) && element.Current.AutomationId == id && sw.ElapsedMilliseconds < 3000) { Thread.Sleep(200); AutomationElement parent = AutomationElement.FocusedElement; if (parent.Current.ControlType != ControlType.ComboBox) { parent = AutomationElement.FromPoint(parent.Current.BoundingRectangle.TopRight); } itemParams.Control = parent.Current.AutomationId; IntPtr wnd = WinFormsUtilities.NativeMethods.WindowFromPoint(point.X, point.Y); //wnd = (IntPtr)parent.Current.NativeWindowHandle; topElement = AutomationElement.FromHandle(WinFormsUtilities.GetTopWindow(wnd)); if (!string.IsNullOrEmpty(topElement.Current.AutomationId)) { itemParams.Window = topElement.Current.AutomationId; } SelectionPattern selectionPattern = parent.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern; var sel = selectionPattern.Current.GetSelection(); if (sel.Length > 0) { itemParams.SubItem = selectionPattern.Current.GetSelection()[0].Current.Name; break; } } return; } else if (element.Current.ControlType.In(ControlType.List, ControlType.Tree)) { SetSelectedItem(itemParams, element, point); itemParams.ClassName = element.Current.ClassName; return; } else { itemParams.ClassName = element.Current.ClassName; //var topLeft = element.Current.BoundingRectangle.TopLeft; //itemParams.Point = new Point(point.X - topLeft.X, point.Y - topLeft.Y); } } if (element.Current.ControlType.In(ControlType.Edit, ControlType.Document)) { var tp = element.GetCurrentPattern(TextPattern.Pattern) as TextPattern; if (tp.SupportedTextSelection != SupportedTextSelection.None) { var trs = tp.GetSelection(); var r = trs.First().GetBoundingRectangles(); if (r.Length == 0) { SendKeys.SendWait("^+{HOME}"); trs = tp.GetSelection(); var selected = trs.First().GetText(-1).Replace("\n", ""); itemParams.Pos = selected.Length; if (selected.Contains("\r")) { SendKeys.SendWait("{LEFT}"); } SendKeys.SendWait("{RIGHT}".Repeat(selected.Length)); } else { //itemParams.Rect = r.First(); var selected = trs.First().GetText(-1).Replace("\n", ""); itemParams.Selected = selected; //itemParams.Pos = element.Current.Name.IndexOf(selected); SendKeys.SendWait("^+{HOME}"); trs = tp.GetSelection(); var newSelected = trs.First().GetText(-1).Replace("\n", ""); itemParams.Pos = newSelected.Length; if (newSelected.Contains("\r")) { SendKeys.SendWait("{Left}"); } if (newSelected.EndsWith(selected)) { if (newSelected != selected) { SendKeys.SendWait("{RIGHT}".Repeat(newSelected.Length - selected.Length)); } else { return; } } else { SendKeys.SendWait("{RIGHT}".Repeat(newSelected.Length)); } SendKeys.SendWait("+{RIGHT}".Repeat(selected.Length)); } } } else if (element.Current.ControlType.In(ControlType.Tree, ControlType.DataGrid, ControlType.List, ControlType.Tab)) { //for list SetSelectedItem(itemParams, element, point); } else if (element.Current.ControlType == ControlType.Table) { //DataGridView var cells = element.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom)); if (cells.Count > 0) { foreach (AutomationElement cellEle in cells) { var rect = cellEle.Current.BoundingRectangle; if (rect.Contains(e.X, e.Y) && cellEle.Current.AutomationId != element.Current.AutomationId && cellEle.Current.IsEnabled) { string pos = cellEle.Current.Name; var sp = pos.Split(' '); itemParams.Row = sp.Last(); if (sp.Length < 3) { continue; } itemParams.Column = sp[0]; break; } } } } else if (element.Current.ControlType == ControlType.Menu) { //menu items if (menuObj != null) { itemParams.Control = menuObj.MenuStripId; itemParams.Window = menuObj.Window; itemParams.Menu = menuTop; itemParams.MenuItem = focused; menuObj = null; } else { itemParams.ContextMenuItem = focused; } } else if (element.Current.ControlType.In(ControlType.ToolBar, ControlType.MenuBar)) { AutomationElementCollection subelements; AutomationElement subelement; if (element.Current.ControlType.In(ControlType.MenuBar)) { var selMenu = topElement.FindFirst(TreeScope.Children, System.Windows.Automation.Condition.TrueCondition); if (selMenu != null && selMenu.Current.ControlType == ControlType.Menu) { menuObj = new ExpandoObject(); menuObj.Window = itemParams.Window; menuObj.MenuStripId = element.Current.AutomationId; itemParams.Menu = selMenu.Current.Name; return; } //menubar root item subelements = topElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuBar)); subelement = subelements.Cast <AutomationElement>().FirstOrDefault(x => x.Current.Name == element.Current.Name); } else { //toolstrip subelements = topElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar)); subelement = subelements.Cast <AutomationElement>().FirstOrDefault(x => x.Current.Name == element.Current.Name); } if (subelement != null) { foreach (AutomationElement it in subelement.FindInRawView()) { var rect = it.Current.BoundingRectangle; if (!string.IsNullOrEmpty(it.Current.AutomationId)) { if (it.Current.AutomationId == element.Current.AutomationId) { continue; } } else if (!string.IsNullOrEmpty(it.Current.Name) && it.Current.Name == element.Current.Name) { continue; } if (rect.Contains(e.X, e.Y) && it.Current.IsEnabled) { itemParams.SubItem = it.Current.Name; break; } } } } else { Point pt; if (element.Current.ControlType.In(ControlType.Window, ControlType.Pane, ControlType.Group, ControlType.Image, ControlType.Custom, ControlType.Document, ControlType.ScrollBar, ControlType.Slider, ControlType.Spinner, ControlType.Calendar, ControlType.DataGrid, ControlType.StatusBar) || e.Button == MouseButtons.Right) { var topLeft = element.Current.BoundingRectangle.TopLeft; itemParams.Point = new Point(point.X - topLeft.X, point.Y - topLeft.Y); } } } //catch (ElementNotAvailableException) //{ // System.Diagnostics.Debug.WriteLine("ElementNotAvailableException caused"); //} catch (Exception ex) { Debug.WriteLine(ex.StackTrace); } finally { if (itemParams != null) { addItem(operation, itemParams); } keybuf = string.Empty; //mainWindow.Dispatcher.Invoke((Action)(() => //{ // mainWindow.ScrollBottomGrid(); //})); ResetEvent.Set(); } }
///-------------------------------------------------------------------- /// <summary> /// Handles user input in the client. /// </summary> /// <param name="sender">The object that raised the event.</param> /// <param name="e">Event arguments.</param> ///-------------------------------------------------------------------- internal void ClientSelectionHandler( object sender, SelectionChangedEventArgs e) { feedbackText = new StringBuilder(); ListBox clientListBox = sender as ListBox; foreach ( AutomationElement targetControl in targetHandler.TargetControls) { if (clientListBox.Name == targetControl.Current.AutomationId.ToString()) { SelectionPattern selectionPattern = targetControl.GetCurrentPattern( SelectionPattern.Pattern) as SelectionPattern; AutomationElementCollection targetSelectionItems = targetHandler.GetSelectionItemsFromTarget(targetControl); if (e.AddedItems.Count > 0) { for (int itemCounter = 0; itemCounter < clientListBox.Items.Count; itemCounter++) { ListBoxItem listboxItem = GetClientItemFromIndex( clientListBox, itemCounter); SelectionItemPattern selectionItemPattern = targetSelectionItems[itemCounter].GetCurrentPattern( SelectionItemPattern.Pattern) as SelectionItemPattern; if (!(selectionItemPattern.Current.IsSelected) && listboxItem.IsSelected) { if (selectionPattern.Current.GetSelection().Length < 1) { try { selectionItemPattern.Select(); feedbackText.Append( targetSelectionItems[itemCounter].Current.Name) .Append(" of the ") .Append( targetControl.Current.AutomationId) .Append(" control was selected."); Feedback(feedbackText.ToString()); } catch (InvalidOperationException exc) { Feedback(exc.Message); } } else { try { if (selectionPattern.Current.CanSelectMultiple) { selectionItemPattern.AddToSelection(); feedbackText.Append( targetSelectionItems[itemCounter].Current.Name) .Append(" of the ") .Append( targetControl.Current.AutomationId) .Append( " control was added to the selection."); Feedback(feedbackText.ToString()); } else { selectionItemPattern.Select(); feedbackText.Append( targetSelectionItems[itemCounter].Current.Name) .Append(" of the ") .Append( targetControl.Current.AutomationId) .Append(" control was selected."); Feedback(feedbackText.ToString()); } } catch (InvalidOperationException exc) { Feedback(exc.Message); } } break; } } } else if (e.RemovedItems.Count > 0) { for (int itemCounter = 0; itemCounter < clientListBox.Items.Count; itemCounter++) { ListBoxItem listboxItem = GetClientItemFromIndex(clientListBox, itemCounter); SelectionItemPattern selectionItemPattern = targetSelectionItems[itemCounter].GetCurrentPattern( SelectionItemPattern.Pattern) as SelectionItemPattern; if ((selectionItemPattern.Current.IsSelected) && !listboxItem.IsSelected) { try { selectionItemPattern.RemoveFromSelection(); feedbackText.Append( targetSelectionItems[itemCounter].Current.Name) .Append(" of the ") .Append( targetControl.Current.AutomationId) .Append( " control was removed from the selection."); Feedback(feedbackText.ToString()); break; } catch (InvalidOperationException exc) { Feedback(exc.Message); } } } } } for (int controlCounter = 0; controlCounter < targetHandler.TargetControls.Count; controlCounter++) { SetControlPropertiesText(controlCounter); EchoTargetControlProperties( targetHandler.TargetControls[controlCounter], controlCounter); } } }
/// <summary> /// Determines whether this instance can select multiple items /// </summary> /// <param name="control">The UI Automation element</param> /// <returns> /// <c>true</c> if this instance can select multiple items; otherwise, <c>false</c>. /// </returns> internal static bool CanSelectMultiple(AutomationElement control) { SelectionPattern pattern = (SelectionPattern)CommonUIAPatternHelpers.CheckPatternSupport(SelectionPattern.Pattern, control); return(pattern.Current.CanSelectMultiple); }
public AutomationElement [] GetSelection() { SelectionPattern sp = (SelectionPattern)element.GetCurrentPattern(SelectionPattern.Pattern); return((AutomationElement [])sp.Current.GetSelection()); }
/// <summary> /// Gets the selected items. /// </summary> /// <param name="control">The UI Automation element</param> /// <returns> /// An AutomationElement array of all selected items /// </returns> internal static AutomationElement[] SelectedItems(AutomationElement control) { SelectionPattern pattern = (SelectionPattern)CommonUIAPatternHelpers.CheckPatternSupport(SelectionPattern.Pattern, control); return(pattern.Current.GetSelection()); }
public void Execute(string action, string parameter) { switch (action.ToLower()) { case "invoke": { InvokePattern pattern = _Win.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern; pattern.Invoke(); } break; case "toggle": { TogglePattern pattern = _Win.GetCurrentPattern(TogglePattern.Pattern) as TogglePattern; pattern.Toggle(); } break; case "expand": { ExpandCollapsePattern pattern = _Win.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern; pattern.Expand(); } break; case "collapse": { ExpandCollapsePattern pattern = _Win.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern; pattern.Collapse(); } break; case "selection": { SelectionPattern pattern = _Win.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern; //pattern.(); } break; case "pageup": { ScrollPattern pattern = _Win.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern; pattern.ScrollVertical(ScrollAmount.LargeIncrement); } break; case "pagedown": { ScrollPattern pattern = _Win.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern; pattern.ScrollVertical(ScrollAmount.LargeDecrement); } break; case "pageleft": { ScrollPattern pattern = _Win.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern; pattern.ScrollHorizontal(ScrollAmount.LargeDecrement); } break; case "pageright": { ScrollPattern pattern = _Win.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern; pattern.ScrollHorizontal(ScrollAmount.LargeIncrement); } break; case "scrollup": { ScrollPattern pattern = _Win.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern; pattern.ScrollVertical(ScrollAmount.SmallIncrement); } break; case "scrolldown": { ScrollPattern pattern = _Win.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern; pattern.ScrollVertical(ScrollAmount.SmallDecrement); } break; case "scrollleft": { ScrollPattern pattern = _Win.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern; pattern.ScrollHorizontal(ScrollAmount.SmallDecrement); } break; case "scrollright": { ScrollPattern pattern = _Win.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern; pattern.ScrollHorizontal(ScrollAmount.SmallIncrement); } break; case "value": { ValuePattern pattern = _Win.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern; pattern.SetValue(parameter); } break; case "text": { //TextPattern pattern = _Win.GetCurrentPattern(TextPattern.Pattern) as TextPattern; //pattern.(parameter); } break; } }