Welcome to MSDN Blogs Sign in | Join | Help

How to host an IContextMenu, part 7 - Invoking the default verb

When we last left our hero, we were wondering how to invoke the default verb programmatically. Now that we've learned a lot about how IContextMenu is used in the interactive case, we can use that information to guide us in its use in the noninteractive case.

The key here is using the HMENU to identify the default menu item and just invoke it directly. Go back to the program from part 1 where we left it and make these changes:

void OnContextMenu(HWND hwnd, HWND hwndContext, UINT xPos, UINT yPos)
{
  IContextMenu *pcm;
  if (SUCCEEDED(GetUIObjectOfFile(hwnd, L"C:\\Windows\\clock.avi",
                   IID_IContextMenu, (void**)&pcm))) {
    HMENU hmenu = CreatePopupMenu();
    if (hmenu) {
      if (SUCCEEDED(pcm->QueryContextMenu(hmenu, 0,
                             SCRATCH_QCM_FIRST, SCRATCH_QCM_LAST,
                             CMF_NORMAL))) {
        UINT id = GetMenuDefaultItem(hmenu, FALSE, 0);
        if (id != (UINT)-1) {
          CMINVOKECOMMANDINFO info = { 0 };
          info.cbSize = sizeof(info);
          info.hwnd = hwnd;
          info.lpVerb = MAKEINTRESOURCEA(id - SCRATCH_QCM_FIRST);
          pcm->InvokeCommand(&info);
        }
      }
      DestroyMenu(hmenu);
    }
    pcm->Release();
  }
}

We added the call to GetMenuDefaultItem to obtain the default menu item and then set the verb in the form of a menu identifier offset. (I.e., we subtract the starting point we passed to IContextMenu::QueryContextMenu.)

This code works but could be better. Next time, we'll make a minuscule tweak that improves the performance.

Published Thursday, September 30, 2004 7:01 AM by oldnewthing
Filed under:

Comments

# How to host an IContextMenu, part 8 - Optimizing for the default command

Sunday, August 07, 2005 6:56 PM by The Old New Thing
Specifying that you are interested only in the default command.

# How to host an IContextMenu, part 8 - Optimizing for the default command

Sunday, August 07, 2005 6:57 PM by The Old New Thing
Specifying that you are interested only in the default command.

# IContextMenu のホスト方法 - Shell

Wednesday, March 22, 2006 1:33 PM by 社本@ワック Blog
IContextMenu のホスト方法 - Shell
New Comments to this post are disabled
 
Page view tracker