将项目数据附加到菜单项
Attach item data to a menu item
创建上下文菜单正在进行中:
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
)?
不清楚为什么需要将菜单项与变量相关联 i
。我认为您需要的是在单击动态菜单项时处理消息。对于静态菜单项,您可以在消息映射中使用 ON_COMMAND
and 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,以便更快地访问。
创建上下文菜单正在进行中:
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
)?
不清楚为什么需要将菜单项与变量相关联 i
。我认为您需要的是在单击动态菜单项时处理消息。对于静态菜单项,您可以在消息映射中使用 ON_COMMAND
and 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,以便更快地访问。