winform 通过消息实现非客户端区域点击事件

protected override void WndProc(ref Message m)
{
      base.WndProc(ref m);

      switch (m.Msg)
       {
            case 0xA1://WM_NCLBUTTONDOWN
            case 0x84://WM_NCHITTEST
                 if (MouseButtons == MouseButtons.Left)//左键按下
                 {
                     var mousePoint = new Point((int)m.LParam);//鼠标位置(屏幕)
                     //NClientClick(mousePoint); //触发响应的事件即可
                 }
                 break;
       }
}

 private Rectangle ScreenRectangle //控件在屏幕的区域
{
     get
     {
                var r = new RECT();
                GetWindowRect(new HandleRef(this, Handle), ref r);
                var clientRect = new Rectangle(r.Left, r.Top, r.Right - r.Left, r.Bottom - r.Top);
                return clientRect;
      }
}

[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect);

public struct RECT
        {
            /// 
            public int Left;
            /// 
            public int Top;
            /// 
            public int Right;
            /// 
            public int Bottom;
        }

 

你可能感兴趣的:(C#,Winform)