{"id":8703,"date":"2012-01-04T07:00:00","date_gmt":"2012-01-04T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/01\/04\/creating-context-menus-on-menus\/"},"modified":"2012-01-04T07:00:00","modified_gmt":"2012-01-04T07:00:00","slug":"creating-context-menus-on-menus","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120104-00\/?p=8703","title":{"rendered":"Creating context menus on menus"},"content":{"rendered":"<p>\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/12\/30\/10251751.aspx\">\nLast week<\/a>\nwe looked at menu drag\/drop.\nAnother little-used menu feature added in Windows&nbsp;2000 is\nthe ability to show context menus on menus.\nThe message is <code>WM_MENU&shy;RBUTTON&shy;UP<\/code>\nand the flag is <code>TPM_RECURSE<\/code>.\nLet&#8217;s demonstrate with a simple program.\n<\/p>\n<p>\nStart with\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">\nthe scratch program<\/a>,\nand add\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/12\/30\/10251751.aspx\">\nthe\n<code>Move&shy;Menu&shy;Item<\/code> function<\/a>\njust so our context menu can do something.\n<\/p>\n<pre>\n\/\/ resource header file\n#define IDM_MAIN 1\n#define IDM_POPUP 2\n#define IDC_MOVEUP 200\n#define IDC_MOVEDOWN 201\n\/\/ resource file\n1 MENU PRELOAD\nBEGIN\n    POPUP \"&amp;Test\"\n    BEGIN\n        MENUITEM \"&amp;Red\",    100\n        MENUITEM \"&amp;Orange\", 101\n        MENUITEM \"&amp;Yellow\", 102\n        MENUITEM \"&amp;Green\",  103\n        MENUITEM \"&amp;Blue\",   104\n        MENUITEM \"&amp;Violet\", 105\n    END\nEND\n2 MENU PRELOAD\nBEGIN POPUP \"\"\n    BEGIN\n        MENUITEM \"Move &amp;Up\",   IDC_MOVEUP\n        MENUITEM \"Move &amp;Down\", IDC_MOVEDOWN\n        MENUITEM SEPARATOR\n        MENUITEM \"&amp;Cancel\",    IDCANCEL\n    END\nEND\n\/\/ scratch.cpp\n#define HANDLE_WM_MENURBUTTONUP(hwnd, wParam, lParam, fn) \\\n    ((fn)((hwnd), (UINT)(wParam), (HMENU)(lParam)), 0L)\nvoid OnMenuRButtonUp(HWND hwnd, UINT uPos, HMENU hmenu)\n{\n if (hmenu == GetSubMenu(GetMenu(hwnd), 0)) {\n  HMENU hmenuPopup = LoadMenu(g_hinst, MAKEINTRESOURCE(IDM_POPUP));\n  if (hmenuPopup) {\n   if (uPos == 0) {\n    EnableMenuItem(hmenuPopup, IDC_MOVEUP, MF_DISABLED | MF_GRAYED);\n   }\n   if (uPos == GetMenuItemCount(hmenu) - 1) {\n    EnableMenuItem(hmenuPopup, IDC_MOVEDOWN, MF_DISABLED | MF_GRAYED);\n   }\n   DWORD dwPos = GetMessagePos();\n   UINT idCmd = TrackPopupMenuEx(GetSubMenu(hmenuPopup, 0),\n                 TPM_RECURSE | TPM_RETURNCMD,\n                 GET_X_LPARAM(dwPos),\n                 GET_Y_LPARAM(dwPos), hwnd, NULL);\n   switch (idCmd) {\n    case IDC_MOVEUP:\n     MoveMenuItem(hmenu, uPos, uPos - 1);\n     break;\n    case IDC_MOVEDOWN:\n     MoveMenuItem(hmenu, uPos, uPos + 2);\n     break;\n   }\n   DestroyMenu(hmenuPopup);\n  }\n }\n}\n    HANDLE_MSG(hwnd, WM_MENURBUTTONUP, OnMenuRButtonUp);\n\/\/ InitApp function\n    wc.lpszMenuName = MAKEINTRESOURCE(IDM_MAIN);\n<\/pre>\n<p>\nWhen we receive the\n<code>WM_MENU&shy;RBUTTON&shy;UP<\/code> message\nand confirm that the menu is the one we support,\nwe create the popup menu and display it at the mouse\nlocation (obtained via <code>Get&shy;Message&shy;Pos<\/code>)\nwith the <code>TPM_RECURSE<\/code> flag,\nindicating that this is a pop-up menu for a pop-up menu.\n(We also use <code>TPM_RETURN&shy;CMD<\/code>,\nbut that&#8217;s nothing new.)\nIf the user chose to move the item up or down, we move it up or down.\n<\/p>\n<p>\nThat&#8217;s all.\nThere really isn&#8217;t much here, but I figured I&#8217;d just\nwrite a sample program just to show how it&#8217;s done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week we looked at menu drag\/drop. Another little-used menu feature added in Windows&nbsp;2000 is the ability to show context menus on menus. The message is WM_MENU&shy;RBUTTON&shy;UP and the flag is TPM_RECURSE. Let&#8217;s demonstrate with a simple program. Start with the scratch program, and add the Move&shy;Menu&shy;Item function just so our context menu can do [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-8703","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Last week we looked at menu drag\/drop. Another little-used menu feature added in Windows&nbsp;2000 is the ability to show context menus on menus. The message is WM_MENU&shy;RBUTTON&shy;UP and the flag is TPM_RECURSE. Let&#8217;s demonstrate with a simple program. Start with the scratch program, and add the Move&shy;Menu&shy;Item function just so our context menu can do [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=8703"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8703\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=8703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=8703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=8703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}