1.添加下列代码到你的窗体中:
#region 轻松移动
bool isInMove;
Point oldPoint;
void InitializeEasyMove()
{
isInMove = false;
this.MouseDown += new MouseEventHandler(EasyMove_MouseDown);
this.MouseUp += new MouseEventHandler(EasyMove_MouseUp);
this.MouseMove += new MouseEventHandler(EasyMove_MouseMove);
}
void EasyMove_MouseMove(object sender, MouseEventArgs e)
{
if (!isInMove) return;
Point pt = PointToScreen(e.Location);
if (pt.X == oldPoint.X || pt.Y == oldPoint.Y) return;
this.Location = new Point(this.Location.X + pt.X - oldPoint.X, this.Location.Y + pt.Y - oldPoint.Y);
oldPoint = pt;
}
void EasyMove_MouseUp(object sender, MouseEventArgs e)
{
isInMove = false;
}
void EasyMove_MouseDown(object sender, MouseEventArgs e)
{
isInMove = true;
oldPoint = PointToScreen(e.Location);
}
#endregion
2.在你的窗体的构造函数或Load事件中调用:
InitializeEasyMove();
#region 轻松移动
bool isInMove;
Point oldPoint;
void InitializeEasyMove()
{
isInMove = false;
this.MouseDown += new MouseEventHandler(EasyMove_MouseDown);
this.MouseUp += new MouseEventHandler(EasyMove_MouseUp);
this.MouseMove += new MouseEventHandler(EasyMove_MouseMove);
}
void EasyMove_MouseMove(object sender, MouseEventArgs e)
{
if (!isInMove) return;
Point pt = PointToScreen(e.Location);
if (pt.X == oldPoint.X || pt.Y == oldPoint.Y) return;
this.Location = new Point(this.Location.X + pt.X - oldPoint.X, this.Location.Y + pt.Y - oldPoint.Y);
oldPoint = pt;
}
void EasyMove_MouseUp(object sender, MouseEventArgs e)
{
isInMove = false;
}
void EasyMove_MouseDown(object sender, MouseEventArgs e)
{
isInMove = true;
oldPoint = PointToScreen(e.Location);
}
#endregion
2.在你的窗体的构造函数或Load事件中调用:
InitializeEasyMove();