C# override WndProc in Control level to detect

Posted by Nullstr1ng on Stack Overflow See other posts from Stack Overflow or by Nullstr1ng
Published on 2010-05-05T06:28:29Z Indexed on 2010/05/05 6:48 UTC
Read the original article Hit count: 425

Filed under:
|

I have overridden WndProc in UserControl level to detect MouseDown, MouseUp, and MouseMove to any Control added in that UserControl.

protected override void WndProc(ref Message m)
    {
        Point mouseLoc = new Point();

        switch (m.Msg)
        {
            case WM_LBUTTONDOWN:
                System.Diagnostics.Debug.WriteLine("mouse down");
                //this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, mouseLoc.X, mouseLoc.Y, 0));

                break;
            case WM_LBUTTONUP:
                System.Diagnostics.Debug.WriteLine("mouse up");
                //this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, mouseLoc.X,mouseLoc.Y, 0));

                break;
            case WM_MOUSEMOVE:
                int lParam = m.LParam.ToInt32();

                //mouseLoc.X = lParam & 0xFFFF;
                //mouseLoc.Y = (int)(lParam & 0xFFFF0000 >> 16);

                mouseLoc.X = (Int16)m.LParam;
                mouseLoc.Y = (Int16)((int)m.LParam >> 16);

                System.Diagnostics.Debug.WriteLine("mouse move: " + mouseLoc.X + ", " + mouseLoc.Y);

                //this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, mouseLoc.X,mouseLoc.Y, 0));
                break;
        }

        base.WndProc(ref m);
    }

MouseMove, Down, and Up are working when the mouse pointer is in UserControl but when the mouse pointer is on other control (inside my UserControl) it doesn't work.

Am I doing something wrong?

Currently developing a flick and scroll control.

© Stack Overflow or respective owner

Related posts about usercontrols

Related posts about override