通过处理NC_HITTEST在移动控件时设置SizeAll光标

时间:2015-11-02 15:43:12

标签: c# .net winforms winapi wndproc

我为可移动控件编写了WndProc方法:

 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);


    }

并为游标属性设置SizeAll游标。但是当我像我一样设置m.Result时,光标在任何情况下都是Default。我该怎么办?

1 个答案:

答案 0 :(得分:4)

你也应该处理WM_SETCURSOR

此外,您可能希望隐藏WM_NCLBUTTONDBLCLK,以防止双击时控件被最大化:

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);
}