在控件上右键菜单的实现

 .h
声明
鼠标右键函数 :afx_msg void OnRclickList(NMHDR* pNMHDR, LRESULT* pResult);
命令响应函数 :virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
.cpp
1, 消息映射里面将鼠标右键消息与该控件绑定
ON_NOTIFY(NM_RCLICK, IDC_SITE_LIST, OnRclickList)
2, 实现OnRclickList:
void CMyDlg::OnRclickList(NMHDR* /*pNMHDR*/, LRESULT* pResult)
{
        if (m_oTraceMenu.GetSafeHmenu())
        {
            (void)m_oTraceMenu.DestroyMenu();
        }
        m_oTraceMenu.CreatePopupMenu();
        m_oTraceMenu.AppendMenu(MF_STRING, MDI_COMMAND1, _T("命令1"));
        m_oTraceMenu.AppendMenu(MF_SEPARATOR);
        CMenu menuPopup;
        (void)menuPopup.CreatePopupMenu();
        menuPopup.AppendMenu(MF_STRING, MDI_COMMAND3, _T("123"));
        m_oTraceMenu.AppendMenu(MF_POPUP,(UINT_PTR)menuPopup.m_hMenu, _T("456"));//加载二级菜单
        POINT pt;
        ::GetCursorPos(&pt);
        m_oTraceMenu.TrackPopupMenu(MK_RBUTTON, pt.x, pt.y, this, NULL);
        *pResult = 0;
}
3,实现OnCommand:
BOOL CMyDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
    if (0 == lParam)
    {
        UINT nMenuItemID = LOWORD(wParam);
        switch (nMenuItemID)
        {         
        case MDI_COMMAND1
            break;
     ......
        default:
            {
                return TRUE;
            }
            break;
        }
    }
    return __super::OnCommand(wParam, lParam);
}

你可能感兴趣的:(c++编程)