简体   繁体   中英

Using c# to get selected text from other applications

Iam working on a project that need to get selected text from other application anytime user selects some text.

I have used globalmousekeyhook to monitor when user clicks on an application and i have not problem with that.

My problem is with getting selected text, As i know there are two ways to get selected text from other Applications:

1-Sending copy command( like this ):
This way works on clipboard and causes some problems with some applications like Adobe reader ( problem ),IDM ( clipboard links ) And other applications that also work on clipboard.

2-Using UI Automation:
As i have tried and seen that this way cant be used for get selected text from ANY window (using UI Automation)

I also edited this code like bellow but it still not working and slows down my computer

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


Isn't there another way to get selected text from other applications?
Or second way can't be used to perform the work?

Thanks for any help...

An approach might be the use of Spy++ . Get the handle, extract and parse data you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM