WinAPI:在不使用剪贴板的情况下获取活动窗口的文本选择 [英] WinAPI: Getting text selection of active window without using the clipboard

查看:33
本文介绍了WinAPI:在不使用剪贴板的情况下获取活动窗口的文本选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以通过发送 WM_COPY 消息,然后读取剪贴板的新内容.
但是,Windows 如何在文本选择存储到剪贴板之前首先检索它呢?我的意思是必须有一种方法可以在不将文本发送到剪贴板的情况下获取文本,对吗?
但是我在网上能找到的只是写到剪贴板然后快速恢复旧内容的解决方法.不过,这不是我要找的.

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

推荐答案

只需发送 EM_GETSEL 到控件,然后使用 GetWindowText 获取完整的控件文本并仅提取选择.

Just send EM_GETSEL to the control and then use GetWindowText to obtain the full control text and extract just the selection.

正如您可能已经猜到的那样,这不如 WM_COPY 有效.
这是因为 Edit 控件不是用来处理大文本的,为此(和其他目的)有 RichEditText 控件.

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 仅获取选定的文本.您仍然需要知道要分配多少空间:这对于 EM_GETSELEM_EXGELSEL.

  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.

使用 EM_GETSEL(或等效的),然后使用 EM_STREAMOUT.

Using EM_GETSEL (or equivalent) and then streaming the text with EM_STREAMOUT.

不用说,所有这些都是通过一个 API SendMessage 完成的,如果还没有,您必须对它充满信心.

Needless to say, all of this is accomplished with a single API SendMessage which, if not already, you have to became confident with.

这篇关于WinAPI:在不使用剪贴板的情况下获取活动窗口的文本选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆