首先:将Form的FormBorderStyle设置为None.
然后,在工具箱中拖一个Panel控件到Form中,并将Panel控件的Dock设置为:Top.
这时的Form因为没有边框,是不能拖动的,接下来,我们来实现拖动效果:
先注册Panel的MouseDown和MouseMove事件,
Point point = new Point();
bool isMove = false;//是否移动
private void pTop_MouseDown(object sender, MouseEventArgs e)
{
point = e.Location;//按住的点
isMove = true;
}
private void pTop_MouseMove(object sender, MouseEventArgs e)
{
Point pointNew;
if (e.Button == MouseButtons.Left && isMove)
{
pointNew = e.Location;//按住的点拖动到的位置
Point fPointNew = new Point(pointNew.X - point.X, pointNew.Y - point.Y);//相对于原来起点的距离点的描述
this.Location += new Size(fPointNew);
}
}