开发者

c++ win32 efficient context menus and submenus

开发者 https://www.devze.com 2023-04-08 13:05 出处:网络
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

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.

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

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

I'm creating the menu with this code:

 HMENU hPopupMenu = CreatePopupMenu();
 InsertMenu(hPopupMenu, 0, MF_BYPOSITION | MF_ST开发者_如何转开发RING, IDM_EXIT, L"Exit");
 SetForegroundWindow(hWnd);
 TrackPopupMenu(hPopupMenu, TPM_BOTTOMALIGN | TPM_RIGHTALIGN, p.x, p.y, 0, hWnd, NULL);

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

How can i create submenus using the above code? Do i create a new HMENU and insert it in the parent menu?

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?

Thanks for reply's.


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

There is nothing stopping you from creating the menu when your application starts but unless the menu has a large amount of items or creating the items requires a lot of calculation I don't see the problem with creating them in response to the tray icon message.

You have to destroy a HMENU yourself (Except if it is attached to a HWND with SetMenu())

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号