在我们使用Winform窗体的时候,由于不喜欢WinForm的边框样式的时候,希望用字自己的设置的样式来进行美观的渲染,这样的话就没有办法像Winform那样拖动窗体,在这里我发现一个好东西分享给大家
可以在窗体的上面放一个Panel控件,使用MouseDown事件
将代码放在Form1中
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private const int VM_NCLBUTTONDOWN = 0XA1;//定义鼠标左键按下
private const int HTCAPTION = 2;
在Panel中添加一下Panel_MouseDown中的代码,就可以随意的拖动的窗口了
private void Panel_MouseDown(object sender, MouseEventArgs e)
{
//为当前应用程序释放鼠标捕获
ReleaseCapture();
//发送消息 让系统误以为在标题栏上按下鼠标
SendMessage((IntPtr)this.Handle, VM_NCLBUTTONDOWN, HTCAPTION, 0);
}