处理多个文件时,我必须在 COleDropTarget::OnDrop() 中返回哪个值

Which value do I have to return in COleDropTarget::OnDrop() when handling multiple files?

本文关键字:OnDrop 返回 COleDropTarget 文件 处理      更新时间:2023-10-16

我通过从COleDropTarget派生类CDropTarget并重写所有必要的函数,使我的MFC应用程序成为放置目标。一切都按预期工作。但是,OnDrop(( 的返回值让我感到困惑。其描述如下:

如果删除成功,则为非零;否则为 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资源管理器或Total Commander中删除文件时,我根本没有注意到任何区别。返回值有什么影响?

当阅读 MFC 文档让您感到困惑时,您应该按照您提供的链接中的建议转向 Windows SDK 文档:">有关更多信息,请参阅 Windows SDK 中的 IDropTarget::D rop。

返回时,必须包含 DROPEFFECT 标志之一,该标志指示删除操作的结果

请注意,IDropTarget::Drop更类似于 COLEDropTarget::OnDropEx,您应该实现它而不是COleDropTarget::OnDrop。对于您描述的情况没有严格的规则。但是,DROPEFFECT应与应用程序行为匹配(即接受或拒绝(。