c# 实现窗体移动

发布时间:2009年07月10日      浏览次数:703 次
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();
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!