title: c++ win32 efficient context menus and submenus
body: <p>I would like to add a right click context menu/sub menus to my win32 application (c++) when the user right clicks on the notifiyicon data (tray icon). I can make a simple 1 level menu, but can't find a example for multiple level menus.</p>
  
  <p>I would like to a create a menu which looks something like this:</p>
  
  <pre><code>Settings -&gt;  Setting 1  -&gt; Setting 2
                          -&gt; Setting 3
           -&gt;  Settings 4 -&gt; Setting 5
                          -&gt; Setting 6    
  Exit 
  </code></pre>
  
  <p>I'm creating the menu with this code:</p>
  
  <pre><code> HMENU hPopupMenu = CreatePopupMenu();
   InsertMenu(hPopupMenu, 0, MF_BYPOSITION | MF_STRING, IDM_EXIT, L"Exit");
   SetForegroundWindow(hWnd);
   TrackPopupMenu(hPopupMenu, TPM_BOTTOMALIGN | TPM_RIGHTALIGN, p.x, p.y, 0, hWnd, NULL);
  </code></pre>
  
  <p>The code above is placed inside the notifyicondata message handler (WM_RBUTTONUP). </p>
  
  <p>How can i create submenus using the above code? Do i create a new HMENU and insert it in the parent menu?</p>
  
  <p>The other thing that bothers me is that the menu is always created when a right click event is triggered, so every time it fires it creates a new HMENU. Is it possible to create the menu (with submenus) when the application starts and destroy it when the application closes? Does windows handle the destroying of the menu?</p>
  
  <p>Thanks for reply's.</p>