Solved

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

Posted on 2011-03-04
14
545 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

During the review/publication process for one of my previous articles, How to Embed Screenshots in Posts (http://www.experts-exchange.com/Other/Miscellaneous/A_11952-How-to-Embed-Screenshots-in-Posts.html), the Page Editor, lherrou (http://www.exper…
In a previously published article (http://www.experts-exchange.com/articles/10331/Automatic-Duplex-Scanning-in-PaperPort-Versions-11-12-14.html) here at Experts Exchange, I explained how to achieve duplex (double-sided) scanning in Nuance's PaperPor…
The goal of the tutorial is to teach the user how to make his/her own presets while editing so it is easier to edit there photos. Create a preset you like and copy that setting then save it in to your presets folder.
The goal of the tutorial is to teach the user the full work flow of how to use flash media encoder to stream onto YouTube.

705 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

21 Experts available now in Live!

Get 1:1 Help Now