Solved

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

Posted on 2011-03-04
14
605 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
PaperPort (http://www.nuance.com/for-individuals/by-product/paperport/index.htm) is among the most important applications that I run on my Windows computers. I use it every day, for nearly all of my document and photo scanning, as well as most of my…
Learn how to automatically add page numbers in your next InDesign project. This can be very helpful in multi-page books and magazines that you are designing. Make sure your Pages window visible.:  In the document you wish to add page numbers to. Act…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

624 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