Getting a custom right-click menu for the caption icon

Date:October 27, 2003 / year-entry #110
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20031027-00/?p=42023
Comments:    2
Summary: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 case WM_CONTEXTMENU: if (lParam != -1 && SendMessage(hwnd, WM_NCHITTEST,...

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
    case WM_CONTEXTMENU:
        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 |
                                      TPM_RIGHTBUTTON,
                               GET_X_LPARAM(lParam),
                               GET_Y_LPARAM(lParam), 0, hwnd, NULL);
                DestroyMenu(hmenu);
            }
            return 0;
        }
        break;

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.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index