我想制作一个像Mozilla Firefox子菜单一样的子菜单:
现在就是这样(在我的程序中):
但是我希望它看起来像Firefox,当你把鼠标放在它上面时它会有一个额外的菜单。
#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;
}
}
答案 0 :(得分:9)
您只需创建另一个菜单并将其附加为子菜单。您可以通过调用相同的AppendMenu
function来执行此操作,只需将uFlags
参数设置为MF_POPUP
,然后将句柄作为uIDNewItem
参数传递给子菜单。
例如:
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;
响应WM_COMMAND
消息,您无需执行任何特殊操作。将鼠标悬停在父菜单项上时,Windows将自动显示弹出菜单。当然,您将需要处理子菜单上 上显示的项目的命令。
答案 1 :(得分:3)
首先,创建一个新菜单并向其中添加项目。例如:
HMENU hSubmenu = CreatePopupMenu();
// Insert or append items to hSubmenu here
然后将创建的菜单附加到hMenu:
AppendMenu(hMenu, MF_POPUP | MF_STRING, (UINT_PTR)hSubmenu, "My Submenu");