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
        {
            /// <exclude/>
            public int Left;
            /// <exclude/>
            public int Top;
            /// <exclude/>
            public int Right;
            /// <exclude/>
            public int Bottom;
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章