简体   繁体   English

如何在Win32中为RichEdit设置回调机制

[英]How do I setup a callback mechanism for RichEdit in win32

In win32, how do I setup a callback mechanism for RichEdit I have not created myself? 在win32中,如何为尚未创建自己的RichEdit设置回调机制?

PART 1 第1部分

I'm reading from a textedit field in another application's GUI . 我正在从另一个应用程序的GUI中的textedit字段中读取 This works just fine now, except after the first read I'd like to fetch only new or modified lines. 现在可以正常工作,除了第一次读取后,我只想获取新行或修改过的行。 In GTK+ or Qt I'd just install a callback on some signal the field edits when its changed, but how does it work on Win32? 在GTK +或Qt中,我只是在某些信号上安装了回调,当字段更改时会对其进行编辑,但是在Win32上它如何工作?

My MSDN searches result with nothing useful, probably because I don't know the exact term to search for. 我的MSDN搜索结果没有任何用处,可能是因为我不知道要搜索的确切术语。 The class of the textedit is RichText20W , and it has some messages that are probably used somehow, though that article just discusses using them for the parent of the class. textedit的类是RichText20W ,它具有一些可能以某种方式使用的消息 ,尽管该文章只是讨论了如何将它们用作类的父类。

PART 2 第2部分

Also, if there is no such "text changed, here is the newly inserted text" callback which returns the new content immediately, I need some way to easily detect what is new. 另外, 如果没有这样的“文本更改,这是新插入的文本”回调,该回调立即返回新内容,则我需要某种方式来轻松检测到新内容。 From top-of-my-head: 从我的头上来:

  1. Have a marker at the end of the text block we've read, and only read between that and the end. 在我们阅读的文本块的末尾有一个标记,并且只能在该文本和末尾之间阅读。
  2. Store what we've read previously, and after a second read, remove the duplicate part from the latter to have the newly inserted stuff. 存储我们之前阅读的内容,然后进行第二次阅读,然后从后者中删除重复的部分,以获取新插入的内容。

Option 2 might not be viable, since the textedit can contain any amount of text. 选项2可能不可行,因为textedit可以包含任意数量的文本。 The marker part sounds doable, but yet again, my feeble Win32 skills and horrible Win32 function names prevent me from finding the right way to do it. 标记部分听起来很可行,但我的Win32技能薄弱和Win32函数名称糟糕,这又一次阻止了我找到正确的方法。

Note that all these must be doable for a textedit I do not own and have not created, they belong to a third party process. 请注意,所有这些对于我拥有且尚未创建的textedit必须是可行的,它们属于第三方进程。

Code samples in C++ highly appreciated. C ++中的代码示例受到高度赞赏。

Disclaimer 放弃

Obviously, if there is some better way of doing it, let me know. 显然,如果有更好的方法,请告诉我。 I only assumed callback would be the way to go based on my previous experience with GTK+/Qt. 根据我以前使用GTK + / Qt的经验,我仅假设回调是要使用的方法。 Feel free to show me the path :) 随时向我展示路径:)

Win32 controls don't work on message-specific callbacks that you can subscribe to. Win32控件不适用于您可以订阅的特定于消息的回调。 They just send messages to their parent window when something happens, in this case EN_UPDATE, EN_CHANGE and all that. 它们只是在发生某些情况(在本例中为EN_UPDATE,EN_CHANGE等)时将消息发送到其父窗口。 Even these events don't tell you what text changed. 即使这些事件也不能告诉您什么文本已更改。 They only tell you that it did change. 他们只告诉你它确实改变了。

You could subclass the parent, but the documentation for SetWindowLongPtr explicitly says you "should not subclass a window class created by another process." 您可以对父类进行子类化,但是SetWindowLongPtr的文档明确声明您“不应对由另一个进程创建的窗口类进行子类化”。 Something like this is probably possible with hooks, but I haven't used them enough to say for certain how you'd actually do it. 钩子可能会出现这种情况,但是我还没有足够地使用它们来确定您实际将如何使用它。

我意识到这是一篇过时的文章,但是这篇文章似乎在做类似的事情。

Based on Joel's answer, I quit looking for callbacks and just made a small class that hooks itself (not by a real API hook though) to the richedit and polls it once a second for content length, and if it has changed since the last poll, it asks for the content, compares that to previous known content and emits a signal with the changed content. 根据Joel的回答,我退出寻找回调,只是制作了一个小类,将其自身(虽然不是通过真正的API钩子)挂接到richedit,并每秒对其内容长度进行一次轮询,以及自上次轮询以来是否已更改,它请求内容,将其与以前的已知内容进行比较,并发出内容已更改的信号。

This seems to work OK for this purpose, but if someone knows a better way still (some real and tested way of doing it via API hooks or something), please post. 为此,这似乎可以正常工作,但是如果有人仍然知道更好的方法(通过API挂钩或其他某种经过实践验证的方法),请发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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