Does IMessageFilter work?

M

Michael Culley

I've added an IMessageFilter to my app but I don't get messages or I get only a few and then it stops. My code is very simple. Am I
doing something wrong?

public class MF : IMessageFilter
{
public MF()
{
}
public bool PreFilterMessage(ref Message m)
{
if(m.Msg == 15)
{
Console.WriteLine("Paint");
}
return false;
}
}

and

Application.AddMessageFilter(new MF());

I've tried putting this in different locations, such as void Main, Form_Load etc
 
B

BMermuys

Hi,

A simple test runs fine here, can't say what you're doing wrong.

There must be a message loop (Application.Run/ Modal form). Try putting
this in the static main method:

Application.AddMessageFilter( new MF() );
Application.Run( new MainForm() );

You can not place AddMessage after Run, because Run is the message loop,
which doesn't stop until the app is closed.

If you only place Application.Run( new MainForm() ) in the main method,
it should work if you call Application.AddMessageFilter from any
control/form event.


HTH
greetings

Michael Culley said:
I've added an IMessageFilter to my app but I don't get messages or I get
only a few and then it stops. My code is very simple. Am I
 
M

Michael Culley

Does it send you the paint message every time your form is painted, eg if another windows is dragged over it. On mine I get a few
paint messages at the start and then nothing.

--
Michael Culley


BMermuys said:
Hi,

A simple test runs fine here, can't say what you're doing wrong.

There must be a message loop (Application.Run/ Modal form). Try putting
this in the static main method:

Application.AddMessageFilter( new MF() );
Application.Run( new MainForm() );

You can not place AddMessage after Run, because Run is the message loop,
which doesn't stop until the app is closed.

If you only place Application.Run( new MainForm() ) in the main method,
it should work if you call Application.AddMessageFilter from any
control/form event.


HTH
greetings

Michael Culley said:
I've added an IMessageFilter to my app but I don't get messages or I get
only a few and then it stops. My code is very simple. Am I
 
B

BMermuys

Michael Culley said:
Does it send you the paint message every time your form is painted, eg if
another windows is dragged over it.

Yes, it does. With your MF class, I get correct nr of paint messages.
On mine I get a few
paint messages at the start and then nothing.

Can't help you more, is there anything special about the app ? Have you
tried it with a new project ? On a different machine, etc....

HTH
greetings

 
M

Michael Culley

Thanks for the reply.

I've found the problem, not sure if it is a bug or not. Every time I was testing the message filter I was testing it by moving the
form off the edge of the screen and back on to get it to fire paint messages. For some reason when you are moving the form the
IMessageFilter stops working and it misses all messages. If I override the WndProc of the form it gets all the messages but the
IMessageFilter does not. Any ideas what's going on?
 
M

Michael Culley

It doesn't seem to get WM_COPYDATA messages either or any message sent from another app. Basically it seems to drop so many messages
that it is unusable.
 
B

BMermuys

Michael Culley said:
Thanks for the reply.

I've found the problem, not sure if it is a bug or not. Every time I was
testing the message filter I was testing it by moving the
form off the edge of the screen and back on to get it to fire paint
messages. For some reason when you are moving the form the
IMessageFilter stops working and it misses all messages. If I override the
WndProc of the form it gets all the messages but the
IMessageFilter does not. Any ideas what's going on?

This has to do with the difference between posting or sending messsages.

* Posted messages are added to the queue and eventually dispatched in order
by the message loop.

* Sended messages are handled differently.
- If you sent a message to a window from the thread that created the window
then the wndproc is directly called like a subroutine.

- Sended messages form different threads execute the wndproc when the
message loop queries for a new message, but they are not dispatched by the
message loop, windows does it transparantly.


HTH
greetings
 
M

Michael Culley

Looks like it is not going to work for me because I am after the WM_COPYDATA message and this seems to be sent on a different thread
and an IMessageFilter will only work on one thread. I am currently overriding WndProc but it seems to play funny buggers with a com
component I am using so I was looking for a different solution.

Thanks for your help.

--
Michael Culley


BMermuys said:
Michael Culley said:
Thanks for the reply.

I've found the problem, not sure if it is a bug or not. Every time I was
testing the message filter I was testing it by moving the
form off the edge of the screen and back on to get it to fire paint
messages. For some reason when you are moving the form the
IMessageFilter stops working and it misses all messages. If I override the
WndProc of the form it gets all the messages but the
IMessageFilter does not. Any ideas what's going on?

This has to do with the difference between posting or sending messsages.

* Posted messages are added to the queue and eventually dispatched in order
by the message loop.

* Sended messages are handled differently.
- If you sent a message to a window from the thread that created the window
then the wndproc is directly called like a subroutine.

- Sended messages form different threads execute the wndproc when the
message loop queries for a new message, but they are not dispatched by the
message loop, windows does it transparantly.


HTH
greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top