【问题标题】:UI Automation "Selected text"UI 自动化“选定文本”
【发布时间】:2010-10-05 18:50:47
【问题描述】:

有人知道如何使用 UI 自动化和 .Net 从其他应用程序中获取选定的文本吗?

http://msdn.microsoft.com/en-us/library/ms745158.aspx

【问题讨论】:

    标签: c# winapi user-interface automation


    【解决方案1】:

    这是另一种仅使用 UI 自动化的解决方案。 它从记事本和写字板中获取选定的文本。

    // Get only top level windows
    PropertyCondition condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window);
    AutomationElementCollection windows = AutomationElement.RootElement.FindAll(TreeScope.Children, condition);
    List<AutomationElement> allDocuments = new List<AutomationElement>();
    
    foreach (AutomationElement window in windows)
    {
        string className = window.Current.ClassName;
        if (className == "Notepad" || className == "WordPadClass")
        {
            condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document);
            AutomationElementCollection documents = window.FindAll(TreeScope.Children, condition);
    
            if (documents.Count > 0)
            {
                allDocuments.Add(documents[0]);
            }
        }
    }
    
    // store all pieces of selected text here
    List<string> selectedText = new List<string>();
    
    // iterate through all documents found
    foreach (AutomationElement document in allDocuments)
    {
        object patternObj = null;
        if (document.TryGetCurrentPattern(TextPattern.Pattern, out patternObj) == true)
        {
            TextPattern textPattern = patternObj as TextPattern;
            TextPatternRange[] ranges = textPattern.GetSelection();
    
            foreach (TextPatternRange range in ranges)
            {
                string text = range.GetText(-1);
                if (text.Length > 0)
                {
                    selectedText.Add(text);
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:
      private void button1_Click(object sender, EventArgs e) {
              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;
                      }
                      else {
                          trs = tp.GetSelection();
                          lblSelectedText.Text = trs[0].GetText(-1);
                      }
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2020-07-16
        • 1970-01-01
        • 2019-09-06
        • 1970-01-01
        • 2015-12-05
        • 2018-11-17
        • 2012-02-21
        • 2015-05-04
        • 2011-03-10
        相关资源
        最近更新 更多