实现效果:
知识运用:
API中的两个函数 ReleaseCapture和SendMessage
[DllImport("user32")] //用于向指定的窗体发送windows消息 public static extern bool SendMessage(IntPtr hwdn,int wMsg,int mParaam,int IParam);
实现代码:
public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; [DllImport("user32")] public static extern bool ReleaseCapture(); [DllImport("user32")] public static extern bool SendMessage(IntPtr hwdn,int wMsg,int mParaam,int IParam); private void Form1_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); //用来释放当前线程中某个窗口捕获的光标 SendMessage(this.Handle,WM_SYSCOMMAND,SC_MOVE+HTCAPTION,0); //向Windows发送拖动窗体的消息 }