WinFrom无边框窗体响应边框事件(无边框Winform快捷响应拖动事件)
网上有在窗体上响应鼠标按下事件,并记录鼠标移动轨迹来移动窗体,比较繁琐。
在窗体响应鼠标按下事件,使用Windows窗体的消息来把鼠标按下消息转发至当前窗体,实现响应拖动窗口。
public class MoveWindow { //无边框程序移动窗体 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int VM_NCLBUTTONDOWN = 0XA1;//定义鼠标左键按下 public const int HTCAPTION = 2; }
private void PinForm_MouseDown(object sender, MouseEventArgs e) { //为当前应用程序释放鼠标捕获 MoveWindow.ReleaseCapture(); //发送消息 让系统误以为在标题栏上按下鼠标 MoveWindow.SendMessage((IntPtr)this.Handle, MoveWindow.VM_NCLBUTTONDOWN, MoveWindow.HTCAPTION, 0); }