1 //为DllImport导出命名空间, 2 using System.Runtime.InteropServices; 3 public partial class Form1 : System.Windows.Forms.Form 4 { 5 #region FormBorderStyle为None,拖放窗体 6 [DllImport("user32.dll")] 7 public static extern bool ReleaseCapture(); 8 [DllImport("user32.dll")] 9 public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); 10 public const int WM_SYSCOMMAND = 0x0112; 11 public const int SC_MOVE = 0xF010; 12 public const int HTCAPTION = 0x0002; 13 private void Form_MouseDown(object sender, MouseEventArgs e) 14 { 15 ReleaseCapture(); 16 SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); 17 } 18 #endregion 19 #region 构造函数 20 public Form1() 21 { 22 //初始化窗体信息 23 //InitializeComponent(); 24 //绑定鼠标拖动窗体事件 25 this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form_MouseDown); 26 } 27 #endregion 28 }
参考: