项目中需要对无标题栏窗口单击拖动功能 , 调用 window api 实现比较简洁. 如下源码,单击窗口任意位置,调用Win API , 实现窗口拖动。
Window API:
[DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
拖动实现:
//初使化窗口,注册所有控件单击事件 private void EngineVerify_Load(object sender, EventArgs e) { BindMouseDown(tableLayoutPanel1.Controls); } void BindMouseDown(Control.ControlCollection cc) { foreach (Control c in cc) { c.MouseDown += C_MouseDown; if (c.Controls.Count > 0) { BindMouseDown(c.Controls); } } } private void C_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(Handle, 274, 61440 + 9, 0); }