* 首先将窗体的边框样式修改为None,让窗体没有标题栏 * 实现这个效果使用了三个事件:鼠标按下、鼠标弹起、鼠标移动 * 鼠标按下时更改变量isMouseDown标记窗体可以随鼠标的移动而移动 * 鼠标移动时根据鼠标的移动量更改窗体的location属性,实现窗体移动 * 鼠标弹起时更改变量isMouseDown标记窗体不可以随鼠标的移动而移动 */ private bool isMouseDown = false; private Point FormLocation; //form的location private Point mouseOffset; //鼠标的按下位置 private void Form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseDown = true; FormLocation = this.Location; mouseOffset = Control.MousePosition; } } private void Form1_MouseUp(object sender, MouseEventArgs e) { isMouseDown = false; } private void Form1_MouseMove(object sender, MouseEventArgs e) { int _x = 0; int _y = 0; if (isMouseDown) { Point pt = Control.MousePosition; _x = mouseOffset.X - pt.X; _y = mouseOffset.Y - pt.Y; this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y); } }
WinForm无标题栏窗体的鼠标拖动效果
上一篇: C#转换汉字为ASCII为何都是63呢?
下一篇: WinForm自动加载图标