c++ win32 高效的上下文菜单和子菜单
当用户右键单击 notifyicon 数据(托盘图标)时,我想向我的 win32 应用程序(c++)添加右键单击上下文菜单/子菜单。我可以制作一个简单的一级菜单,但找不到多级菜单的示例。
我想创建一个看起来像这样的菜单:
Settings -> Setting 1 -> Setting 2
-> Setting 3
-> Settings 4 -> Setting 5
-> Setting 6
Exit
我正在使用以下代码创建菜单:
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);
上面的代码放置在notifyicondata 消息处理程序(WM_RBUTTONUP)内。
如何使用上面的代码创建子菜单?我是否创建一个新的 HMENU 并将其插入到父菜单中?
另一件让我困扰的事情是,菜单总是在触发右键单击事件时创建,因此每次触发时都会创建一个新的 HMENU。是否可以在应用程序启动时创建菜单(带有子菜单)并在应用程序关闭时销毁它? Windows 是否处理菜单的销毁?
感谢您的回复。
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_STRING, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
子菜单只是另一个 HMENU(来自 CreatePopupMenu()),通过使用 MF_POPUP 标志的 AppendMenu/InsertMenu 或掩码中带有 MIIM_SUBMENU 的 InsertMenuItem 作为菜单项插入。
当应用程序启动时,没有什么可以阻止您创建菜单,但除非菜单有大量项目或创建项目需要大量计算,否则我不认为创建它们来响应托盘图标消息有问题。
您必须自己销毁 HMENU(除非它附加到带有 SetMenu())
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())