c#拖动picturebox控件
如题,拖动的方法我已经写好了如下
bool whetherSelected = false;
Point pt = new Point();
private void pbBigImage_MouseDown(object sender, MouseEventArgs e)
{
whetherSelected = true;
pt = Cursor.Position;
this.Cursor = System.Windows.Forms.Cursors.SizeAll;
}
private void pbBigImage_MouseMove(object sender, MouseEventArgs e)
{
if (whetherSelected)
{
Point newPoint = new Point();
if (pbBigImage.Width > panel2.Width)
{
newPoint.X = pbBigImage.Left + Cursor.Position.X - pt.X;
}
if (pbBigImage.Height > panel2.Height)
{
newPoint.Y = pbBigImage.Top + Cursor.Position.Y - pt.Y;
}
pbBigImage.Location = newPoint;
pt = Cursor.Position;
}
}
private void pbBigImage_MouseUp(object sender, MouseEventArgs e)
{
whetherSelected = false;
this.Cursor = System.Windows.Forms.Cursors.Default;
}
private void pbBigImage_MouseMove(object sender, MouseEventArgs e)
{
if (whetherSelected)
{
Point newPoint = new Point();
if (pbBigImage.Width > panel2.Width)
{
if (pbBigImage.Right < panel2.Width)
{
newPoint.X = panel2.Width- pbBigImage.Width;
}
else if (pbBigImage.Left>0)
{
newPoint.X = 0;
}
else
newPoint.X = pbBigImage.Left + Cursor.Position.X - pt.X;
}
if (pbBigImage.Height > panel2.Height)
{
if (pbBigImage.Bottom<panel2.Height)
{
newPoint.Y=panel2.Height-pbBigImage.Height;
}
else if (pbBigImage.Top > 0)
{
newPoint.Y = 0;
}
else
newPoint.Y = pbBigImage.Top + Cursor.Position.Y - pt.Y;
}
pbBigImage.Location = newPoint;
pt = Cursor.Position;
}
}