How to get IDropTarget to work with my Drop handler in Delphi?

I have associated the file extension with my Delphi 2009 program. I use the command line invocation method to pass the file name to my Delphi program so that it can be opened.

However, I found that when I select several files and click on them at the same time, it opens each file in a separate copy of my program. I asked about this , and apparently the solution is to use one of the two other Windows methods: DDE or IDropTarget .

But DDE is deprecated, and MSDN recommends the IDropTarget method. Lars Truyens also says in his answer to me that IDropTarget can fit better if I already have the drag and drop capabilities that I have.

This is currently my drop handler:

private procedure WMDropFiles(var WinMsg: TMessage); message wm_DropFiles; procedure TLogoAppForm.FormShow(Sender: TObject); begin DragAcceptFiles(Handle, true); end; procedure TLogoAppForm.WMDropFiles(var WinMsg: TMessage); // From Delphi 3 - User Interface Design, pg 170 const BufSize = 255; var TempStr : array[0..BufSize] of Char; NumDroppedFiles, I: integer; Filenames: TStringList; begin NumDroppedFiles := DragQueryFile(TWMDropFiles(WinMsg).Drop, $ffffffff, nil, 0); if NumDroppedFiles >= 1 then begin Filenames := TStringList.Create; for I := 0 to NumDroppedFiles - 1 do begin DragQueryFile(TWMDropFiles(WinMsg).Drop, I, TempStr, BufSize); Filenames.Add(TempStr); end; OpenFiles(Filenames, ''); Filenames.Free; end; DragFinish(TWMDropFiles(WinMsg).Drop); WinMsg.Result := 0; end; 

Now it takes one or more files and opens them as needed. This is very old code from the Delphi 3 book, but it still works.

What I cannot find is any documentation anywhere on how to implement IDropHandler in Delphi, and in particular, to make it work with the Drop handler (above) that I use.

Can someone tell me how to use IDropHandler to click on selected files with my file extension will pass them to my Drop handler and my program can open all the files that I clicked on?

+2
delphi drag-and-drop associations registry
source share
2 answers

This page provides an example of the implementation of IDropTarget in Delphi. Here is another Jedi code formatter. But this library could be even better. This, among other things, allows you to drag and drop from Windows Explorer and, therefore, already supports IDropTarget in the TDropHandler class.

+1
source share

Using Delphi wizards to add a standard COM server outside the process to your project, implement the IDropHandler interface (the article code is in C ++, but many of these concepts can be applied to Delphi code, and some of them are already implemented by VCL for you) and then override its virtual UpdateRegistry () method to add a couple of additional Registry Keys (see the bottom of this article) that your IDropHandler object needs to operate with double-click operations and the Windows Explorer pop-up menu.

Then change your application code to use RegisterDragDrop () instead of DragAcceptFiles () so that users can drag and drop n-drop files into your application window through an instance of your class based on IDropTarget.

Inside the IDropHandler method implementations, you can query the IDataObject interface to see if it can provide its data in HDROP format, and if so, you can pass it to the existing WM_DROPFILES message handler (which is old as well as obsolete, BTW, so you should consider removing it).

It sounds like a lot of work, but as soon as you actually get into it, it is not at all. I recently implemented IDropTarget support in one of my C ++ Builder VCL applications, and it didn't take a lot of time to work.

After you work with IDropTarget, there are many other Shell formats that you can support, if necessary, for example, when deleting data from sources without a file system.

+1
source share

All Articles