Recent

Author Topic: (SOLVED) Detect kind of file using DragAndDrop  (Read 2285 times)

AsleyCruz

  • Full Member
  • ***
  • Posts: 108
    • Graphic and web designer
(SOLVED) Detect kind of file using DragAndDrop
« on: March 27, 2020, 03:10:52 am »
Hi guys,
My would like my application accept txt files only on drag/drop files on the form.

If the file that is being draging is .txt, I would like to use the cursor "accept-file"
if not, use the cursor "forbidden-file". See attached image.

How can I detect the kind of file?
« Last Edit: March 27, 2020, 06:10:47 pm by AsleyCruz »
Graphic & web designer

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Detect kind of file using DragAndDrop
« Reply #1 on: March 27, 2020, 07:22:32 am »
The OnDropFiles handler receives a list of the files dropped so all you need to do is to check the extensions or, probably better, whether they are in fact text files:

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.FormDropFiles(Sender: TObject;
  2.   const FileNames: array of String);
  3. var
  4.   AFileName: String;
  5. begin
  6.   for AFileName in FileNames do
  7.     if Lowercase(ExtractFileExtension(AFileName)) = '.txt' then begin
  8.       {Do whatever}
  9.     end;
  10.     {Alternatively, using LazFileUtils:
  11.     if FileIsText(AFileName) then begin
  12.       // Do whatever
  13.     end;
  14.     }
  15. end;
« Last Edit: March 27, 2020, 07:25:37 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ASerge

  • Hero Member
  • *****
  • Posts: 2336
Re: (SOLVED) Detect kind of file using DragAndDrop
« Reply #2 on: March 28, 2020, 07:52:20 am »
In Windows, OnDropFiles is triggered only when files are already dropped as a result of processing the WM_DROPFILES message. From MSDN "Avoid using Windows 3.1 targets or the WM_DROPFILES message, if possible".
In your case, it is also better to explicitly use the IDataObject interface to respond dragging files over the form. For the example of how exactly, I have attached a small unit. And an example of its use is as follows:
Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.     Memo1: TMemo;
  4.     procedure FormCreate(Sender: TObject);
  5.     procedure FormDestroy(Sender: TObject);
  6.   private
  7.     function AcceptFiles(Files: TStrings): Boolean;
  8.     procedure FilesWasDropped(Files: TStrings);
  9.   end;
  10.  
  11. var
  12.   Form1: TForm1;
  13.  
  14. implementation
  15.  
  16. uses uDropTarget;
  17.  
  18. {$R *.lfm}
  19.  
  20. function TForm1.AcceptFiles(Files: TStrings): Boolean;
  21. var
  22.   FileName: string;
  23. begin
  24.   Result := False;
  25.   for FileName in Files do
  26.     Result := Result or SameFileName(ExtractFileExt(FileName), '.txt');
  27. end;
  28.  
  29. procedure TForm1.FilesWasDropped(Files: TStrings);
  30. begin
  31.   Memo1.Text := Files.Text;
  32. end;
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. begin
  36.   RegisterDropTarget(Handle, @AcceptFiles, @FilesWasDropped);
  37. end;
  38.  
  39. procedure TForm1.FormDestroy(Sender: TObject);
  40. begin
  41.   UnregisterDropTarget(Handle);
  42. end;

jamie

  • Hero Member
  • *****
  • Posts: 6734
Re: (SOLVED) Detect kind of file using DragAndDrop
« Reply #3 on: March 28, 2020, 01:13:33 pm »
Thank you very much for dropping your example here.

I have in the past implemented the IdragDrop handler but found that it has some issues of its own, mainly resource issues..

  I will look at your example to see if there is anything you are doing differently..

 :D
The only true wisdom is knowing you know nothing

AsleyCruz

  • Full Member
  • ***
  • Posts: 108
    • Graphic and web designer
Re: (SOLVED) Detect kind of file using DragAndDrop
« Reply #4 on: March 28, 2020, 04:33:11 pm »
In Windows, OnDropFiles is triggered only when files are already dropped as a result of processing the WM_DROPFILES message. From MSDN "Avoid using Windows 3.1 targets or the WM_DROPFILES message, if possible".
In your case, it is also better to explicitly use the IDataObject interface to respond dragging files over the form. For the example of how exactly, I have attached a small unit. And an example of its use is as follows:
Code: Pascal  [Select][+][-]
  1.  

Thanks ASerge, works like a charm.
My application only accepts .ico and .exe files. You can see how it works in the gif attached.

Thanks again.
Graphic & web designer

 

TinyPortal © 2005-2018