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

标签 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。我该怎么办?

最佳答案

您也应该处理 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);
}

关于c# - 通过处理 NC_HITTEST 在移动控件时设置 SizeAll 光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33481422/

相关文章:

c# - WCF SslStreamSecurity DNS 身份检查仅针对 4.6 框架失败

c# - 禁用组合框中的特定项目

c# - 从用户控件代码隐藏,在设计模式下,如何获取父控件中包含的所有控件名称?

c# - DefaultCredentials 在哪里拉出 "UserName"作为 web 请求的用户名?

c# - 尝试激活时无法解析类型 'Microsoft.AspNetCore.Mvc.IUrlHelper' 的服务

c# - 如何在 asp.net MVC 4 和 MVC 5 中设置默认 Controller

c# - Rider 工具中没有 EF 作为选项

c# - Nancy - 两个模块监听不同的端口

c# - 你能启动一个应用程序 C# 所以它不需要 .NET

c# - NHibernate:来自表 X 的关联引用未映射的类:X