Tuesday, January 20, 2004

You probably already know about a brilliant ApplicationEx class created by Chris for OpenNETCF.  It allows getting every single Windows message that your app receives.  I 've put together a IMessageFilter implementation that would filter out the messages that are send to a particular control on your Form or to the Form itself if you wish... Here is the code that for IMessageFilter implementation:

//Delegate to raise a event to a client

public delegate void WinProc(ref Message m);

 

class WinProcFilter : IMessageFilter

{

      public WinProc WndProc;

      private IntPtr handle;

 

      public WinProcFilter(Control ctl)

      {

            //Get control's window handle           

            ctl.Capture = true;

            this.handle = GetCapture();

            ctl.Capture = false;

      }

      #region IMessageFilter Members

      public bool PreFilterMessage(ref Message m)

      {

            bool handled = false;

            if (WndProc!=null)

            {

                  if (m.HWnd == handle) //Check window handle event

                  {

                      if (WndProc!=null) 

                       WndProc(ref m); //Raise the event

                  }

            }

            return handled;

      }

      #endregion

 

      [DllImport("coredll.dll")]

      private static extern IntPtr GetCapture();

}

On the client form you would need to declare and instanciate WinProcFilter class:

//Let's get the winproc for some TextBox

WinProcFilter procFilter = new WinProcFilter(textBox1);

//Hook up to WinProc event

procFilter.WndProc+=new WinProc(textBox1_WinProc);

.....

 

private void textBox1_WinProc(ref Message m)

{

    //Do whatever you like with the messages.

}

 

1/20/2004 5:02:04 PM (GMT Standard Time, UTC+00:00)  #     |