I am trying to drag and drop from VirtualTreeView to create a file in shell (drag and drop from VirtualTreeView to a folder in File Explorer or desktop folder).
I only found example of doing the opposite (shell to VirtualTreeView), but I cannot find any example for doing that. Help?
Doing any drag-drop operations in Windows involves creating an
IDataObject, and giving that object to Windows.The Virtual Treeview handles a lot of that grunt-work for you, creating an object that implements
IDataObjectfor you. The tree then raises events when you need to help populate it.When passing "file-like" things through a copy-paste or a drag-drop, you are require to add two clipboard formats to the
IDataObject:CF_FILEDESCRIPTOR, andCF_FILECONTENTSIn addition to support for formats that the virtualtree itself will add, you can choose to indicate support for more clipboard format.
OnGetUserClipboardFormats Event
This is the event where you are given a chance to add additional clipboard formats to the
IDataObjectthat the tree will be creating:The tree will then given the
IDataObjectto the shell as part of the drag-drop operation.Later, an application that the user dropped items onto will enumerate all formats in the
IDataObject, e.g.:CF_HTML("HTML Format")CFSTR_FILEDESCRIPTOR("FileGroupDescriptorW")CFSTR_FILECONTENTS("FileContents")CF_ENHMETAFILEAnd it will see that the
IDataObjectcontains FileDescriptor and FileContents.The receiving application will then ask the
IDataObjectto actually cough up data. (This "delayed-rendering" is a good thing, it means your source application doesn't actually have to read any content unless it actually gets requested).OnRenderOleData Event
This is the event where the virtual tree realizes its
IDataObjecthas been asked to render something, and it needs you to finally render that actual content.The general idea with these two clipboard formats is:
CF_FILEDESCRIPTORlets you return a record that describes the file-like thing (e.g. filename, file size, created date, last modified date, last accessed date)CF_FILECONTENTSlets you return anIStreamthat contains the actual file contentsThe first helper function creates an array of
FILE_DESCRIPTORobjects, and copies them to aHGLOBALallocated memory:The second helper copies your file-like thing's binary contents to an
IStream:Your attachment object, whatever it is has to know:
TFileDescriptorIStream