1.去掉winform窗体的标题栏:
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
说明:当FormBorderStyle样式被设置为None模式时,该窗体是无法在移动了。若是要移动窗体,可以通过设置窗体鼠标按下事件和鼠标移动监听事件来实现窗体的移动。
2.实现窗体移动的监听事件:
1)MouseDown鼠标按下时监听事件:鼠标点击窗体时,记录下点击的坐标位置。
2)MouseMove鼠标移动过程中监听:鼠标拖拉窗体移动是,改变窗体坐标位置。
代码:
int x,y; //鼠标点击坐标位置
this.MouseDown += FrmLogin_MouseDown; //设置鼠标按下时监听事件
this.MouseMove += FrmLogin_MouseMove; //设置鼠标移动是监听事件
//鼠标移动监听事件,改变窗体坐标位置,实现窗体移动效果
void FrmLogin_MouseMove(object sender, MouseEventArgs e)
{
if(e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Location = new Point(this.Location.X + (e.X - x),this.Location.Y + (e.Y - y));
}
}
//鼠标按下监听事件,记录鼠标按下时的坐标
void FrmLogin_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == System.Windows.Forms.MouseButtons.Left)
{
x = e.X;
y = e.Y;
}
}
说明:鼠标点击窗体空白地方才有效,如果点击子窗体的子控件上,焦点会被子控件拦截,无法传递到窗体的监听事件,窗体无法移动。比如,在窗体中添加一个panel,并设置panel为fill模式,这时再点击窗体时,panel会拦截掉鼠标事件,点击事件将无法传递到窗体的监听事件中。但是,可以通过同样的方法来对panel控件设置监听事件,从而实现窗体的移动效果。