C ++ win32 effective context menus and submenus

I would like to add the context menu / submenu of the context menu to my win32 application (C ++) when the user right-clicks on notifiyicon data (tray icon). I can make a simple level 1 menu, but I cannot find an example for a menu with several levels.

I would like to create a menu that looks something like this:

Settings -> Setting 1 -> Setting 2 -> Setting 3 -> Settings 4 -> Setting 5 -> Setting 6 Exit 

I am creating a menu using this code:

  HMENU hPopupMenu = CreatePopupMenu(); InsertMenu(hPopupMenu, 0, MF_BYPOSITION | MF_STRING, IDM_EXIT, L"Exit"); SetForegroundWindow(hWnd); TrackPopupMenu(hPopupMenu, TPM_BOTTOMALIGN | TPM_RIGHTALIGN, px, py, 0, hWnd, NULL); 

The above code is placed inside the notifyicondata message handler (WM_RBUTTONUP).

How can I create a submenu using the above code? Create a new HMENU and paste it into the parent menu?

Another thing that bothers me is that the menu is always created when the right-click event is fired, so every time it fires, a new HMENU is created. Is it possible to create a menu (with a submenu) when starting the application and destroy it when closing the application? Do windows handle menu destruction?

Thanks for the answer.

+4
source share
1 answer

A submenu is another HMENU (from CreatePopupMenu ()) inserted as a menu item with AppendMenu / InsertMenu using the MF_POPUP flag or using InsertMenuItem with MIIM_SUBMENU in the mask.

Nothing prevents you from creating a menu when your application starts, but if the menu does not have a large number of elements or it takes a lot of calculations to create elements, I don’t see a problem creating them in response to the icon tray.

You need to destroy HMENU yourself (except that it is attached to HWND with SetMenu () )

+4
source

All Articles