在CMFCMenuButton中正确删除所有子菜单项的问题。

huangapple go评论47阅读模式
英文:

Problem deleting all sub-menu items correctly in CMFCMenuButton

问题

我感到困惑。

我有一个菜单:

IDR_MENU_POPUP_MANAGE_GROUPS MENU
BEGIN
    POPUP "__MANAGE_GROUP__"
    BEGIN
        MENUITEM "Add Group",                   ID_POPUP_ADD_GROUP
        POPUP "Edit Group"
        BEGIN
            MENUITEM "__EDIT__",                    ID_POPUP_EDIT_GROUP_BASE
        END
        POPUP "Delete Group"
        BEGIN
            MENUITEM "__DELETE__",                  ID_POPUP_DELETE_GROUP_BASE
        END
    END
END

这被一个 CMFCMenuButton 使用。在运行时,我动态删除了两个子菜单,像这样:

CMenu* pMenu = m_menuManageGroups.GetSubMenu(0);
CMenu* pSubMenu = nullptr;

pSubMenu = pMenu->GetSubMenu(1);
pMenu->RemoveMenu(ID_POPUP_EDIT_GROUP_BASE, MF_BYCOMMAND);

在第一次实例中,这是有效的,Edit 子菜单不会出现在我的菜单按钮中。

但后来我向子菜单添加了一些菜单项:

for (auto& groupInfo : mapGroups)
{
    // first:  Group Name
    // second: Group Id
    const auto iMenuItemId = iMenuBase + groupInfo.second;

    pSubMenu->AppendMenu(MF_STRING, iMenuItemId, groupInfo.first);
}

它们被添加了,我在按钮的子菜单中看到它们。

现在,如果由于某种原因我再次运行此代码,它会将新的菜单项添加到菜单按钮中现有子菜单的底部。为什么?我以为 RemoveMenu 会删除所有现有的子菜单项。

英文:

I am getting myself confused.

I have a menu:

IDR_MENU_POPUP_MANAGE_GROUPS MENU
BEGIN
    POPUP "__MANAGE_GROUP__"
    BEGIN
        MENUITEM "Add Group",                   ID_POPUP_ADD_GROUP
        POPUP "Edit Group"
        BEGIN
            MENUITEM "__EDIT__",                    ID_POPUP_EDIT_GROUP_BASE
        END
        POPUP "Delete Group"
        BEGIN
            MENUITEM "__DELETE__",                  ID_POPUP_DELETE_GROUP_BASE
        END
    END
END

This is used by a CMFCMenuButton. At runtime I dynamically delete the two submenus like this:

CMenu* pMenu = m_menuManageGroups.GetSubMenu(0);
CMenu* pSubMenu = nullptr;

pSubMenu = pMenu->GetSubMenu(1);
pMenu->RemoveMenu(ID_POPUP_EDIT_GROUP_BASE, MF_BYCOMMAND);

In the first instance that works and the Edit sub-menu is not there in my menu button.

But then I add some menu items to the sub menu:

for (auto& groupInfo : mapGroups)
{
	// first:  Group Name
	// second: Group Id
	const auto iMenuItemId = iMenuBase + groupInfo.second;

	pSubMenu->AppendMenu(MF_STRING, iMenuItemId, groupInfo.first);

}

They get added and I see them in the sub menu of the button.

Now, if for some reason I run this code a second time it ends up adding the new menu items to the bottom of the existing sub menu in the menu button. Why? I thought RemoveMenu would delete all existing sub menu items.

答案1

得分: 1

我是这样做的:

const auto menuCount = pSubMenu->GetMenuItemCount();
for (int iMenuItem = 0; iMenuItem < menuCount; iMenuItem++)
{
    pSubMenu->DeleteMenu(0, MF_BYPOSITION);
}

但我仍然不明白为什么我不得不这样做,因为我认为:

pMenu->RemoveMenu(iMenuBase, MF_BYCOMMAND);

...会清除弹出菜单中的所有子菜单项。

英文:

I managed to do it like this:

const auto menuCount = pSubMenu-&gt;GetMenuItemCount();
for (int iMenuItem = 0; iMenuItem &lt; menuCount; iMenuItem++)
{
	pSubMenu-&gt;DeleteMenu(0, MF_BYPOSITION);
}

But I still don't understand why I had to do this because I thought:

pMenu-&gt;RemoveMenu(iMenuBase, MF_BYCOMMAND);

... would get rid of all the sub-menu items in the flyout.

huangapple
  • 本文由 发表于 2023年7月23日 17:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定