Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my form receive WM_DropFiles when files are dropped on it?

I am using Embarcadero RAD Studio XE to develop an application. I am trying catch the file(s) drag and drop to the application with the following code

TMainForm = class(TForm)
public:
  procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  DragAcceptFiles(Self.Handle, True);
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  DragAcceptFiles(Self.Handle, False);
end;

procedure TMainForm.WMDropFiles(var Msg: TWMDropFiles);
begin
  inherited;
  showmessage('catch here');
  // some code to handle the drop files here
  Msg.Result := 0;
end;

This code complied without problem. Also, when I drag and drop files, the cursor show that the status changed to drag and drop but after things dropped, nothing happen (no message shown too). Is that anything wrong with that?

like image 324
user1285419 Avatar asked Nov 20 '12 21:11

user1285419


2 Answers

in TForm.Create use two lines

ChangeWindowMessageFilter (WM_DROPFILES, MSGFLT_ADD);

ChangeWindowMessageFilter (WM_COPYGLOBALDATA, MSGFLT_ADD);

that's all

like image 62
MarcinH Avatar answered Oct 17 '22 20:10

MarcinH


In a plain vanilla application, the code in the question results in WMDropFiles executing when an object is dropped on the form. So, clearly there's something else happening to stop it working. The most obvious potential causes are:

  1. The main form's window handle is re-created after the initial call to DragAcceptFiles.
  2. Your process is running at a higher integrity level than the process that is dropping files on it. For example, are you running your process as administrator. Note that running the Delphi IDE as administrator would lead to your process running as administrator when started from the IDE.
  3. Something else in your process is interfering with drag/drop. Without knowing what's in your app, it's hard to guess what this could be. Start removing portions of your app until there's nothing left but the code in the question.

Option 2 seems quite plausible. To learn more see: Q: Why Doesn’t Drag-and-Drop work when my Application is Running Elevated? – A: Mandatory Integrity Control and UIPI

like image 36
David Heffernan Avatar answered Oct 17 '22 20:10

David Heffernan