Search code examples
c#.netwinformswinapiwndproc

Set SizeAll cursor while moving control by handling NC_HITTEST


I wrote the WndProc method for a moveable control such this:

 protected override void WndProc(ref Message m)
    {
        const int WM_NCHITTEST = 0x0084;


        if (m.Msg == WM_NCHITTEST)
        {

            base.WndProc(ref m);
            if ((int)m.Result == 0x1)
                m.Result = (IntPtr)0x2;

            return;
        }

            base.WndProc(ref m);


    }

and setted SizeAll cursor for the cursor property. but when we set m.Result as i did, the cursor will be Default in any case. How can i do?


Solution

  • You should handle WM_SETCURSOR too.

    Also you may want to hanlde WM_NCLBUTTONDBLCLK to prevent your control from being maximized when you double click on it:

    protected override void WndProc(ref Message m)
    {
        const int WM_NCHITTEST = 0x84;
        const int WM_SETCURSOR = 0x20;
        const int WM_NCLBUTTONDBLCLK = 0xA3;
        const int HTCAPTION = 0x2;
        if (m.Msg == WM_NCHITTEST)
        {
            base.WndProc(ref m);
            m.Result = (IntPtr)HTCAPTION;
            return;
        }
        if (m.Msg == WM_SETCURSOR)
        {
            if ((m.LParam.ToInt32() & 0xffff) == HTCAPTION)
            {
                Cursor.Current = Cursors.SizeAll;
                m.Result = (IntPtr)1;
                return;
            }
        }
        if ((m.Msg == WM_NCLBUTTONDBLCLK))
        {
            m.Result = (IntPtr)1;
            return;
        }
        base.WndProc(ref m);
    }