I made my MFC application a drop target by deriving the class CDropTarget
from COleDropTarget
and overriding all necessary functions. Everything works as expected. However, the return value of OnDrop() confuses me. Its description reads:
Nonzero if the drop is successful; otherwise 0.
I don't understand what "successful" means if multiple files are dropped on my application. For example, consider the following implementation:
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.
}
Now assume that my function handleFile()
can only handle .png files
and that multiple files with different file types are dropped on my application at once.
How do I replace return ?
in the above code correctly? I see two options:
return numHandledFiles > 0; // At least one file could be handled.
And:
return numHandledFiles == numDroppedFiles; // All files could be handled.
I tried both, but when dropping files from Windows Explorer or Total Commander on my application, I don't notice any difference at all. What effect does the return value have?
When reading MFC documentation leaves you puzzled you should turn to the Windows SDK documentation, as recommended in the link you provided: "For more information, see IDropTarget::Drop in the Windows SDK.":
On return, must contain one of the DROPEFFECT flags, which indicates what the result of the drop operation would be.
Note that IDropTarget::Drop
more closely resembles COleDropTarget::OnDropEx, which you should be implementing instead of COleDropTarget::OnDrop
. There is no strict rule for the case you describe. However, the DROPEFFECT
should match application behavior (i.e. accept or reject).