C#移动窗体的问题

来源:百度知道 编辑:UC知道 时间:2024/09/20 07:06:24
我用了两种方法一种正常,一种不正常
第一种不正常,能否告诉我为何?
private void Main_MouseDown(object sender, MouseEventArgs e)
{
m__RectOrg.X = e.X;
m__RectOrg.Y = e.Y;

}

private void Main_MouseMove(object sender, MouseEventArgs e)
{
if (MouseButtons.Left == e.Button)
{
Left -= m__RectOrg.X - e.X;
Top += m__RectOrg.Y - e.Y;

m__RectOrg.X = e.X;
m__RectOrg.Y = e.Y;
}
}
想法是每次移动鼠标加上与上一次的差值,但效果不一样,大家试一下回答我

int x0, y0;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
x0 = e.X;
y0 = e.Y;

this.Capture = true;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.Capture)
{
int x1 = e.X;
int y1 = e.Y;

this.Location = new Point(this.Left + (x1 - x0), this.Top + (y1 - y0));
}
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
this.Capture = false;
}

需要用2个全局变量来记录上一次的x,y