如何覆盖Winforms中的消息循环? [英] How to override the message loop in winforms?

查看:78
本文介绍了如何覆盖Winforms中的消息循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我遇到了需要做一些自定义消息过滤的情况.
不幸的是,我需要的功能比IMessageFilter系统提供的功能要多,或者需要覆盖Form的wndproc( )方法.
我需要的是能够在处理消息之前和之后运行代码,并可以选择删除选定的消息.据我了解,

IMessageFilter系统仅允许筛选出选定的消息
(处理完之后无法运行代码)





窗体的wndproc方法仅看到发往该窗体的消息. (我需要处理整个应用程序的所有消息)

在非托管应用程序中,在Visual C ++中说这将是实现自己的getmessage/dispatchmessage循环的简单方法.在dotnet框架中,实际的消息循环是用本机代码实现的,并且程序员无法访问.

有人知道解决方案吗?

请问,
> Felix

Hi All,
I have a situation where I need to do some custom message filtering.
Unfortunately I need more functionality than provided by the
IMessageFilter system or by overriding a Form''s wndproc() method.
What I need is to be able to run code before and after a message is
processed and optionally drop selected messages.

As I understand it:

The IMessageFilter system only allows filtering out selected messages
(can''t run code after they have been processed)

A Form''s wndproc method only sees messages destined for that form. (I
need to handle all messages for the entire application)

In an unmanaged application say in visual c++ it would be a simple
matter of implementing ones own getmessage/dispatchmessage loop. In
the dotnet framework the actual message loop is implemented in native
code and is inaccessible to the programmer.

Does anyone know a solution for this?

Regards,
Felix

推荐答案

不幸的是,您只能在窗体外执行以下操作:使用Application.AddMessageFilter添加您自己的过滤器,并且(据推测) ,这仅适用于在将消息发送到窗口之前捕获消息.您可能会做的(这将是一个骇人听闻的技巧),是使用AddMessageFilter和Mediator的组合来控制发生的情况.您要做的就是挂接到每种表单的WndProc中,然后将详细信息传递给中介程序,以确定是否可以处理该消息.

Unfortunately, the only thing you can do outside the form is use Application.AddMessageFilter to add your own filter, and (as you''ve surmised), this only works on capturing a message before it is dispatched to the window. What you could possibly do (and this would be one heck of a hack), is use a combination of AddMessageFilter and a Mediator to control what happens. What you''d do is hook into the WndProc of each form, and pass the details to the mediator to work out whether or not the message could be processed.


实际上,Internet上有很多关于此的东西.我做过类似的事情,但是没有用.netty做过 Winformy的东西.

Ther''s actually a fair bit out in the wilds of the Internet about this. I have done similar but not with .netty Winformy things.

在Jeff Richter上搜索或形成消息拦截,您可能会更快到达那里.

Search on Jeff Richter or forms message intercept and you might get there more quickly.

对不起,我在这里只有C

Sorry all I have here is C

Best of Luck

Best of Luck


本文代码可能会为您提供您正在寻找

尽管文章中对代码的解释尚有待改进,但它确实涵盖了钩接到事件中的事件,包括不是为窗口指定的全局事件,特别是鼠标和键盘钩子.这可能无法满足您的需求.
This articles code may give you what you are looking for

While the explanation of the code in the article leaves something to be desired, it does cover hooking into events including global events that aren''t destined for your window, specifically mouse and keyboard hooks. This may or not work for your needs.


这篇关于如何覆盖Winforms中的消息循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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