Recent

Author Topic: Drag and Drop an Image on a form?  (Read 4435 times)

bill0287

  • Full Member
  • ***
  • Posts: 146
Drag and Drop an Image on a form?
« on: May 04, 2016, 03:19:35 am »
I want to be able to drag a image across the form and drop it, like you might do in a solitaire game. I am using this wiki as guidance but not having any luck:

http://wiki.freepascal.org/Drag_and_Drop_sample

1. I set the Dragmode on the image and the form to dmAutomatic
2. I set the image OnMouseDown even to start the drag

When I click on the image nothing happens so I must be missing something.

Here is the test code:

Code: Pascal  [Select][+][-]
  1. unit main;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Image1: TImage;
  16.     procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
  17.       State: TDragState; var Accept: Boolean);
  18.     procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
  19.       Shift: TShiftState; X, Y: Integer);
  20.   private
  21.     { private declarations }
  22.   public
  23.     { public declarations }
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  36.   Shift: TShiftState; X, Y: Integer);
  37. begin
  38.     if Button = mbLeft then   {check if left mouse button was pressed}
  39.       Image1.BeginDrag(true);  {starting the drag operation}
  40. end;
  41.  
  42. procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
  43.   State: TDragState; var Accept: Boolean);
  44. begin
  45.   Accept := true;
  46. end;
  47.  
  48. end.
  49.  

balazsszekely

  • Guest
Re: Drag and Drop an Image on a form?
« Reply #1 on: May 04, 2016, 06:28:47 am »
Try this:
Code: Pascal  [Select][+][-]
  1.  TForm1 = class(TForm)
  2.  //...
  3.   private
  4.     dX: Integer;
  5.     dY: Integer;
  6.     { private declarations }
  7.   public
  8.     { public declarations }
  9.   end;    
  10.  
  11. //...
  12.  
  13. [code=pascal]
  14. procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  15.   Shift: TShiftState; X, Y: Integer);
  16. begin
  17.   if Button = mbLeft then
  18.   begin
  19.     dX := X;
  20.     dY := Y;
  21.     Image1.BeginDrag(true);
  22.   end;
  23. end;
  24.  
  25. procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
  26.   State: TDragState; var Accept: Boolean);
  27. begin
  28.   Accept := True;
  29. end;
  30.  
  31. procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
  32. begin
  33.   Image1.Left := X - dX;
  34.   Image1.Top := Y - dY;
  35. end;    
  36.  

bill0287

  • Full Member
  • ***
  • Posts: 146
Re: Drag and Drop an Image on a form?
« Reply #2 on: May 04, 2016, 06:16:13 pm »
That definitely works, thanks.

 

TinyPortal © 2005-2018