UI自动化“选定的文本" [英] UI Automation "Selected text"
本文介绍了UI自动化“选定的文本"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道如何使用 UI 自动化和 .Net 从其他应用程序中获取选定的文本吗?
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自动化“选定的文本"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文