Create Submenu

I want to make a submenu similar to this Mozilla Firefox submenu:

Firefox-> View-> Toolbars

This is what is now (in my program):

Program-> Menu-> Sub Menu

But I want it to look like Firefox if it had an extra menu when you hover over it.

#define ID_SM 1 LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: HMENU hMenubar = CreateMenu(); HMENU hMenu = CreateMenu(); AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu, "Menu"); AppendMenu(hMenu, MF_STRING, ID_SM, "Sub Menu"); /* Would I put it here? How? */ SetMenu(hwnd, hMenubar); break; case WM_COMMAND: if (LOWORD(wParam) == ID_SM) { /* Not sure if this should be here, cause I want it to pop up when you mouse over */ } break; } } 
+4
source share
2 answers

You simply create another menu and add it as a submenu. You can do this by calling the same function as AppendMenu , you just need to set uFlags on MF_POPUP and pass the handle to the submenu as a uIDNewItem .

For example, something like:

 case WM_CREATE: HMENU hMenubar = CreateMenu(); HMENU hMenu = CreateMenu(); HMENU hSubMenu = CreatePopupMenu(); AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu, "Menu"); AppendMenu(hMenu, MF_STRING, ID_SM, "Sub Menu"); AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT_PTR)hSubMenu, "Sub-Sub Menu"); SetMenu(hwnd, hMenubar); break; ) hMenu, "Menu"); case WM_CREATE: HMENU hMenubar = CreateMenu(); HMENU hMenu = CreateMenu(); HMENU hSubMenu = CreatePopupMenu(); AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu, "Menu"); AppendMenu(hMenu, MF_STRING, ID_SM, "Sub Menu"); AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT_PTR)hSubMenu, "Sub-Sub Menu"); SetMenu(hwnd, hMenubar); break; (UINT_PTR) hSubMenu, "Sub-Sub Menu"); case WM_CREATE: HMENU hMenubar = CreateMenu(); HMENU hMenu = CreateMenu(); HMENU hSubMenu = CreatePopupMenu(); AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu, "Menu"); AppendMenu(hMenu, MF_STRING, ID_SM, "Sub Menu"); AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT_PTR)hSubMenu, "Sub-Sub Menu"); SetMenu(hwnd, hMenubar); break; 

You do not need to do anything in response to WM_COMMAND . A window automatically displays a pop-up menu when you hover over a parent menu item. Of course, you will need to process the commands of the elements displayed in the submenu.

+8
source

First create a new menu and add items to it. For instance:

 HMENU hSubmenu = CreatePopupMenu(); // Insert or append items to hSubmenu here 

Then add the created menu to hMenu:

 AppendMenu(hMenu, MF_POPUP | MF_STRING, (UINT_PTR)hSubmenu, "My Submenu"); 
+3
source

Source: https://habr.com/ru/post/1391935/


All Articles