UI自动化"选择了文字和QUOT; [英] UI Automation "Selected text"

查看:165
本文介绍了UI自动化"选择了文字和QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何开始使用UI自动化和.Net?

选择从其他应用程序文本

<一个href=\"http://msdn.microsoft.com/en-us/library/ms745158.aspx\">http://msdn.microsoft.com/en-us/library/ms745158.aspx


解决方案

 私人无效的button1_Click(对象发件人,EventArgs的发送){
        流程[] =的plist Process.GetProcesses();        的foreach(进程p中的plist){
            如果(p.ProcessName ==记事本){                AutomationElement AE = AutomationElement.FromHandle(p.MainWindowHandle);                AutomationElement npEdit = ae.FindFirst(TreeScope.Descendants,新PropertyCondition(AutomationElement.ClassNameProperty,编辑));                textpattern的TP = npEdit.GetCurrentPattern(TextPattern.Pattern)作为Textpattern的;                TextPatternRange [] TRS;                如果(tp.SupportedTextSelection == SupportedTextSelection.None){
                    返回;
                }
                其他{
                    TRS = tp.GetSelection();
                    lblSelectedText.Text = TRS [0] .GetText(-1);
                }
            }
        }
    }

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

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

解决方案

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);
                }
            }
        }
    }

这篇关于UI自动化&QUOT;选择了文字和QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆