我读到的是菜单必须将其MenuInfo.dwStyle标志设置为MNS_NOTIFYBYPOS,我所做的是:
MENUINFO MenuInfo;
memset(&MenuInfo,0, sizeof(MenuInfo));
MenuInfo.cbSize = sizeof(MenuInfo);
HMENU hPopupMenu = CreatePopupMenu();
GetMenuInfo(hPopupMenu, &MenuInfo);
MenuInfo.dwStyle |= MNS_NOTIFYBYPOS;
SetMenuInfo(hPopupMenu, &MenuInfo);
然后继续添加项目:
InsertMenu(hPopupMenu, pos, MF_BYPOSITION, id , "do command");
下一步跟踪:
TrackPopupMenu(hPopupMenu, TPM_CENTERALIGN | TPM_RETURNCMD, cursorPos.x, cursorPos.y, 0, hwnd, NULL);
但它没有效果,它编译没有错误,但点击事件直到发送为WM_COMMAND
答案 0 :(得分:2)
您需要设置MENUINFO
结构的fMask成员,以告诉系统您要设置/获取哪些字段。
E.g。
MenuInfo.fMask = MIM_STYLE;
GetMenuInfo(hPopupMenu, &MenuInfo);
MenuInfo.dwStyle |= MNS_NOTIFYBYPOS;
SetMenuInfo(hPopupMenu, &MenuInfo);
另请注意,文档说:
MNS_NOTIFYBYPOS是菜单标题样式,应用时无效 个别子菜单。
所以有可能它无论如何都不适合你用弹出菜单。