首先定义右键消息函数:
afx_msg void OnRBClick(NMHDR* pNMHDR, LRESULT* pResult);
然后在消息循环中定义消息对应关系:
ON_NOTIFY(NM_RCLICK, ID_TREECTRL, OnRbClick)
接着定义消息函数内容:
void OnRBClick(NMHDR* pNMHDR, LRESULT* pResult)
{
CPoint point;
GetCursorPos(&point);
CPoint pointInTree = point;
m_TreeCtrl.ScreenToClient(&pointInTree);
HTREEITEM item;
UINT flag = TVHT_ONITEM;
item = m_TreeCtrl.HitTest(pointInTree, &flag);
if(item != NULL)
{
m_TreeCtrl.SelectItem(item);
CMenu menu;
menu.LoadMenu(IDR_MENU1);
menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN |
TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
}
}