C# - 从 SetWindowText 获取事件
我们正在为现有的 VB6 应用程序编写一个插件(通过 COM 互操作),并且我们需要一些他们不支持的功能。如果我们能够以某种方式接收到窗口上特定控件的文本发生更改时的事件,我们就可以轻松获得所需的功能。
我们已经可以使用 FindWindow 获取控件的现有窗口句柄,并使用 GetWindowText 获取文本。
当窗口上的文本更改时,是否有任何非托管/pinvoke 方法可以接收事件?
我们不想用 while 循环进行轮询——将来我们可能想在他们的几个控件上执行此操作。
PS - 我知道有方法可以拦截 p/invoke 调用(google EasyHook),但我们不想拦截对 SetWindowText 的每个调用。我们只想为一个控件接收它,而使用 EasyHook 之类的东西可能会增加不必要的复杂性。
We are writing a plugin for an existing VB6 application (via COM interop), and we are requiring some functionality that they do not support. We could easily get the required functionality if we could somehow receive an event for when a particular control's text on their window changes.
We can already grab their existing window handle of the control with FindWindow and get the text with GetWindowText.
Is there any unmanaged/pinvoke method to receive an event when the text on a window is changed?
We do not want to poll with a while loop--we may want to do this on several of their controls in the future.
PS - I know there are ways to intercept p/invoke calls (google EasyHook), but we do not want to intercept every call to SetWindowText. We just want to receive it for one control, and using something like EasyHook is probably adding unnecessary complexity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试在 C# 中创建一个派生自 NativeWindow 的新类。
重写 WndProc 函数,如果消息类型为 WM SETWINDOWTEXT,则添加您的功能 - 忽略所有其他消息并调用基类实现
创建此 C# 类的实例,并使用您获得的控件的 hWnd 调用 AssignHandle 函数查找窗口。
请参阅此链接了解更多详细信息和更深入的解释:
重写 NativeWindow 中的 WndProc类
You could try creating a new class in c# that derives from NativeWindow.
Override the WndProc function, and if the message if of type WM SETWINDOWTEXT add your functionality - ignore all other messages and call the base class implementation
Create an instance of this C# class and call the AssignHandle function with the hWnd for the control that you got with FindWindow.
See this link for further details and a more in depth explanation:
Overriding WndProc in the NativeWindow class
一般的windows没有这个事件。如果控件是编辑控件,它可能会通过 WM_NOTIFY 向其父级发送 EN_CHANGE,但是您不能将托管代码注入到另一个进程来拦截通知。我建议您联系VB6应用程序的作者为您添加COM事件,或者放弃通知要求。
There is no such event for general windows. If the control is an Edit control, it may send EN_CHANGE via WM_NOTIFY to its parent, however you can not inject managed code to another process to intercept the notification. I suggest you to contact the author of the VB6 app to add a COM event for you, or give up the notification requirement.