How do I make a boldface item on a context menu?


Today's Little Program displays a context menu with a bold item. I sort of talked about this some time ago, but now I'm going to actually do it. Remember, the boldface item represents the default item. You should set it to the action that would have taken place if the user had double-clicked the object.

Start with our scratch program and make the following changes:

void OnContextMenu(HWND hwnd, HWND hwndContext, UINT xPos, UINT yPos)
{
 HMENU hmenu = CreatePopupMenu();
 AppendMenu(hmenu, MF_STRING, 100, TEXT("&First"));
 AppendMenu(hmenu, MF_STRING, 101, TEXT("&Second"));
 AppendMenu(hmenu, MF_STRING, 102, TEXT("&Third"));

 SetMenuDefaultItem(hmenu, 101, FALSE);

 TrackPopupMenuEx(hmenu, 0, xPos, yPos, hwnd, nullptr);

 DestroyMenu(hmenu);
}

    HANDLE_MSG(hwnd, WM_CONTEXTMENU, OnContextMenu);

Note that for expository purposes (this is a Little Program, after all), I am not heeding the advice I gave some time ago. As a result, this program does not support multiple monitors or keyboard-invoked context menus. Read the linked article for instructions on how to fix the code.

When you right-click on the window, the On­Context­Menu function creates a pop-up window, fills it with some dummy commands, and says that item 101 should be the default. Then it displays the context menu to the user, throws away the result, and destroys the menu to avoid a memory leak.

When the menu pops up, the item Second appears in boldface.

Comments (8)
  1. 12BitSlab says:

    I probably shouldn't post this, but I have to admit that I didn't realize that there was such a thing as a default item or how to invoke it.  Thanks for educating me.

  2. Neil says:

    The lack of TPM_RIGHTBUTTON on context menus is really annoying. (I think Task Manager's is the one that normally annoys me.) The only one for which there is an excuse is the taskbar jump list menu, since that has its own context menu.

  3. kero says:

    Dear Raymond, please clarify one dark spot in the history of TrackPopupMenuEx.

    On Win9x we can use flag TPM_SYSMENU: menu item's click causes WM_SYSCOMMAND (instead of WM_COMMAND).

    NT bans this useful flag.

    Why?

  4. Steve Thresher says:

    Hi Raymond,

    Would you also be able to share how you add the right-justified text for the keyboard shortcut?

    Save           Ctrl+S

    Print          Ctrl+P

  5. ChrisR says:

    @Steve Thresher: Not sure if this is the Win32 way or just the MFC way, but placing a tab character between the main text and the accelerator text is what I've always done.

  6. Grenangen says:

    @Steve Thresher

    To get right aligned accelerators you would use a

    Ex:

    My Menu Item aCTRL+M

  7. Steve Thresher says:

    Thanks for the suggestions ChrisR and Grenangen. I'm using the Win32 function TrackPopupMenuEx() and inserting a tab character is working for me. Inserting a just gives me a square character between the menu text and accelerator text.

  8. Grenangen says:

    @Steve Hmm, that's a bit odd. a works just fine from resource files for ex. Looking at a rc file of mine:

    MENUITEM "E&xit aAlt+F4",              IDM_APP_EXIT

    This gives me the expected right alignment without the side effects t will have.

Comments are closed.

Skip to main content