无标题窗口的拖动和改变大小
2005-12-13 22:16 Orin 阅读(858) 评论(0) 编辑 收藏 举报
在 OnMouseDown事件处理中 利用 WinAPI 向窗体发送 WM_NCLBUTTONDOWN 消息,模拟在窗体的标题栏点击鼠标左键操作。典型的调用的代码可能是这样:
将窗体边框设置为None,重载下面的方法可以实现无标题可改变大小的窗口。
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HTCAPTION = 2;
[DllImport("user32.dll")]
private static extern int SendMessage (IntPtr hwnd, int wMsg, int wParam, int lParam)
[DllImport("user32.dll")]
private static extern int ReleaseCapture();
private void form_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//为当前的应用程序释放鼠标捕获
ReleaseCapture();
//发送消息,让系统误以为你在标题拦上按下鼠标
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
private const int HTCAPTION = 2;
[DllImport("user32.dll")]
private static extern int SendMessage (IntPtr hwnd, int wMsg, int wParam, int lParam)
[DllImport("user32.dll")]
private static extern int ReleaseCapture();
private void form_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//为当前的应用程序释放鼠标捕获
ReleaseCapture();
//发送消息,让系统误以为你在标题拦上按下鼠标
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
将窗体边框设置为None,重载下面的方法可以实现无标题可改变大小的窗口。
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0xC0000 + 0x20000;
return cp;
}
}
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0xC0000 + 0x20000;
return cp;
}
}