Getting a custom right-click menu for the caption icon

Explorer does it. Now you can too.

It's a simple matter of detecting a context menu on the caption icon and displaying a custom context menu. Here are the simple changes to our scratch program to display a rather pointless one-item menu.

// Add to WndProc
        if (lParam != -1 &&
            SendMessage(hwnd, WM_NCHITTEST,
                        0, lParam) == HTSYSMENU) {
            HMENU hmenu = CreatePopupMenu();
            if (hmenu) {
                AppendMenu(hmenu, MF_STRING, 1,
                           TEXT("Custom menu"));
                TrackPopupMenu(hmenu, TPM_LEFTALIGN | TPM_TOPALIGN |
                               GET_Y_LPARAM(lParam), 0, hwnd, NULL);
            return 0;

When we receive a WM_CONTEXTMENU message, we check that it did not come from the keyboard (lParam != -1) and that the mouse is on the caption icon (called HTSYSMENU because it displays the system menu by default). If so, then we create a little popup menu and display it. Don't forget to return 0 instead of passing the message to DefWindowProc, because the default behavior is to display the system menu.

Of course, in real life, you probably would use LoadMenu to get the menu so you could just use the resource editor to create it, rather than creating it in code.

Comments (2)
  1. Scott says:

    I’m a little late to this party, but what exactly is the caption icon?

  2. Reid says:

    The icon in the top left corner of an app’s window, next to the caption. You’ll notice that the context menu for most of them looks exactly like the context menu for the app in the taskbar, but IE and Explorer do custom menus there.

Comments are closed.