Laura, miembro de este staff, nos hizo el favor de redactar esta reseña ya que domina perfectamente el tema.
Solución:
Todo lo que necesita:
public partial class Form1 : Form
public Form1()
InitializeComponent();
private Point MouseDownLocation;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
if (e.Button == System.Windows.Forms.MouseButtons.Left)
MouseDownLocation = e.Location;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
if (e.Button == System.Windows.Forms.MouseButtons.Left)
pictureBox1.Left = e.X + pictureBox1.Left - MouseDownLocation.X;
pictureBox1.Top = e.Y + pictureBox1.Top - MouseDownLocation.Y;
También puedes usar la extensión:
public static class CmponentsExtensions
//Management of mouse drag and drop
#region Menu and Mouse
private static bool mouseDown;
private static Point lastLocation;
///
/// To enable control to be moved around with mouse
///
///
///
public static void moveItselfWithMouse(this T control) where T: Control
control.MouseDown += (o, e)=> mouseDown = true; lastLocation = e.Location; ;
control.MouseMove += (o, e) =>
if (mouseDown)
control.Location = new Point((control.Location.X - lastLocation.X) + e.X, (control.Location.Y - lastLocation.Y) + e.Y);
control.Update();
;
control.MouseUp += (o, e) => mouseDown = false; ;
public static void moveOtherWithMouse(this T control, Control movedObject) where T : Control
control.MouseDown += (o, e) => mouseDown = true; lastLocation = e.Location; ;
control.MouseMove += (o, e) =>
if (mouseDown)
movedObject.Location = new Point((movedObject.Location.X - lastLocation.X) + e.X, (movedObject.Location.Y - lastLocation.Y) + e.Y);
movedObject.Update();
;
control.MouseUp += (o, e) => mouseDown = false; ;
#endregion
Entonces necesitas usarlo con algo de control:
En este caso, pictureBox1 movió todo el formulario
pictureBox1.moveOtherWithMouse(this);
En este caso solo mueves pictureBox:
pictureBox1.moveItselfWithMouse();
intente esto para mover el control de PictureBox en tiempo de ejecución usando el mouse
private void pictureBox7_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
if (e.Button == MouseButtons.Left)
xPos = e.X;
yPos = e.Y;
private void pictureBox7_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
PictureBox p = sender as PictureBox;
if (p != null)
if (e.Button == MouseButtons.Left)
p.Top += (e.Y - yPos);
p.Left += (e.X - xPos);
Si piensas que ha resultado útil nuestro artículo, sería de mucha ayuda si lo compartes con más desarrolladores de este modo contrubuyes a difundir nuestra información.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)