May 13th, 2021

Drag/drop effects: The little drop information box

Explorer’s drag/drop effects include a little message information box as part of the drop effect. For example, it will start with the message Move to ⸢folder⸥, but if you hold the Ctrl key, the message changes to Copy to ⸢folder⸥. How can you get a piece of that action?

As we saw last time, the visual drag/drop feedback effects used by Explorer are available for general use, via an object know as the shell drop target helper.

Let’s take our scratch program and teach it how to add the fancy information box.

The information box is configured by the DROP­DESCRIPTION structure which is associated with the CFSTR_DROP­DESCRIPTION clipboard format.

#include <strsafe.h>

HRESULT SetDropDescription(
    IDataObject* dataObject, DROPIMAGETYPE type,
    PCWSTR message = nullptr, PCWSTR insert = nullptr)
{
    wil::unique_hglobal_ptr<DROPDESCRIPTION> data(
        reinterpret_cast<DROPDESCRIPTION*>(
            GlobalAlloc(GPTR, sizeof(DROPDESCRIPTION))));
    RETURN_IF_NULL_ALLOC(data);
    data->type = type;
    RETURN_IF_FAILED(StringCchCopyEx(data->szMessage, ARRAYSIZE(data->szMessage),
        message, nullptr, nullptr, STRSAFE_IGNORE_NULLS));
    RETURN_IF_FAILED(StringCchCopyEx(data->szInsert, ARRAYSIZE(data->szInsert),
        insert, nullptr, nullptr, STRSAFE_IGNORE_NULLS));
    FORMATETC fmte = { (CLIPFORMAT)RegisterClipboardFormat(CFSTR_DROPDESCRIPTION),
        nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
    STGMEDIUM med = { TYMED_HGLOBAL };
    med.hGlobal = data.get();
    RETURN_IF_FAILED(dataObject->SetData(&fmte, &med, true));
    data.release();
    return S_OK;
}

We allocate a DROP­DESCRIPTION structure as an HGLOBAL and copy the drop image type, optional message, and optional message insertion into the structure before setting the data into the data object.

You can read the documentation for DROP­DESCRIPTION for details on how the message and message insertion interact. For this example, we’ll just use a simple message with no insertions.

struct SimpleDropTarget
{
    ...

    STDMETHOD DragLeave()
    {
        if (dto) SetDropDescription(dto.get(), DROPIMAGE_INVALID);
        dto = nullptr;
        return helper->DragLeave();
    }


    HRESULT CalculateFeedback(
        DWORD grfKeyState,
        DWORD* pdwEffect)
    {
        if (grfKeyState & MK_CONTROL) {
            *pdwEffect = DROPEFFECT_COPY;
            SetDropDescription(dto.get(), DROPIMAGE_COPY,
                L"Copy with awesomesauce.");
        }
        else {
            *pdwEffect = DROPEFFECT_MOVE;
            SetDropDescription(dto.get(), DROPIMAGE_MOVE,
                L"Move with awesomesauce.");
        }
        return S_OK;
    }

};

When we calculate the feedback, we also set the drop description to tell the user what will happen when the drop happens. We also clear the drop description (by setting it to “invalid” with an empty message)¹ when the user decided not to drop it onto our drop target after all. This ensures that our custom description doesn’t hang around and accidentally get shown for other drop targets that don’t set a description.

So there you have it, drag feedback done the same way that Explorer does it.

¹ There is no way to remove data from a data object, so the best you can do is replace the data with new data that says “Nevermind.”

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

7 comments

Discussion is closed. Login to edit/delete existing comments.

  • M. W. · Edited

    Does each application get its own DnD instance or is there a global shared one? I've seen it get stuck on numerous occasions where for example, there is a ghost image of a file or folder stuck on the desktop and drag-and-drop no longer works and I have to jump through hoops to try to get Explorer to let me drag something again (which usually doesn't work at all until I restart it or reboot)....

    Read more
  • 紅樓鍮

    Is it a requirement to use GlobalAlloc here?

    • Chris Iverson

      Yes. The documentation for CFSTR_DROPDESCRIPTION specifically defines the data structure as being an HGLOBAL, which needs to come from GlobalAlloc.

      More specifically, the STGMEDIUM(storage medium) parameter is defined to hold TYMED_HGLOBAL, which the TYMED doc lists as

      "The storage medium is a global memory handle (HGLOBAL). Allocate the global handle with the GMEM_MOVEABLE flag. If the punkForRelease member of STGMEDIUM is NULL, the destination process should use GlobalFree to release the memory."

      Read more
      • 紅樓鍮 · Edited

        I'm tentative about whether that requirement is actually enforced in Windows NT, because in the given code snippet (which implies ) is passed to , whereas accordingly requires . causes to return a pointer, whereas causes it to return an opaque handle, so if does work the way it is described, the given code should not work because the incorrect type of argument is passed.

        Read more
      • skSdnW

        GlobalAlloc is absolutely required and the example code is correct. Think about what the data object is going to do; GlobalLock, use data, GlobalUnlock, GlobalFree. The trick here is that GlobalLock on a fixed HGLOBAL is legal.

        The bug potential is in custom data objects. If they forget to call Lock they will seem to work fine when given fixed memory but break when given movable memory.

        See also: Search this blog for GlobalAlloc to find the...

        Read more
  • Stuart Ballard

    Does that mean it's up to every application to associate semantics with particular modifier keys - they have to ask "is the ctrl key pressed?" and know the convention that ctrl means copy, rather than being able to ask the framework "is this a copy operation?"

    That would seem to pose some interesting future-compatibility challenges: if the touchscreen UI wanted to adopt a convention that two-finger drag means copy[1], or a Minority Report style VR gesture-based...

    Read more
    • Andrew Cook · Edited

      The touch convention is that immediate drag is treated as left-click drag and tap-and-hold then drag is treated as right-click drag. Standard keyboard modifiers apply. If you're on a device without a keyboard, you're not out of luck, since all of the options available with modifier keys are typically (and required by the Windows UI design guidelines) also available on the context menu after a right-click drag.

      Not every application has the same verbs for a...

      Read more