Recent

Author Topic: [SOLVED] Files drag&drop is broken on Windows 10?  (Read 2327 times)

edgen

  • New member
  • *
  • Posts: 7
[SOLVED] Files drag&drop is broken on Windows 10?
« on: June 30, 2023, 08:10:03 pm »
Hello,

I need to allow user to drop files onto my form from Windows Explorer. The fastest way (set form's AllowDropFiles to true, and use OnDropFiles handler to process the dropped files) didn't work for me, the OnDropFiles just doesn't fire.

Then I wrote Windows-specific code to use OLE drag&drop, with implementation of IDropTarget and registering my form as drop target. And it also doesn't work from Explorer. But I've tried to drag files from FarManager (console-based file manager for Windows) and got succeed in it.

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   {TFileDropTarget}
  4.   TFileDropTarget = class(TInterfacedObject, IDropTarget)
  5.   private
  6.     FHandle: HWND;
  7.     FDropAllowed: Boolean;
  8.     procedure SetEffect(var dwEffect: LongWord);
  9.     function DragEnter(const dataObj: IDataObject; grfKeyState: LongWord; pt: TPoint; var dwEffect: LongWord): HResult; stdcall;
  10.     function DragOver(grfKeyState: LongWord; pt: TPoint; var dwEffect: LongWord): HResult; stdcall;
  11.     function DragLeave: HResult; stdcall;
  12.     function Drop(const dataObj: IDataObject; grfKeyState: LongWord; pt: TPoint; var dwEffect: LongWord): HResult; stdcall;
  13.   public
  14.     constructor Create(AHandle: HWND);
  15.     destructor Destroy; override;
  16.   end;
  17.  
  18. //...
  19. implementation
  20.  
  21. procedure TFileDropTarget.SetEffect(var dwEffect: LongWord);
  22. begin
  23.   dwEffect:=DROPEFFECT_COPY;
  24. end;
  25.  
  26. function TFileDropTarget.DragEnter(const dataObj: IDataObject; grfKeyState: LongWord; pt: TPoint; var dwEffect: LongWord): HResult;stdcall;
  27. begin
  28.   Result := S_OK;
  29.   try
  30.     FDropAllowed := true;
  31.     SetEffect(dwEffect);
  32.   except
  33.     Result := E_UNEXPECTED;
  34.   end;
  35. end;
  36.  
  37. function TFileDropTarget.DragLeave: HResult;stdcall;
  38. begin
  39.   Result := S_OK;
  40. end;
  41.  
  42. function TFileDropTarget.DragOver(grfKeyState: LongWord; pt: TPoint; var dwEffect: LongWord): HResult;stdcall;
  43. begin
  44.   Result := S_OK;
  45.   try
  46.     SetEffect(dwEffect);
  47.   except
  48.     Result := E_UNEXPECTED;
  49.   end;
  50. end;
  51.  
  52. function TFileDropTarget.Drop(const dataObj: IDataObject; grfKeyState: LongWord; pt: TPoint; var dwEffect: LongWord): HResult;stdcall;
  53. begin
  54.   Result := S_OK;
  55.   try
  56.     FDropAllowed := true;
  57.     if FDropAllowed then begin
  58.       MessageDlg('Drop called!', mtInformation, [mbOk], 0);
  59.     end;
  60.   except
  61.     Application.HandleException(Self);
  62.   end;
  63. end;
  64.  
  65. // ...
  66. // Form's OnShow
  67. procedure TfmDocsToLoad.FormShow(Sender: TObject);
  68. begin
  69.   FDropTarget := TFileDropTarget.Create(Handle) as IDropTarget;
  70. end;
  71.  
  72.  


So, the code seems to be ok (yes, it accepts any data, but I just want to see if the Drop() method is called actually). Moreover, the same code works fine with Windows Explorer on Windows 7. Then seems like the Windows version does matter, the problem appears on Windows 10 22H2 19045.3086. I've tried to build both 32 and 64 bits apps, the same effect. Used Lazarus version — current trunk, also tested on Lazarus 2.2.6 stable.

Did anyone faced such Windows behaviour? Could you please advise me how to make drag&drop working?

Best regards, Eugene.
« Last Edit: July 03, 2023, 05:32:33 pm by edgen »

loaded

  • Hero Member
  • *****
  • Posts: 855
Re: Files drag&drop is broken on Windows 10?
« Reply #1 on: June 30, 2023, 08:41:02 pm »
Hi, The link below will hopefully help solve your problem.

https://forum.lazarus.freepascal.org/index.php/topic,56890.msg422998.html#msg422998
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

edgen

  • New member
  • *
  • Posts: 7
Re: Files drag&drop is broken on Windows 10?
« Reply #2 on: June 30, 2023, 08:56:40 pm »
Hi, The link below will hopefully help solve your problem.

https://forum.lazarus.freepascal.org/index.php/topic,56890.msg422998.html#msg422998

hi, thank you,
I've compiled the program from that thread, but unfortunately it doesn't work too. That example uses the same OnDropFiles event, which doesn't fire.

Best regards, Eugene.

Josh

  • Hero Member
  • *****
  • Posts: 1350
Re: Files drag&drop is broken on Windows 10?
« Reply #3 on: June 30, 2023, 09:48:28 pm »
can u try

on the file your dragging, press and hold the leftmouse button down, press escape, then release mouse button.  Then try drag and drop
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

wp

  • Hero Member
  • *****
  • Posts: 12527
Re: Files drag&drop is broken on Windows 10?
« Reply #4 on: June 30, 2023, 10:27:05 pm »
The fastest way (set form's AllowDropFiles to true, and use OnDropFiles handler to process the dropped files) didn't work for me, the OnDropFiles just doesn't fire.
Cannot confirm this:
- Laz/main + FPC/3.2.2, Win 11
- Laz 2.2.6 + FPC 3.2.2, Win 11
- Laz 2.0.10 + FPC 3.2.0, Win 11
- Laz 2.0.10 + FPC 3.2.0 on Win 10

Maybe you have some buggy explorer add-on in your system, or some aggressive anti-virus scanner?

nanobit

  • Full Member
  • ***
  • Posts: 165
Re: Files drag&drop is broken on Windows 10?
« Reply #5 on: July 01, 2023, 07:19:51 am »
Used Lazarus version — current trunk, also tested on Lazarus 2.2.6 stable.

Even if the same eventhandler works on most systems, a LCL bug might be hidden and is visible only on specific (your) systems. Consider testing an older Lazarus version (2.0.x or older) on the same system.

edgen

  • New member
  • *
  • Posts: 7
Re: Files drag&drop is broken on Windows 10?
« Reply #6 on: July 03, 2023, 05:31:57 pm »
Thank you everyone! The case is solved, the source of the problem was inside one of Explorer helpers.
After switching that helper off I've tested the code shown above on all listed FPC and Lazarus versions, it works fine.

Best regards, Eugene.

 

TinyPortal © 2005-2018