【问题标题】:Using c# to get selected text from other applications使用 c# 从其他应用程序中获取选定的文本
【发布时间】:2016-12-26 15:07:03
【问题描述】:

我正在处理一个需要在用户选择某些文本时从其他应用程序获取选定文本的项目。

我使用 globalmousekeyhook 来监控用户何时单击应用程序,我对此没有任何问题。

我的问题是获取选定的文本,据我所知有两种方法可以从其他应用程序中获取选定的文本:

1-发送复制命令(like this):
这种方式适用于剪贴板,并导致一些应用程序出现问题,如 Adob​​e reader (problem)、IDM (clipboard links) 和其他也适用于剪贴板的应用程序。

2-使用 UI 自动化:
正如我已经尝试并看到这种方式不能用于 get selected text from ANY window (using UI Automation)

我还编辑了 this 代码,如下所示,但它仍然无法正常工作并减慢我的计算机速度

Process p = Process.GetCurrentProcess();

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();
      label1.Text = trs[0].GetText(-1);
}


难道没有其他方法可以从其他应用程序中获取选定的文本吗?
或者不能使用第二种方式来执行工作?

感谢您的帮助...

【问题讨论】:

    标签: c# .net automation clipboard ui-automation


    【解决方案1】:

    一种方法可能是使用Spy++。获取所需的句柄、提取和解析数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-11
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 2020-11-11
      相关资源
      最近更新 更多