【发布时间】:2016-03-16 09:38:12
【问题描述】:
如何从 drop 操作中检索全局数据,就好像这些数据是文件一样? 据我所知(在 Internet 和 Jeff Procise's "Programming Windows with MFC (2nd ed.)" 中,COleDataObject::GetFileData 应该是答案,但我做错了什么,因为创建了 MFC CFile 对象但具有“无效句柄”值 (-1)。
我实际上是在尝试在我的应用程序的两个实例之间传输虚拟文件(那些实际上不存在于本地驱动器上的文件)。通过 HDROP 和 API DragQueryFile(也就是导出或导入物理文件时),我的应用程序和 Windows 资源管理器(或类似的)之间的传输工作正常。
这是启动拖动的代码(使用 CFSTR_FILEDESCRIPTOR 和 CFSTR_FILECONTENTS 格式传输本地磁盘上不存在的虚拟文件):
// - allocate and fill in the FileGroupDescriptor structure
HGLOBAL hg=::GlobalAlloc( GHND|GMEM_SHARE, ... );
LPFILEGROUPDESCRIPTOR pfgd=::GlobalLock(hg);
...
::GlobalUnlock(hg);
// - create an IDataObject instance and put the FileGroupDescriptor structure into it
// (it holds that cfDescriptor=::RegisterClipboardFormat(CFSTR_FILEDESCRIPTOR) )
CCustOleDataSource obj=... // COleDataSource-derived object
FORMATETC etcFileGroupDescriptor={ cfDescriptor, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
obj.CacheGlobalData( cfDescriptor, hg, &etcFileGroupDescriptor );
// - render the actual data upon request
// (it holds that cfContent=::RegisterClipboardFormat(CFSTR_FILECONTENTS) )
FORMATETC etcFileContents={ cfContent, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
obj.DelayRenderFileData( cfContent, &etcFileContents );
// - perform drag&drop
obj.DoDragDrop(DROPEFFECT_COPY|DROPEFFECT_MOVE);
这是接受 drop 的代码(如果通过 COleDataObject::GetGlobalData 检索数据可以正常工作,但在使用 COleDataObject::GetFileData 时不起作用):
// - get the FileGroupDescriptor structure
HGLOBAL hg=pDataObject->GetGlobalData(cfDescriptor);
LPFILEGROUPDESCRIPTOR pfgd=(LPFILEGROUPDESCRIPTOR)::GlobalLock(hg);
// - get a CFile abstraction of the actual data
FORMATETC etcFileContents={ cfContent, NULL, DVASPECT_CONTENT, 0, TYMED_ISTREAM }; // 0 = first dropped file
CFile *f=pDataObject->GetFileData( cfContent, &etcFileContents );
// Problem: "f" created but CFile::m_hFile equals INVALID_HANDLE_VALUE
// - read one sample byte
BYTE b;
f->Read(&b,1);
// - delete the file object
delete f;
还有一个关键问题:是否真的可以将 CFile 对象与延迟渲染结合使用? 不过,我可以通过全局内存指针访问延迟渲染的数据。
非常感谢任何帮助/建议,提前非常感谢:-)
托马斯
【问题讨论】:
标签: file winapi com mfc drag-and-drop