How to obtain files/content on DragEnter

Support forum for our inter-application drag-n-drop components.

How to obtain files/content on DragEnter

Postby busack » Mon Aug 03, 2015 1:26 am

Hi,

I would like to obtain the dragged files/content in the "DragEnter" event.
However, the property "DroppedLines" is empty (like "DroppedTextFormat"). As far as I know these properties are set on Drop only.

So how to get the dragged files/content during "DragEnter"?

Thanks in advance :)

Steven
busack
 
Posts: 3
Joined:
Tue Jul 28, 2015 12:50 am

Re: How to obtain files/content on DragEnter

Postby DropMaster » Mon Aug 03, 2015 10:09 am

Sure. Something like the code below. If you drag some text onto the form, it'll populate Label1 with the content of the drag, and clear it when you drag off the form again.

In other words, look at the DataObject* methods on TDMTextTarget.

Hope this helps.

Code: Select all
procedure TForm1.DMTextTarget1DragEnter(Sender: TObject; Effect: TDropEffect; X,
 Y: Integer);
var
  aFormatName: String;
  theText: String;
begin
  aFormatName := IntToStr(CF_TEXT);  // Name of the format you're looking for
  if DMTextTarget1.DataObjectHasFormat(aFormatName) then
     begin
        theText := DMTextTarget1.DataObjectGetFormat(aFormatName);
        Label1.Caption := theText;
     end;

end;

procedure TForm1.DMTextTarget1DragLeave(Sender: TObject);
begin
  Label1.Caption := '';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DMTextTarget1.AcceptorControl := Self;
  DMTextTarget1.AcceptText := true;
end;
_________________________
DropMaster Support
DropMaster
 
Posts: 24
Joined:
Fri Apr 15, 2011 6:32 am

Re: How to obtain files/content on DragEnter

Postby busack » Tue Aug 04, 2015 3:47 am

Hi,

Thanks for the reply.

This is working for text.
However, how to get the file list in case of a file drag from Windows Explorer or MS Outlook?

I tried it with "FileGroupDescriptor" and "FileContents" but they are not included.
I also tried the format "FileName" (which is included) but it returns the first file path only with a '#0' at the end.
busack
 
Posts: 3
Joined:
Tue Jul 28, 2015 12:50 am

Re: How to obtain files/content on DragEnter

Postby DropMaster » Tue Aug 04, 2015 6:42 am

Best approach is to read the code in DMComps.pas and you'll see how it's done by the components themselves.

A couple of other pointers, though.

a) Drag onto the demo app FormatViewer so you can see what formats are actually present. You'll see the formats, the media types and the indices (see below). Then you'll know what to look for when you're writing your code.

b) Sometimes the formats are present not on a global handle but on an IStream or an IStorage. By default, it's assumed that the media type is a TYMED_HGLOBAL (a global handle). To grab a format from e.g. an IStream, you first set the global variable DataObjectTymed := TYMED_ISTREAM and then make your call to DataObjectHasFormat or DataObjectGetFormat; after each such call, DataObjectTymed is reset to TYMED_ISTREAM automatically. For an indexed format, like a list of files, there is DataObjectlIndex which you can set to the (zero-based) element of the format you want to receive. This is all Windows-level stuff and is normally hidden away from you by DM. There are examples of all of this kind of manipulation in the demo applications.

c) Based on the above you may need to supply a different TYMED. Also, it's possible you need to look for FileGroupDescriptorW but that will be clear from step a). If you make the normal call to DataObjectGetFormat and you get nothing back, it means that format is not present on a global handle, or if it's an indexed format, there's nothing at index zero.

d) When you have your format data returned (on an AnsiString) there are helper methods in DMUtil you can use to extract the data, like FileListFromFileGroupDescripor (which returns the list of files from a FileGroupDescriptor into a TStringList), etc. Again there are examples of this kind of usage in the demo applications.
_________________________
DropMaster Support
DropMaster
 
Posts: 24
Joined:
Fri Apr 15, 2011 6:32 am

Re: How to obtain files/content on DragEnter

Postby busack » Tue Aug 04, 2015 9:48 am

Hi again,

Thanks for the detailed reply. I will take a look into it.

Are there any plans to add this issue as feature? So that the file list is available like in "OnDrop":

Code: Select all
if (DroppedTextFormat = dtfFiles) then begin
   for i := 0 to DroppedLines.Count - 1 do begin
      MyFileList.Add(DroppedLines[i]);
   end;
end;


Best regards
Steven
busack
 
Posts: 3
Joined:
Tue Jul 28, 2015 12:50 am

Re: How to obtain files/content on DragEnter

Postby DropMaster » Tue Aug 04, 2015 9:57 am

Probably not.

In fact, much content for drag-and-drop is delay-rendered (so a data provider - the source side - can advertise its presence, but only has to create and provide it when a drop actually happens). If it were accessed on every OnDragEnter that would defeat the purpose of delay-rendering.

And the DroppedLines etc. properties that are filled in during OnDrop are really only helpful for text and lists of files anyway. For complex content from an external application, you'd probably have to use DataObjectGetFormat anyway in OnDrop (again, see the examples), so the benefit would be minimal.

I suppose it could be added based on whether a flag was set, but that complicates the workflow. We'll add it to the list of things to consider for future releases, but it seems low-priority to me.

Hope this helps.
_________________________
DropMaster Support
DropMaster
 
Posts: 24
Joined:
Fri Apr 15, 2011 6:32 am


Return to DropMaster

Who is online

Users browsing this forum: No registered users and 1 guest

cron