【发布时间】:2014-07-09 21:39:24
【问题描述】:
我让图片框移动了。但是当我点击图片框时,它会跳遍整个地方。谁能帮我解决这个问题?
namespace Move_Shapes
{
public partial class Form1 : Form
{
int X = 0;
int Y = 0;
int mX = 0;
int mY = 0;
bool Move = false;
public Form1()
{
InitializeComponent();
}
private void pic_TL_Click(object sender, EventArgs e)
{
//X = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
//Y = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;
//label1.Text = X.ToString();
//label2.Text = Y.ToString();
//X = MousePosition.X - this.Location.X - 8;
//Y = MousePosition.Y - this.Location.Y - 30;
//label3.Text = X.ToString();
//label4.Text = Y.ToString();
}
private void pic_TL_MouseDown(object sender, MouseEventArgs e)
{
X = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
Y = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;
label1.Text = X.ToString();
label2.Text = Y.ToString();
Move = true;
}
private void pic_TL_MouseMove(object sender, MouseEventArgs e)
{
mX = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
mY = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;
if (Move)
{
pic_TL.Location = new Point(mX - X, mY - Y);
}
}
private void pic_TL_MouseUp(object sender, MouseEventArgs e)
{
Move = false;
}
}
}
【问题讨论】:
-
请修改您的标题以解释您的问题而不是您的一般情况。
-
对此很陌生,但基本上当我点击鼠标时会发生什么,图片框会前后跳动,不知道为什么
-
这些神奇的常数是什么:8、30?
-
表格左侧的距离与表格顶部的高度
-
它跳跃,因为公式错误。基本上,记住
MouseDown事件中的鼠标位置(顺便说一句,从e获取,而不是从MousePosition获取)和MouseMove事件更改位置:pic_TL.X += oldMouseX - e.X,Y相同。