Solved

how to drap and drop a visual component on a form in delphi ?

Posted on 2011-03-04
14
555 Views
Last Modified: 2012-06-21
I have a form in Delphi with a couple of TImage components
I want the user to be able to drag such TImage and drop it on, for example another one
I just put DragMode to dmAutomatic to see what happens, but when I run and try to drag it, the cursor become a crossed O and the object does not move
What shell I activate ?
0
Comment
Question by:LeTay
  • 8
  • 6
14 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35036606
Do you mean to drag the TImage all over the form?
If yes, then this is what you are looking for:
http://delphiforfun.org/programs/Delphi_Techniques/DragImageDemo.htm
0
 

Author Comment

by:LeTay
ID: 35036782
Well, this looks to be a very sophisticated demo !
I just want to have a very simple sample : a TForm with a TImage and just the "code" or setting to be able to drag the TImage and drop it, for example on a small TPanel
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35036831
Just in case if you want a simpler one, set the Form DragDrop & DragOver as follows:

Set the TImage Property "DragMode" to "dmAutomatic" and set a suitable cursor for the TImage "DragCursor".
procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  if Source is TImage then
    begin
      TImage(Source).Left := X;
      TImage(Source).Top := Y;
    end;
end;

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

Open in new window

Other properties (Form & TImage) kept as the default.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35037093
To DragDrop onto the Form and TPanel:
procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  if Source is TImage then
    begin
      TImage(Source).Left := X;
      TImage(Source).Top := Y;
    end;
end;

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

procedure TForm1.Panel1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  if Source is TImage then
    begin
      TImage(Source).Parent := Panel1;
      Accept := True;
    end;
end;

procedure TForm1.Panel1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  if Source is TImage then
    begin
      TImage(Source).Left := X;
      TImage(Source).Top := Y;
    end;
end;

Open in new window

0
 

Author Comment

by:LeTay
ID: 35037117
I just tried your sample but when I try to drag the TImage, the cursor becomes a crossed O and nothing moves...
I change the TImage properties as you said ...
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35037213
Probably some properties. Would you try the attached project please.
DragDrop.zip
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35037413
I saw another approach, if you consider, which is using MouseDown and MouseMove to have the VCL moving around the Form:
http://delphi.about.com/library/weekly/aa102505a.htm
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:LeTay
ID: 35037472
Of course your attachment works
I can't figure out which properties is wrongly set
I completed the 2 drag events on the form (no TPanel for the moment ..) with your code
For the TImage, I set DragCursor crHandPoint, DragKind dkDrag and DragMode dmAutomatic
Do I forget something ?
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35037614
Can you attach it please?
0
 

Author Comment

by:LeTay
ID: 35037690
Not the entire project (almost 70 units !)
I will just put the Form and its unit
There are 52 TImage (play cards). I only test currently the first one ... (Image1)
The dfm is quiet large due to the 52 GIF images
U45.dfm
U45.pas
0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 35037820
Your Image must be directly on the Form to work. If you have anything in between or as a background, such as another Image, then you need to define the DragDrop and DragOver of that background so the movement will be recognized. Check the attached project I have added a blank Image as a background and set the events of it to FormDragDrop and FormDragOver so the Drag of the Target image is possible if you try to remove those events then you get the crossed O.
DragDrop-2.zip
0
 

Author Comment

by:LeTay
ID: 35037895
Sorry jimyX !
I understand my problem : the TImage is itself on a TPanel so I have to adapt the TPanel events !
Many thanks for you help
0
 

Author Closing Comment

by:LeTay
ID: 35037929
Many thanks
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35038016
Thanks 2 u 2 :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 63
Intraweb download file link ? 1 110
Delphi Form ownership 4 73
Christmas family picture with greetings 19 84
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This Micro Tutorial will teach you how to create simple effects and smooth transitions in your films. This will be demonstrated using Adobe Premiere Pro CS6.
The goal of the tutorial is to teach the user what exposure is and how to use the exposure slider. Analyze the photo that you want to edit, then adjust the exposure slider to your liking.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now