WinAPI - right mouse drag & drop and IContextMenu

74 Views Asked by At

When a user right-clicks and drags a file into a different directory, they are presented with a small context menu that includes entries such as Copy here and Move here:

popup

Is there a way to query these menu items yourself?

I was able to trigger the default implementation for this right drag-and-drop popup by following these steps:

  • Register your app with RegisterDragDrop.
  • In the IDropTarget::Drop method:
    • Obtain the IShellFolder interface for the drop path.
    • Get the IDropTarget interface from this folder.
    • Forward the IDataObject to the DragEnter and Drop methods.
    • The Drop method will display the correct context menu.

Here's the simplified version of the code:

HRESULT Win_DragDropTargetDrop(IDropTarget *iTarget, IDataObject *iData, DWORD keys, POINTL cursor, DWORD *effect)
{
    HRESULT hr = 0;
    IShellFolder *folder = null;
    IDropTarget *folderDropTarget = null;
    POINTL pt = {0};

    hr = Win_GetIShellFolder(hwnd, dragDropPath, &folder);
    hr = folder->lpVtbl->CreateViewObject(folder, hwnd, &IID_IDropTarget, &folderDropTarget);
    hr = folderDropTarget->lpVtbl->DragEnter(folderDropTarget, iData, MK_RBUTTON, pt, effect);
    hr = folderDropTarget->lpVtbl->Drop(folderDropTarget, iData, MK_RBUTTON, pt, effect);
}

However, what I want is to enumerate these items within IContextMenu myself, using functions such as CreatePopupMenu, QueryContextMenu, and then iterating through that menu via GetMenuItemCount and GetMenuItemInfo.

The reason is that I'm rendering the context menu myself (instead of using the Windows built-in UI framework). I'm already doing this for the regular right-click context menu on a directory background.

context menu

0

There are 0 best solutions below