WinAPI: Getting text selection of active window without using the clipboard

喜夏-厌秋 提交于 2019-12-08 03:28:15

问题


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:

  1. 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.

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!