问题
I know that you can get the text selection by sending a WM_COPY message and then reading the new contents of the clipboard.
But how does Windows retrieve the text selection in the first place before it gets stored in the clipboard? I mean there has to be a way to get the text without sending it to the clipboard, right?
But all I could find online were workarounds that write to the clipboard and then quickly restore the old contents.. That's not what I'm looking for, though.
(This question is not about a specific programming language.)
回答1:
Just send EM_GETSEL to the control and then use GetWindowText to obtain the full control text and extract just the selection.
As you may have guessed this is not as efficient as WM_COPY
.
This is because Edit controls are not meant to deal with huge texts, for that (and other purpose) there is the RichEditText control.
It allows you to do some efficient copy by either using:
EM_GETSELTEXT to obtain only the selected text. You still need to know how much space to allocate: this is trivial with the EM_GETSEL or EM_EXGELSEL.
Using EM_GETSEL (or equivalent) and then streaming the text with EM_STREAMOUT.
Needless to say, all of this is accomplished with a single API SendMessage
which, if not already, you have to became confident with.
来源:https://stackoverflow.com/questions/36083784/winapi-getting-text-selection-of-active-window-without-using-the-clipboard