Avatar of hidrau
hidrau
Flag for Brazil asked on

Help me with drag and drop

Hello Guys

I need a help.

I am trying to do a small game with pictures, but I am having trouble with drag and drop function.

I put an small example in image what I want to do.  As you can see, I have three panels.
The first one is the panel where is all my images, and the panel 2, is the place where I need
to drop the images. The 3 panel is only the example as my second panel must be after dropping the image.

My doubts are:

1) Drag the image from Panel 1 to the  panel2.

2) The panel 2 will only accept three images, for example: pict5, pict1 and pict3.

3) When I drop the picture in the panel2, the figure must be align in the center of the panel, vertical and horizontal center.

4) when dropped more than 1 picture, they must be spaced and centered as you can see the panel3 as example.


My example was made in Delphi XE4

Thanks
Alexandre
IMG1.jpg
Unit1.dfm
Unit1.pas
Project1.dpr
Delphi

Avatar of undefined
Last Comment
hidrau

8/22/2022 - Mon
Marco Gasi

This is the code I use to implement drag and drop in your project (let me some time to play with alignement):

Unit1.pasUnit1.dfmProject1.dpr
Marco Gasi

Mmmmhh, ordering images is difficult: how has the behavior to be? Do you want the user can choose the place of each image whan he drops it on the panel? Or you want the position is random?
Marco Gasi

Okay, see if this is a good solution for you: it has not that automatically reallignement I wa looking for, but it works.

DragDropImages.zip
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
hidrau

ASKER
Hello MarquG,

Thanks very very much for helping me.

I'm gonna try it now and let you know about it.
hidrau

ASKER
MarqusG, you are right, the problem is being now the reallignement.

I changed the panel to receive 5 figures, and according to dropping the figures on it, the figures must reallign on the panel.

Change the recival value to 5 and drop 5 figures on it, you wil see them,

I am trying to do it, but I don't have many idea how to do it. Take a look at it and tell me if you can help me to realign the figures.
Marco Gasi

Please, tell me about the principle of the realignement: does the first slide to the left?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
hidrau

ASKER
Hello MarqusG,

I am trying but I haven't got it yet :)

when I drop the first figure, it must align to the center and so on with another.

Well, I inserted a code of mine and I am almost reach the result.

Take a look at:

procedure TForm1.Panel1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
 a, b, c : Integer;

begin
  if (Source is TImage) and (Panel1.ControlCount < 5) then
  begin
    TImage(Source).Parent := Panel1;
    TImage(Source).Left := 115 + ((TImage(Source).Width + 15) * (Panel1.ControlCount - 1));
  end;

  a := (sender as TPanel).ControlCount;
  a := a * 60;
  b := ((sender as Tpanel).Width - a) div ((sender as TPanel).ControlCount+1);

  For c := 0 to (sender as TPanel).ControlCount-1 do
  Begin
    if ((sender as TPanel).Controls[c] is TImage)  then
    Begin
      ((sender as TPanel).Controls[c] as TImage).Left := b;
      b := b + 60 + 15;
    End;
  End;


end;

Open in new window


Maybe you can have another idea on it or another way to do it. Let me know
Marco Gasi

Wow, well done, I'll try to play me too. But I changed (sender as TPanel) to Panel1, which is the one where the images have to be aligned.
Marco Gasi

I think I got it. Add this procedure:

procedure TForm1.AlignImages(Container: TObject);
var
  a, b, c, Factor: Integer;
begin
  case Panel1.ControlCount of
  1: Factor := Panel1.ControlCount+1;
  2: Factor := Panel1.ControlCount;
  3: Factor := Panel1.ControlCount -1;
  4: Factor := Panel1.ControlCount -1;
  5: Factor := Panel1.ControlCount -1;
  end;
  a := Panel1.ControlCount;
  a := a * 60;
  b := (Panel1.Width - a) div Factor;

  For c := 0 to Panel1.ControlCount-1 do
  Begin
    if (Panel1.Controls[c] is TImage)  then
    Begin
      (Panel1.Controls[c] as TImage).Left := b;
      b := b + 60 + 15;
    End;
  End;
end;

Open in new window


and call it here:

procedure TForm1.Panel1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  if (Source is TImage) and (Panel1.ControlCount < 5) then
    TImage(Source).Parent := Panel1;
  AlignImages(Panel1);
end;

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
hidrau

ASKER
MarqusG,

Yeah, I also made a small change.

I changed the function in a more generic function, where it can work with any container.

Take a look at it:

procedure AlinhaImages(Container: TObject; spaceBetween: Integer);
var
  a, b, c, Factor, OBJwidth: Integer;
begin
  case (Container as TPanel).ControlCount of
    1: Factor := (Container as TPanel).ControlCount+1;
    2: Factor := (Container as TPanel).ControlCount;
    3: Factor := (Container as TPanel).ControlCount -1;
    4: Factor := (Container as TPanel).ControlCount -1;
    5: Factor := (Container as TPanel).ControlCount -1;
  end;

  OBJwidth := ((Container as TPanel).Controls[0] as TImage).Width;

  a := (Container as TPanel).ControlCount;
  a := a * OBJwidth;
  b := ((Container as TPanel).Width - a) div Factor;

  For c := 0 to (Container as TPanel).ControlCount-1 do
  Begin
    if ((Container as TPanel).Controls[c] is TImage)  then
    Begin
      ((Container as TPanel).Controls[c] as TImage).Left := b;
      b := b + OBJwidth + spaceBetween;
    End;
  End;
end;

Open in new window



Well, it is almost there, the align it is not perfect yet and you can notice that the left side is smaller than right side. What do you think it is missing to the align be perfect?
Marco Gasi

Lol, I also had made some change: i pass the spaceBetween and in addition the width of the control itself...

Anyway, I'll go to play ;)
hidrau

ASKER
:) We are reach over there lol;

MarqusG, just curiosity, where are you from?

I am from Brazil - São Paulo
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Marco Gasi

I'm from Italy, a small, small town near Monza (do you like Formula 1?) But i'm going to move me and my family to Tenerife, Canary Islands.
hidrau

ASKER
I just asked because we change idea with many people and we don't have any idea where the people are from. I have heard about Monza, one day I hope to know Italy and other parts of the world.

Thanks marqusG for helping me with this. :)

do you have skype? if so and if you want, mine is alexhaifa
Marco Gasi

Sorry, no skype yet - and my english is better if written than if spoken :)
Your help has saved me hundreds of hours of internet surfing.
fblack61
hidrau

ASKER
MarqusG, did you get a better/perfect align? I am trying but nothing yet here.
Marco Gasi

I'm trying too but no good news. There is a progressive sliding to the left and I can't get rid off of it.

These are the values that Image.Left should have accordingly to the number of images:

1:  195
2:  155 - 235
3:  118 - 198 - 273
4:  78 - 158 - 238 - 313
5:  40 - 120 - 200 - 275 - 350

And these are the real value I get with code:

a Panel1.ControlCount = 1
a Panel1.ControlCount * Width = 60
b Panel1.Width - a) div Factor = 450 - 60 div 2 = 195
Left of Image 0 = 195
a Panel1.ControlCount = 2
a Panel1.ControlCount * Width = 120
b Panel1.Width - a) div Factor = 450 - 120 div 2 = 165
Left of Image 0 = 165
Left of Image 1 = 240
a Panel1.ControlCount = 3
a Panel1.ControlCount * Width = 180
b Panel1.Width - a) div Factor = 450 - 180 div 2 = 135
Left of Image 0 = 135
Left of Image 1 = 210
Left of Image 2 = 285
a Panel1.ControlCount = 4
a Panel1.ControlCount * Width = 240
b Panel1.Width - a) div Factor = 450 - 240 div 3 = 70
Left of Image 0 = 70
Left of Image 1 = 145
Left of Image 2 = 220
Left of Image 3 = 295
a Panel1.ControlCount = 5
a Panel1.ControlCount * Width = 300
b Panel1.Width - a) div Factor = 450 - 300 div 4 = 37
Left of Image 0 = 37
Left of Image 1 = 112
Left of Image 2 = 187
Left of Image 3 = 262
Left of Image 4 = 337

Do you wish get crazy you too? ;)
Marco Gasi

I have to stop a bit. I'll cmoe back later. Bye
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
hidrau

ASKER
Yeah, it is really hard.

Of course, it must have a way to do that. I will also stop a little bit and return later.
ASKER CERTIFIED SOLUTION
Marco Gasi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
hidrau

ASKER
wow, you are right, I can't believe.

Thanks very very much for your help. It helped me a lot and it got perfect.

You are very good at Delphi. I am not as good as you, I am just started with some kinds different from my kind of proclamation.
Marco Gasi

What you mean saying "I am just started with some kinds different from my kind of proclamation"?

This apart, thank you for nice words, but you'll find a lot of guys mutch mucth better than me here at EE :)

Cheers
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
hidrau

ASKER
Those words mean:

I work with Delphi only for company system with database, reports, grids, form for to insert (name, address, phone, etc)

This kind of project is the first that I will have to use some function and knowledge very different that I used to do everyday. :)

Now, that everything is ok, I have a second and third part that I will start. I am gonna open another thread for the second part.
hidrau

ASKER
thanks very very much!!!
hidrau

ASKER
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.