这是一项正在进行的工作,用于创建上下文菜单:
CMenu mnuContext;
CMenu *pMnuPopup, *pMnuSwap;
mnuContext.LoadMenu(IDR_MENU_MWB_SWAP);
pMnuPopup = mnuContext.GetSubMenu(0);
if (pMnuPopup != nullptr)
{
pMnuSwap = pMnuPopup->GetSubMenu(0);
if (pMnuSwap != nullptr)
{
CMenu *pMnuChairman = pMnuSwap->GetSubMenu(0);
if (pMnuChairman != nullptr)
pMnuChairman->DeleteMenu(0, MF_BYPOSITION);
//#TODO Init other pop up menus
int iChairman = -1; // Reset counters
for (int i = 0; i < m_cbDates.GetCount(); i++)
{
CString strDate, strMenuText;
m_cbDates.GetLBText(i, strDate);
CChristianLifeMinistryEntry *pEntry = static_cast<CChristianLifeMinistryEntry*>(m_cbDates.GetItemDataPtr(i));
if (pEntry->GetChairman() != _T("") && pMnuChairman != nullptr && i != m_iDateIndex)
{
iChairman++;
strMenuText = pEntry->GetChairman() + _T(" (") + strDate + _T(")");
pMnuChairman->InsertMenu(iChairman, MF_BYPOSITION, ID_SWAP_CHAIRMAN_ITEM + iChairman, strMenuText);
//# TODO Can I assign the pointer of pEntry? Or the "i" variable?
}
}
pMnuPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON,
pMsg->pt.x, pMsg->pt.y, this, nullptr);
}
}
如您所见,我正在创建一个这样的菜单项:
pMnuChairman->InsertMenu(iChairman, MF_BYPOSITION, ID_SWAP_CHAIRMAN_ITEM + iChairman, strMenuText);
我是否可以将此i
变量与此菜单项相关联?要么是,要么是实际指针(pEntry
)?
答案 0 :(得分:1)
目前还不清楚为什么需要将菜单项与变量i
相关联。我认为你需要的是在单击动态菜单项时处理消息。对于静态菜单项,您可以在消息映射中使用ON_COMMAND和ON_UPDATE_COMMAND_UI宏。
在您的情况下,您可以使用消息映射中的ON_COMMAND_RANGE为菜单项范围设置一个处理程序:
BEGIN_MESSAGE_MAP(CYourView, CFormView)
ON_COMMAND_RANGE(ID_SWAP_CHAIRMAN_ITEM, ID_SWAP_CHAIRMAN_ITEM + 100, OnChairmanClick)
END_MESSAGE_MAP()
您的OnChairmanClick
函数将具有以下语法:
void CYourView::OnChairmanClick(UINT nID)
您需要记住与主席关联的菜单项的UID,例如在CChristianLifeMinistryEntry
中,您可以为其添加UID成员。您还可以创建一个CMap<UID, CChristianLifeMinistryEntry, UID, CChristianLifeMinistryEntry>
来存储UID,以便更快地访问。