How to catch windows messages in normal class

Hi,

During the development of a project with threads i was supposed to use windows messages. I know how to catch windows messages in forms, but not in a normal class. Can anybody help me with this problem

With kind regards,

Jeroen Olie




Answer this question

How to catch windows messages in normal class

  • v-pakote

    Look at the documentation for IMessageFilter. This allows you to specify any class to get a peek at Windows' messages flowing to your application, even if that class is not a Form or Control.


  • Nyasha

    what u r meaning about window message -- is this

    1. Showing Message box.

    2. Catching Exception.

    3. Events.

    or any other



  • SpeakerBob

    I’m sticking my neck out here, but I’m not sure what you mean by a ‘normal’ class. The Windows message pump that you’re talking about is specific to Windows Forms. In other words, the form is needed to receive/intercept them. No form, no message pump.

    Unless, of course, you’re talking about message queues. :-)

    Anyway, more details about what you’re trying to accomplish might help.

    Bruce Johnson [C# MVP]
    http://www.objectsharp.com/blogs/bruce




  • thiti300

    Hi

    is this what you are looking for

  • Astrogator

    Hoping I have interpreted the original question properly, the desire was to peek in on the messages passing without having to do so within' a specific Form.

    This is very possible as an application itself has a single Messaging Queue that all messages pass through on their way to appropriate Forms/Controls.

    By creating an IMessageFilter-enabled class and registering it, you can do just this. Using the code from the MSDN documentation, adding the following class to your application... and adding

    Application.AddMessageFilter( new TestMessageFilter() );

    Will allow every message to pass through your PreFilterMessage(). While situational, this is a very powerful and useful capability which I have actually needed to use on several occasions.

    public class TestMessageFilter : System.Windows.Forms.IMessageFilter
    {
    public bool PreFilterMessage(ref System.Windows.Forms.Message m)
    {
    // Blocks all the messages relating to the left mouse button.
    if (m.Msg >= 513 && m.Msg <= 515)
    {
    Console.WriteLine("Processing the messages : " + m.Msg);
    return true;
    }
    return false;
    }
    }


  • How to catch windows messages in normal class