一千萬個為什麽

搜索

在運行時更改MenuItem標題

I have a Menu with all sorts of Menu items, as you normally would. Every MenuItem (button) has a caption and I'd like to change that caption at runtime. On a normal button that isn't really a problem, I just call GetDlgItem(ID)->SetWindowText(CString);

但是我無法在菜單項上執行此操作,因為我無法將ID分配給其中任何一個。 “屬性”編輯器中的ID字段實際上顯示“無法編輯ID”。

那麽如何在運行時更改菜單項文本?

EDIT: I have tried using the CMenu::ModifyMenu however I have been unsuccessful. I don't know how to specify the button (element) to change. Also, I have doubts in the correctness of the way I pass the CString as an argument.

這是我的(失敗)嘗試:

CString str = "Foo";
CMenu * pMenu = m_wndToolBar.GetMenu();
pMenu->ModifyMenu(1, MF_BYPOSITION | MF_STRING, 0 /*Don't know what to pass as nIDNewItem */, str);

這(對ModifyMenu方法的調用)會引發調試斷言錯誤。請註意,我不知道nIDNewItem是什麽。

最佳答案

您應首先獲取菜單項的命令ID。嘗試這個:

tr = L"Foo";
CMenu * pMenu = m_wndToolBar.GetMenu();
MENUITEMINFO info;
info.cbSize = sizeof(MENUITEMINFO);
info.fMask = MIIM_ID;
VERIFY(pMenu->GetMenuItemInfo(1, &info, TRUE));
pMenu->ModifyMenuW(info.wID, MF_BYCOMMAND | MF_STRING, info.wID, tr);

轉載註明原文: 在運行時更改MenuItem標題