Solved

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

Posted on 2011-03-04
14
563 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

I. Introduction In a previous article (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_6537-PaperPort-Upgrade-How-to-download-and-install-updated-versions-of-PaperPort-11-and-12.html) (now deprecated), I discussed how to upgrad…
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…
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 how to use the cropping tool and to straighten there photos so it does not look tilted. You will choose which photo you want to edit, crop it to you liking and make it straight.

815 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

13 Experts available now in Live!

Get 1:1 Help Now