我通过从 COleDropTarget
派生类 CDropTarget
使我的 MFC 应用程序成为放置目标并覆盖 all necessary functions .一切都按预期工作。但是OnDrop()的返回值让我困惑。它的描述是这样的:
Nonzero if the drop is successful; otherwise 0.
如果我的申请中有多个文件,我不明白“成功”是什么意思。 例如,考虑以下实现:
BOOL CDropTarget::OnDrop(CWnd* pWnd, COleDataObject* pDataObj, DROPEFFECT tDropEffect, CPoint tPoint)
{
// I left out declaration/definition of hDrop and path for reasons of clarity.
[...]
UINT numHandledFiles = 0;
// Determine the number of dropped files.
UINT numDroppedFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
// Iterate over all dropped files.
for (UINT n = 0; n < numDroppedFiles; n++)
{
// Get the path of the current file from the HDROP structure.
if (DragQueryFile(hDrop, n, path, PATH_MAX) > 0)
{
// Try to handle each dropped file in my function handleFile().
// It returns true if a file could be handled and false otherwise.
// (The latter happens if a file with the wrong type was dropped.)
if (handleFile(path))
numHandledFiles++;
}
}
return ? // See description below.
}
现在假设我的函数 handleFile()
只能处理 .png 文件
并且具有不同文件类型的多个文件一次被拖放到我的应用程序中。
如何正确替换上面代码中的return ?
?我看到两个选项:
return numHandledFiles > 0; // At least one file could be handled.
和:
return numHandledFiles == numDroppedFiles; // All files could be handled.
我都试过了,但是当从 Windows Explorer 或 Total Commander 中删除文件到我的应用程序时, 我根本没有注意到任何差异。返回值有什么作用?
最佳答案
当阅读 MFC 文档让您感到困惑时,您应该按照您提供的链接中的建议转向 Windows SDK 文档:“有关详细信息,请参阅 Windows SDK 中的 IDropTarget::Drop。”:
On return, must contain one of the DROPEFFECT flags, which indicates what the result of the drop operation would be.
请注意 IDropTarget::Drop
更类似于 COleDropTarget::OnDropEx ,你应该实现而不是 COleDropTarget::OnDrop
。你描述的情况没有严格的规定。但是,DROPEFFECT
应该匹配应用程序行为(即接受或拒绝)。
关于c++ - 处理多个文件时,我必须在 COleDropTarget::OnDrop() 中返回哪个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43328932/