Foros del Web » Programación para mayores de 30 ;) » .NET »

Arrastrar y soltar (drag&drop)

Estas en el tema de Arrastrar y soltar (drag&drop) en el foro de .NET en Foros del Web. hola gente, mira tengo que hacer el siguiente ejercicio: Sabeis el tipico arkanoid, bueno pues me lo han mandado como practica. En un form tengo ...
  #1 (permalink)  
Antiguo 11/01/2012, 17:23
 
Fecha de Ingreso: septiembre-2011
Mensajes: 87
Antigüedad: 12 años, 7 meses
Puntos: 6
Arrastrar y soltar (drag&drop)

hola gente, mira tengo que hacer el siguiente ejercicio:

Sabeis el tipico arkanoid, bueno pues me lo han mandado como practica. En un form tengo dos picturebox, uno donde estan los tochos a arrastrar(son 3 cochos, cada uno mas duro k el otro y los puedo arrastrar varias veces) y el otro PB que contendran los tochos arrastrados y donde empezare la partida.

He probado muxos codigos pero no me acabo de salir para hacer el drag and drop. Me esta resultando muy dificl.

Código:
Private btnDown As Integer
    Private offsetX As Integer
    Private offsetY As Integer
    Private llista As List(Of PictureBox)
    Private Sub rect3_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rect3.MouseDown, rect2.MouseDown, rect1.MouseDown

        'el boton izquierdo esta pulsado
        If (e.Button = MouseButtons.Left) Then
            btnDown = True
            offsetX = e.X
            offsetY = e.Y
        End If
    End Sub

    Private Sub rect3_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rect3.MouseMove, rect2.MouseMove, rect1.MouseMove

        If (btnDown) Then
            'mover el pictureBox con el raton
            Dim pb As PictureBox = New PictureBox()
            pb.Image = CType(sender, PictureBox).Image
            pb.Left += e.X + PPantalla.Location.X
            pb.Top += e.Y + PPantalla.Location.Y
            'pb.Location = New Point(e.X + PPantalla.Location.X - offsetX, e.Y + PPantalla.Location.Y - offsetY)
            'PPantalla.Controls.Add(pb)
            

        End If
    End Sub

    Private Sub rect3_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rect3.MouseUp, rect2.MouseUp, rect1.MouseUp

        'el boton izquierdo se libera
        If (e.Button = MouseButtons.Left) Then
            btnDown = False
            Dim pb As PictureBox = New PictureBox()
            pb.Image = CType(sender, PictureBox).Image
            pb.Location = New Point(e.X - PPantalla.Location.X + offsetX, e.Y - PPantalla.Location.Y + offsetY)
            PPantalla.Controls.Add(pb)
        End If
    End Sub
no se como seguir. necesito ayudita.

Gracias,, saludos
  #2 (permalink)  
Antiguo 11/01/2012, 18:40
Avatar de drako_darpan  
Fecha de Ingreso: octubre-2008
Ubicación: Sinaloa
Mensajes: 617
Antigüedad: 15 años, 6 meses
Puntos: 58
Respuesta: Arrastrar y soltar (drag&drop)

Hola que tal, bueno gracias a tu duda me quedo mas clara una pregunta similar a tu duda , un pequeño Deja VU

Para el Drag&Drop en C#, seria asi:

Código C#:
Ver original
  1. private Image picture;
  2.         private Point pictureLocation;
  3.  
  4.         public Form1()
  5.         {
  6.             InitializeComponent();
  7.         }
  8.  
  9.         private void Form1_Load(object sender, EventArgs e)
  10.         {
  11.             this.AllowDrop = true;
  12.             this.DragDrop += new DragEventHandler(this.Form1_DragDrop);
  13.             this.DragEnter += new DragEventHandler(this.Form1_DragEnter);
  14.         }
  15.  
  16.         protected override void OnPaint(PaintEventArgs e)
  17.         {
  18.            // If there is an image and it has a location,
  19.            // paint it when the Form is repainted.
  20.            base.OnPaint(e);
  21.            if(this.picture != null && this.pictureLocation != Point.Empty)
  22.            {
  23.               e.Graphics.DrawImage(this.picture, this.pictureLocation);
  24.            }
  25.         }
  26.  
  27.         private void Form1_DragDrop(object sender, DragEventArgs e)
  28.         {
  29.            // Handle FileDrop data.
  30.            if(e.Data.GetDataPresent(DataFormats.FileDrop) )
  31.            {
  32.               // Assign the file names to a string array, in
  33.               // case the user has selected multiple files.
  34.               string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
  35.               try
  36.               {
  37.                  // Assign the first image to the picture variable.
  38.                  this.picture = Image.FromFile(files[0]);
  39.                  // Set the picture location equal to the drop point.
  40.                  this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
  41.               }
  42.               catch(Exception ex)
  43.               {
  44.                  MessageBox.Show(ex.Message);
  45.                  return;
  46.               }
  47.            }
  48.  
  49.            // Handle Bitmap data.
  50.            if(e.Data.GetDataPresent(DataFormats.Bitmap) )
  51.            {
  52.               try
  53.               {
  54.                  // Create an Image and assign it to the picture variable.
  55.                  this.picture = (Image)e.Data.GetData(DataFormats.Bitmap);
  56.                  // Set the picture location equal to the drop point.
  57.                  this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
  58.               }
  59.               catch(Exception ex)
  60.               {
  61.                  MessageBox.Show(ex.Message);
  62.                  return;
  63.               }
  64.            }
  65.            // Force the form to be redrawn with the image.
  66.            this.Invalidate();
  67.         }
  68.  
  69.         private void Form1_DragEnter(object sender, DragEventArgs e)
  70.         {
  71.             // If the data is a file or a bitmap, display the copy cursor.
  72.             if (e.Data.GetDataPresent(DataFormats.Bitmap) ||
  73.                e.Data.GetDataPresent(DataFormats.FileDrop))
  74.             {
  75.                 e.Effect = DragDropEffects.Copy;
  76.             }
  77.             else
  78.             {
  79.                 e.Effect = DragDropEffects.None;
  80.             }
  81.         }

El ejemplo lo revise en MSD de esta ruta:
http://msdn.microsoft.com/es-es/libr...=vs.90%29.aspx

Espero te sirva, si funciona por que ya lo probe, pero ahora quiero poder mover la imagen que anexe , pero eso me toca buscarle a mi, suerte

Etiquetas: visual
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:47.