C#实现控件拖动窗口
1. 添加Using 声明
using System.Runtime.InteropServices;
2.在类中加入如下API代码:
[DllImport("user32")] private static extern bool ReleaseCapture(); [DllImport("user32")] private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0Xf010; public const int HTCAPTION = 0x0002;
3.在需要拖动的Form或控件的MouseDown事件中加入以下代码:
ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE +HTCAPTION,0);
完成;