Drag&Drop images on a form (example of)  

Send By: Q3 Team
Web : http://www.q3.nu
Email: dlib@q3.nu
Date: 11/07/99

Tip accessed 765 times

 


This example is the answer to a user request.
It is to be able to make Drag&Drop of one or several TImages that are in a form, so that when droped over the form, a new TImagen is created in that position.


The example:

  • Make a form (Form1) and put a TImage (Image1) and a TLabel (Label1).
  • Load a bitmap in the Image1
  • In the var of the form, add this line:


     	var
     	  Form1: TForm1;
     	  Imagenes: TList; {Esta linea/This line}
    




    And we will make a variable global TList to the whole form.
    This TList will use it to go keeping pointers toward the images that we go creating.

  • Into OnDragOver event of Form1, we put this code:



     procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
       State: TDragState; var Accept: Boolean);
     begin
       Accept:=(Source is TImage);
     end;
    



    That the only thing that makes is to accept or not that we drop something over the form, in function that it is a TImage or not.

  • We put the DragMode property of Image1 to dmAutomatic
  • Into OnEndDragMode of the Image1, we put this code:


     procedure TForm1.Image1EndDrag(Sender, Target: TObject; X, Y: Integer);
     begin
         {Creamos un nuevo TImage}
         {Create a new TImage}
         Imagenes.Add(TImage.create(self));
         With TImage(Imagenes.Items[Imagenes.Count-1]) do
         begin
           {Llenamos las propiedades de la nueva TImage creada}
           {Fill the properties of the new created TImage}
           Parent:=self;
           Left:=X;
           Top:=Y;
           Height:=TImage(Sender).Height;
           Width:=TImage(Sender).Width;
           {Aqui copiamos el Picture del origen}
           Picture.Assign(TImage(Sender).Picture);
         end;
         {En Label 1 el Nš de imagenes que hemos creado}
         {In Label1 the number of created images}
         Label1.Caption:=IntToStr(Imagenes.Count);
     end;
    



    That make this:

  • It makes a new TImage, and the pointer adds it to the TList (Images), to have it kept and to be able to make things with the created images.
  • Once created the new TImage (by the way, you should check if it is free memory, etc, that I don't make it in the example. For example make it with a Try... except) we fill their properties, copying those that interest us of the Image1 that it is the one that we are dropping and that we index with TImage(Sender), it is worth this way you the same code to be assigned it to other TImages that you have in the screen.
  • And we put in Label1 the number of images that we have created.

    In any moment you could access anyone of the images that you have created.
    For example, to access to the property bitmap of the second image that you have created you should use something like that:


               TImage(Imagenes.Items[1]).Bitmap:=...
    



    (You keep in mind that the first Item is the zero, so the item[1] it is the second image)
    And Imagenes.Count is the number of images that you have in the List.