8

I would like to know how can I get highlighted text from any window for example: (excel, ie, firefox,…). please note that the following message not work in the above application WM_GETTEXT,WM_COPY,EM_GETSELTEXT.

I have also tried control C (copy) and get selected text from clipboard but it is not a good idea.

Language used: C#

Gaby
  • 2,923
  • 4
  • 38
  • 52
  • 1
    Sending the copy command is indeed not very polite, I'd become quite nervous if a misbehaved app would tamper my clipboard in which I would've put things with a purpose. – Sorin Comanescu Feb 10 '10 at 11:48
  • @SorinComanescu What if the user knows that's the purpose? – Levi H Oct 26 '18 at 08:42

4 Answers4

2

I haven't tried it myself, but the Microsoft UI Automation API should have the functionality that you need.

The UI Automation API is what you would use if you were building a screen reader to assist blind people. So it should definitely be able to access the selected text in an arbitrary application.

A good place to start would be with the "Text Pattern Overview" at http://msdn.microsoft.com/en-us/library/ms745158.aspx

Also keep your eye on question 517694. I think you'll find that answers to that question will solve your problem.

Community
  • 1
  • 1
Antony Perkov
  • 931
  • 6
  • 13
1

No answers huh? Well, I know you can get it from Excel, Word etc using interop. Look into that. It might give you som ideas on how to proceed with ie and ff. But basically the recieving application must have some sort of fascility for letting you do this and I don't think there's any general way which works all the time.

Martin
  • 2,956
  • 7
  • 30
  • 59
1

There is no general purpose answer to this question. Each window class will have a different solution.

For instance, if the hilighted text is in an edit window, then you can use EM_GETSEL to get the range of the selection, then WM_GETTEXT to get the text (and then throw the unselected part a way) or EM_LINEFROMCHAR to turn that range into line indexes, and then EM_GETLINE to get the selected text one line at a time.

But this won't work for any other window class.

John Knoeller
  • 33,512
  • 4
  • 61
  • 92
-1

No need to write this in C# from scratch. What's wrong with using the clipboard? This script ensures that it restores what was on the clipboard when it has finished.

Autohotkey makes this much simpler.

; Hotkey:  Ctrl Shift t

^!t::

; Remember what was in the clipboard
clipboardPrev = %clipboard%

; Clear the clipboard
clipboard:=

Sleep,200

; Send a Ctrl C to copy the current selection
SendInput, {Ctrl down}c{Ctrl up}

Sleep,200

; Get the current selection from the clipboard
selectedText=%Clipboard%

if SelectedText =
{
    ; If the first attempt didn't get any test, try again
    Sleep,200

    ; Send a Ctrl C to copy the current selection
    SendInput, {Ctrl down}c{Ctrl up}

    ; Get the current selection from the clipboard
    selectedText=%Clipboard%

}

; Restore the clipboard
clipboard=%clipboardPrev% 

MsgBox, %selectedText%

return
Ash
  • 60,973
  • 31
  • 151
  • 169
  • I understand he's asking for it in C# but that would take a *lot* of work and testing. You could compile this to an executable and then just call from the C# application using the Process class. – Ash Jan 26 '10 at 02:25
  • hi, thanks for your reply currently in my application i am using the clipboard method, but i'm facing problems with Google Chrome. After sending control c to Google chrome, the selected text or the clipboard value is empty. I've seen a lot of programs like wordweb.. that can get the highlighted text without using the clipboard, how i can i do that? Sorry for my bad English. – Gaby Jan 26 '10 at 11:58
  • @Gaby, I think apps like Google chrome and say iTunes use custom drawn UI, not standard UI and selection/clipboard will often not work sadly. Not sure what WordWeb does but the Win API allows you to get text straight out of a window but not sure how selected text can be extracted, sorry. – Ash Jan 26 '10 at 12:06