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;
}
}
}
答案 0 :(得分:0)
在MoseMove上,您可以将位置设置为当前鼠标位置和初始鼠标位置的差异。
pic_TL.Location = new Point(mX - X, mY - Y);
我的情况下鼠标移动了一个像素,图片将移动到左上角。