Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI Automation "Selected text"

Anyone knows how to get selected text from other application using UI Automation and .Net?

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

like image 716
Amr Elgarhy Avatar asked Feb 05 '09 20:02

Amr Elgarhy


1 Answers

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);
                }
            }
        }
    }
like image 148
NetMage Avatar answered Nov 15 '22 17:11

NetMage