如何从任何窗口中获取选定的文本(使用 UI 自动化) - C# [英] How to get selected text from ANY window (using UI Automation) - C#

查看:34
本文介绍了如何从任何窗口中获取选定的文本(使用 UI 自动化) - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小托盘应用程序,它注册了一个系统范围的热键.当用户在任何应用程序中的任何位置选择文本并按下此热键时,我希望能够捕获所选文本.我目前正在使用 AutomationElements 执行此操作:

I have a small tray application which registers a system-wide hotkey. When the user selects a text anywhere in any application and presses this hotkey I want to be able to capture the selected text. I'm currently doing this using AutomationElements:

//Using FocusedElement (since the focused element should be the control with the selected text?)
AutomationElement ae = AutomationElement.FocusedElement;        
AutomationElement txtElement = ae.FindFirst(TreeScope.Subtree,Condition.TrueCondition);
if(txtElement == null)
    return;

TextPattern tp;

try
{
    tp = txtElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
}
catch(Exception ex)
{
    return;
}

TextPatternRange[] trs;

if (tp.SupportedTextSelection == SupportedTextSelection.None)
{
    return;
            }
else
{
    trs = tp.GetSelection();
    string selectedText = trs[0].GetText(-1);
    MessageBox.Show(selectedText );

}

这适用于某些应用(例如记事本、visual studios 编辑框等),但不适用于所有应用(例如 Word、FireFox、Chrome 等.)

This works for some apps (such as notepad, visual studios edit boxes and such) but not for all (such as Word, FireFox, Chrome, and so on.)

这里有人对如何在任何应用程序中检索所选文本有任何想法吗?

Anyone here with any ideas of how to be able to retreive the selected text in ANY application?

推荐答案

遗憾的是,无法从任意应用程序中获取所选文本.UI 自动化如果应用程序支持 UIA TextPattern;不幸的是,大多数没有.我写了一个尝试这样做的应用程序,但有很多后备.

Unfortunately, there's no way to get the selected text from any arbitrary application. UI Automation works if the application supports UIA TextPattern; unfortunately, most do not. I wrote an application that tried to do this, and had a bunch of fallbacks.

我尝试过(大致按顺序):

I tried (pretty much in order):

  1. UIA.TextPattern
  2. 特定于 Internet Explorer 的(IE 6、7、8、9 有不同的实现)
  3. Adobe Reader 专用
  4. 剪贴板

这涵盖了 80-90% 的应用程序,但仍有不少应用程序失败.

This covered 80-90% of the applications out there, but there were quite a few that still failed.

请注意,恢复剪贴板有其自身的问题;某些应用程序(Office 等)将特定于供应商的信息放入剪贴板,这些信息可以指向内部数据;当您将自己的信息放在剪贴板上时,内部数据会被释放,而当您放回旧数据时,剪贴板现在指向已释放的数据,从而导致崩溃.您可以通过仅保存/恢复已知的剪贴板格式来解决这个问题,但同样,这会导致奇怪的行为,因为应用程序行为错误"而不是崩溃.

Note that restoring the clipboard has problems of its own; some applications (Office, etc.) put vendor-specific information into the clipboard that can have pointers into internal data; when you put your own info on the clipboard, the internal data gets released, and when you put the old data back, the clipboard now points to freed data, resulting in crashes. You could work around this somewhat by only saving/restoring known clipboard formats, but again, that results in odd behavior in that apps behave "wrong" instead of crashing.

这篇关于如何从任何窗口中获取选定的文本(使用 UI 自动化) - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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