如何移动无标题栏窗体,用鼠标事件, 用窗体上的控件拖拽

class Form{
    private const int WM_NCHITTEST = 0x0084;
    private const int HTCAPTION = 0x0002;
    protected override void WndProc(ref Message m) {
 switch (m.Msg) {
     case WM_NCHITTEST:
  m.Result = (IntPtr) HTCAPTION;
  break;
     default:
  base.WndProc (ref m);
  break;
 }
    }
}

以上的代码是可以在form的客户区用鼠标移动窗体。

以下代码窗体上有个label 控件,拖拽 label 就能移动窗体

[System.Runtime.InteropServices.DllImport("user32.dll",EntryPoint="SendMessage")]
  public static extern int SendMessage(int hWnd,int wMsg,int wParam,int lParam);
  [System.Runtime.InteropServices.DllImport("user32.dll",EntryPoint="ReleaseCapture")]
  public static extern int ReleaseCapture();
  public const int WM_SysCommand = 0x0112;
  public const int SC_MOVE = 0xF012;

  private void label1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   ReleaseCapture();
   SendMessage(this.Handle.ToInt32(),WM_SysCommand,SC_MOVE,0);
  }

你可能感兴趣的:(鼠标事件)