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

我为这样的可移动控件编写了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);


    }

并为cursor属性设置SizeAll光标.但是当我们像我一样设置m.Result时,在任何情况下光标都将为Default.我能怎么做?

解决方法:

您也应该处理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);
}
上一篇:linux安装至少有哪两个分区,各自作用是什么?


下一篇:C# hook WndProc