Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-04
14
Medium Priority
?
639 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

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 article shows how to convert a multi-page PDF file into multiple image files, with one image file created for each page of the PDF. It does this by utilizing an excellent, free software package called GraphicsMagick. The solution is amazingly s…
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Suggested Courses

704 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