Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Adding sub menus to a menu

Adding sub menus to a menu

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionlearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel1324
    wrote on last edited by
    #1

    Heres what I have... { CMenu menu; menu.CreatePopupMenu(); for (int x = 0; x < ndex; x++) { Swap = StrArr.GetAt(x); if (Swap != "Main") menu.AppendMenu(MF_STRING, x, Swap); } CPoint point; GetCursorPos(&point); SetForegroundWindow(); int Sel = TrackPopupMenuEx(menu.GetSafeHmenu(), TPM_RETURNCMD, point.x, point.y, GetSafeHwnd(), NULL); PostMessage(WM_NULL, 0, 0); menu.DestroyMenu(); } I've deleted variable declarations and some code to save space. With the AppendMenu(), I'm of course adding menu items. What I want to do is add sub menus to each of these menu items. Instead of clicking on an item and executing a function, I want a sub menu to pop up. Can't figure out how to do it. Any help? Thanks! Daniel

    A 1 Reply Last reply
    0
    • D Daniel1324

      Heres what I have... { CMenu menu; menu.CreatePopupMenu(); for (int x = 0; x < ndex; x++) { Swap = StrArr.GetAt(x); if (Swap != "Main") menu.AppendMenu(MF_STRING, x, Swap); } CPoint point; GetCursorPos(&point); SetForegroundWindow(); int Sel = TrackPopupMenuEx(menu.GetSafeHmenu(), TPM_RETURNCMD, point.x, point.y, GetSafeHwnd(), NULL); PostMessage(WM_NULL, 0, 0); menu.DestroyMenu(); } I've deleted variable declarations and some code to save space. With the AppendMenu(), I'm of course adding menu items. What I want to do is add sub menus to each of these menu items. Instead of clicking on an item and executing a function, I want a sub menu to pop up. Can't figure out how to do it. Any help? Thanks! Daniel

      A Offline
      A Offline
      Antti Keskinen
      wrote on last edited by
      #2

      The menu objects are always grouped as sub or pop-up menus. Now, considering your code post, there is one crucial difference in creating pop-up menus. Firstly, you need to create a new CMenu object. Then initialize it with CreatePopupMenu like you have done. After that, you need to create a yet another CMenu item, and initialize it with CreatePopupMenu again. The second menu object behaves like a pop-up menu on the first one. In order to link these two, first add items and their respective command IDs to the second menu by using AppendMenu or InsertMenu. Then use AppendMenu/InsertMenu on the first menu object, and specify MF_POPUP flag in the first parameter. Adding a MF_STRING flag allows you to have an item text as well, specified in the third parameter. Instead of a command ID in the second parameter, you now must supply a handle to the sub-menu object (HMENU). You can get this from the second menu object by querying CMenu::operator HMENU or CMenu::m_hMenu. After this, you can launch the first menu object as a pop-up menu with a sub pop-up by calling TrackPopupMenuEx. Here is a complete code fragment to give you a better clue:

      // Create a CMenu object and initialize
      CMenu* pMenu = new CMenu();
      pMenu->CreatePopupMenu();

      // Create the sub pop-up menu object
      CMenu* pSubMenu = new CMenu();
      pSubMenu->CreatePopupMenu();

      // Add menu items into the second menu
      pSubMenu->AppendMenu( MF_STRING, 1, "Option 1" );
      pSubMenu->AppendMenu( MF_STRING, 2, "Option 2" );

      // Add the pop-up menu
      pMenu->AppendMenu( MF_STRING | MF_POPUP, (UINT_PTR) pSubMenu->m_hMenu, "Sub menu" );

      // Launch and do cleanup
      pMenu->TrackPopupMenuEx(...);

      pMenu->RemoveMenu( 0, MF_BYPOSITION );
      pSubMenu->DestroyMenu(); delete pSubMenu; pSubMenu = NULL;
      pMenu->DestroyMenu(); delete pMenu; pMenu = NULL;

      I am not certain if the RemoveMenu call is necessary, as the menu cleanup code might have a routine that destroys sub-menus as well. Also, the code has no error-checking routines for possible error states like

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups