private string getSelectedText2() { System.Drawing.Point mouse = System.Windows.Forms.Cursor.Position; // use Windows forms mouse code instead of WPF AutomationElement element = AutomationElement.FromPoint(new System.Windows.Point(mouse.X, mouse.Y)); if (element == null) { // no element under mouse return(null); } Console.WriteLine("Element at position " + mouse + " is '" + element.Current.Name + "'"); object pattern; // the "Value" pattern is supported by many application (including IE & FF) if (element.TryGetCurrentPattern(ValuePattern.Pattern, out pattern)) { ValuePattern valuePattern = (ValuePattern)pattern; //Console.WriteLine(" Value=" + valuePattern.Current.Value); return(valuePattern.Current.Value); } // the "Text" pattern is supported by some applications (including Notepad)and returns the current selection for example if (element.TryGetCurrentPattern(TextPattern.Pattern, out pattern)) { TextPattern textPattern = (TextPattern)pattern; foreach (TextPatternRange range in textPattern.GetSelection()) { //Console.WriteLine(" SelectionRange=" + range.GetText(-1)); return(range.GetText(-1)); } } return(null); }
private string GetTextViaHtml() { Process[] plist = Process.GetProcesses(); foreach (Process p in plist) { if (p.ProcessName == "notepad") { AutomationElement ae = AutomationElement.FromHandle(p.MainWindowHandle); AutomationElement npEdit = ae.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "Edit")); TextPattern tp = npEdit.GetCurrentPattern(TextPattern.Pattern) as TextPattern; TextPatternRange[] trs; if (tp.SupportedTextSelection == SupportedTextSelection.None) { return(null); } else { trs = tp.GetSelection(); String Text = trs[0].GetText(-1); return(Text); } } } return(null); }
/// <summary> /// Expands the text range to the specified TextUnit. /// </summary> /// <param name="control">The UI Automation element</param> /// <param name="unit">The textual unit.</param> internal static void ExpandToEnclosingUnit(AutomationElement control, TextUnit unit) { TextPattern pat = (TextPattern)CommonUIAPatternHelpers.CheckPatternSupport(TextPattern.Pattern, control); TextPatternRange[] selection = pat.GetSelection(); selection[0].ExpandToEnclosingUnit(unit); }
public TextPatternRange [] GetSelection(bool log) { if (log) { procedureLogger.Action(string.Format("Get selection from {0}.", this.NameAndType)); } TextPattern tp = (TextPattern)element.GetCurrentPattern(TextPattern.Pattern); return((TextPatternRange [])tp.GetSelection()); }
public void TextPatternTest() { System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); // Fragile -- I'm open to a better way of doing this. using (AppHost host = new AppHost("xpsrchvw.exe", "..\\..\\..\\UiaComWrapperTests\\bin\\debug\\test.xps")) { AutomationElement mainContent = host.Element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.IsTextPatternAvailableProperty, true)); TextPattern text = (TextPattern)mainContent.GetCurrentPattern(TextPattern.Pattern); Assert.AreEqual(text.SupportedTextSelection, SupportedTextSelection.Single); TextPatternRange range1 = text.DocumentRange; Assert.IsNotNull(range1); Assert.AreEqual(text, range1.TextPattern); TextPatternRange range2 = range1.Clone(); Assert.IsNotNull(range2); Assert.IsTrue(range1.Compare(range2)); Assert.IsTrue(0 == range1.CompareEndpoints(TextPatternRangeEndpoint.Start, range2, TextPatternRangeEndpoint.Start)); Assert.IsTrue(0 == range1.CompareEndpoints(TextPatternRangeEndpoint.End, range2, TextPatternRangeEndpoint.End)); string keyString = "Constitution of the United States"; TextPatternRange range3 = range1.FindText(keyString, false, true); Assert.IsNotNull(range3); string foundString = range3.GetText(-1); Assert.AreEqual(keyString, foundString); range3.Select(); TextPatternRange[] selectedRanges = text.GetSelection(); Assert.AreEqual(1, selectedRanges.Length); TextPatternRange selectedRange = selectedRanges[0]; Assert.IsTrue(range3.Compare(selectedRange)); // Test attributes. Casts will fail if types are wrong System.Globalization.CultureInfo culture = (System.Globalization.CultureInfo)range3.GetAttributeValue(TextPattern.CultureAttribute); string fontName = (string)range3.GetAttributeValue(TextPattern.FontNameAttribute); bool hiddenValue = (bool)range3.GetAttributeValue(TextPattern.IsItalicAttribute); Assert.AreEqual(AutomationElement.NotSupported, range3.GetAttributeValue(TextPattern.IsHiddenAttribute)); TextPatternRange range5 = range1.FindAttribute(TextPattern.IsItalicAttribute, true, false /* backward */); Assert.IsNotNull(range5); Assert.AreEqual("Note", range5.GetText(-1)); range5.ExpandToEnclosingUnit(TextUnit.Line); string line5 = range5.GetText(-1); Assert.AreEqual("Preamble Note ", line5); System.Windows.Rect[] rects = range3.GetBoundingRectangles(); Assert.AreEqual(rects.Length, 1); Assert.IsTrue(rects[0].Width > 0); Assert.IsTrue(rects[0].Height > 0); } }
public override string GetSelectedText() { AutomationElement edit = GetNotepadEdit(node); TextPattern pattern = edit.GetCurrentPattern(TextPattern.Pattern) as TextPattern; string ret = string.Empty; if (pattern != null) { TextPatternRange[] ranges = pattern.GetSelection(); foreach (TextPatternRange range in ranges) { ret += range.GetText(-1); } } return(ret); }
static void Main(string[] args) { do { System.Drawing.Point mouse = Cursor.Position; // use Windows forms mouse code instead of WPF var s = WordCapturer.GetCharFromPosition(mouse); Console.WriteLine(s); Thread.Sleep(1000); AutomationElement element = AutomationElement.FromPoint(new Point(mouse.X, mouse.Y)); if (element == null) { // no element under mouse return; } try { Console.WriteLine("Element at position " + mouse + " is '" + element.Current.Name + "'"); } catch { } object pattern = null; // the "Value" pattern is supported by many application (including IE & FF) if (element.TryGetCurrentPattern(ValuePattern.Pattern, out pattern)) { ValuePattern valuePattern = (ValuePattern)pattern; Console.WriteLine(" Value=" + valuePattern.Current.Value); } // the "Text" pattern is supported by some applications (including Notepad)and returns the current selection for example if (element.TryGetCurrentPattern(TextPattern.Pattern, out pattern)) { TextPattern textPattern = (TextPattern)pattern; foreach (var range in textPattern.GetSelection()) { Console.WriteLine(" SelectionRange=" + range.GetText(-1)); } } Thread.Sleep(1000); Console.WriteLine(); }while (true); }
private string ExtractText(AutomationElement element) { object pattern; if (element.TryGetCurrentPattern(ValuePattern.Pattern, out pattern)) { ValuePattern valuePattern = (ValuePattern)pattern; return(valuePattern.Current.Value); } if (element.TryGetCurrentPattern(TextPattern.Pattern, out pattern)) { TextPattern textPattern = (TextPattern)pattern; foreach (var range in textPattern.GetSelection()) { return(range.GetText(-1)); } } return(element.Current.Name); }
public string GetSelectedText() { AutomationElement ae = AutomationElement.FocusedElement; TextPattern tp = ae.GetCurrentPattern(TextPattern.Pattern) as TextPattern; if (tp == null) { return(""); } TextPatternRange[] trs; if (tp.SupportedTextSelection == SupportedTextSelection.None) { return(""); } else { trs = tp.GetSelection(); return(trs[0].GetText(-1)); } }
internal static TextPatternRange[] GetSelection(AutomationElement control) { TextPattern pat = (TextPattern)CommonUIAPatternHelpers.CheckPatternSupport(TextPattern.Pattern, control); return(pat.GetSelection()); }