How to enable file drag and drop for specific controls in Delphi

I would like to accept files as soon as someone throws the file into a specific control (e.g. TMemo). I started with this example: http://delphi.about.com/od/windowsshellapi/a/accept-filedrop.htm and changed it like this:

procedure TForm1.FormCreate(Sender: TObject); begin DragAcceptFiles( Memo1.Handle, True ) ; end; 

This allows the control to display a drag and drop icon, but the correct WM_DROPFILES message WM_DROPFILES not called because DragAcceptFiles requires a window handle (Parent?). I can define MemoHandle in the WMDROPFILES procedure, but I don’t know how the drag cursor is for all controls. How to enable drag and drop for a specific control (and block other controls from dragging)?

+4
source share
1 answer

You really need to pass the window handle to the memo control, but then you also need to listen to the WM_DROPFILES message sent to it:

 unit Unit5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ShellAPI; type TMemo = class(StdCtrls.TMemo) protected procedure WMDropFiles(var Message: TWMDropFiles); message WM_DROPFILES; procedure CreateWnd; override; procedure DestroyWnd; override; end; TForm5 = class(TForm) Memo1: TMemo; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form5: TForm5; implementation {$R *.dfm} procedure TForm5.FormCreate(Sender: TObject); begin end; { TMemo } procedure TMemo.CreateWnd; begin inherited; DragAcceptFiles(Handle, true); end; procedure TMemo.DestroyWnd; begin DragAcceptFiles(Handle, false); inherited; end; procedure TMemo.WMDropFiles(var Message: TWMDropFiles); var c: integer; fn: array[0..MAX_PATH-1] of char; begin c := DragQueryFile(Message.Drop, $FFFFFFFF, fn, MAX_PATH); if c <> 1 then begin MessageBox(Handle, 'Too many files.', 'Drag and drop error', MB_ICONERROR); Exit; end; if DragQueryFile(Message.Drop, 0, fn, MAX_PATH) = 0 then Exit; Text := fn; end; end. 

In the above example, only deleting one file is allowed. The file name will be placed in the recording control. But you can also allow deletion of several objects:

var c: integer; fn: array [0..MAX_PATH-1] from char; i: integer; to begin

 c := DragQueryFile(Message.Drop, $FFFFFFFF, fn, MAX_PATH); Clear; for i := 0 to c - 1 do begin if DragQueryFile(Message.Drop, i, fn, MAX_PATH) = 0 then Exit; Lines.Add(fn); end; 
+6
source

Source: https://habr.com/ru/post/1481751/


All Articles