home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 June / Chip_1999-06_cd.bin / zkuste / Delphi / jak / drafile / aaa next >
Text File  |  1999-04-13  |  4KB  |  93 lines

  1. The Unofficial Newsletter of Delphi Users - by Robert Vivrette 
  2.  
  3. --------------------------------------------------------------------------------
  4.  
  5.     
  6. Drop File Helper Components 
  7.  
  8. by Grahame Marsh - gsmarsh@aol.com 
  9.  
  10. These simple components were written to provide an easy way for a Delphi form or individual control to receive files dropped on to it.  An another component is used to retrieve files dropped onto the application's icon, from say Explorer, triggering the same events as for a form or control. 
  11.  
  12. The components encapsulate a number of Win32 API calls (found in ShellAPI.pas) : 
  13.  
  14. type 
  15.   HDROP = Longint; 
  16. function DragQueryFile(Drop: HDROP; FileIndex: UINT; FileName: PChar; cb: INT): INT; stdcall; 
  17. function DragQueryPoint(Drop: HDROP; var Point: TPoint): BOOL; stdcall; 
  18. procedure DragFinish(Drop: HDROP); stdcall; 
  19. procedure DragAcceptFiles(Wnd: HWND; Accept: BOOL); stdcall;
  20. You use the DragAcceptFiles call first to inform windows that the control will accept dropped files.  When you are dragging a file over this control the cursor changes to show that the control can accept the drop.  DragQueryFile is used to retrieve the filename or names, and the count of files available.  DragQueryPoint returns to coordinates of the drop point.  And finally, when you are finished retrieving information you call DragFinish. 
  21. The component interface looks like this: 
  22.  
  23. type 
  24.   TDnDStartEvent = procedure (Sender: TObject; Count, X, Y: integer) of object; 
  25.   TDnDItemEvent = procedure (Sender: TObject; const Item: string) of object; 
  26.   TCustomDnD = class (TComponent) 
  27.   private 
  28.     FParent: THandle; 
  29.     FActive: boolean; 
  30.     FOnDnDStart: TDnDStartEvent; 
  31.     FOnDnDItem: TDnDItemEvent; 
  32.     FOnDnDFinish: TNotifyEvent; 
  33.     procedure SetActive (Value : boolean); 
  34.   public 
  35.     property Active: boolean read FActive write SetActive; 
  36.     property OnDnDStart: TDnDStartEvent read FOnDnDStart write FOnDnDStart; 
  37.     property OnDnDItem: TDnDItemEvent read FOnDnDItem write FOnDnDItem; 
  38.     property OnDnDFinish: TNotifyEvent read FOnDnDFinish write FOnDnDFinish; 
  39.   end;
  40.  
  41. This custom interface is never used directly, it simply contains the three events common to all of the functional drag and drop components.  When files are dropped the events fire in turn - OnDnDStart, then OnDnDItem for each filename and then OnDnDFinish when all file events have been triggered.  The Active property can be used to turn on and off the drop capabilities. 
  42. The interface of the usable components are: 
  43.  
  44. type 
  45.   TDnDRun = class (TCustomDnD) 
  46.   protected 
  47.     procedure Loaded; override; 
  48.   public 
  49.     procedure Execute; 
  50.   published 
  51.     property Active; 
  52.     property OnDnDStart; 
  53.     property OnDnDItem; 
  54.     property OnDnDFinish; 
  55.   end; 
  56.   TDnDForm = class(TCustomDnD) 
  57.   private 
  58.     FOldDnDefWndProc, 
  59.     FNewDefWndProc: pointer; 
  60.     procedure NewDefWndProc (var Msg: TMessage); 
  61.   protected 
  62.     procedure Loaded; override; 
  63.   public 
  64.     constructor Create (AOwner: TComponent); override; 
  65.     destructor Destroy; override; 
  66.   published 
  67.     property Active; 
  68.     property OnDnDStart; 
  69.     property OnDnDItem; 
  70.     property OnDnDFinish; 
  71.   end; 
  72.  
  73.   TDnDControl = class(TDnDForm) 
  74.   private 
  75.     FControl: TWinControl; 
  76.     procedure SetControl (Value: TWinControl); 
  77.   protected 
  78.     procedure Loaded; override; 
  79.     procedure Notification(AComponent: TComponent; Operation: TOperation); override; 
  80.   published 
  81.     property Control: TWinControl read FControl write SetControl; 
  82.   end;
  83.  
  84. In each case, the Create and Loaded methods make the necessary API calls so that the control will receive dropped files. 
  85. The TDnDRun component is a bit of a cheat in that it only uses ParamCount and ParamStr to return items found on the command line.  But it uses the same events interface so I would hope some common code can be used for handling these events. 
  86.  
  87. One final comment is that since the API call to DragAcceptFiles requires a windows handle, then non-windowed visual controls cannot be used to accept dropped files. 
  88.  
  89. The zip file contains the complete source, dcr file and a simple demo. Click here to download it. 
  90.  
  91. Enjoy! 
  92.   
  93.