1

我不反对使用pCmdInfo->lpVerb,但我的问题是当我们动态创建右键单击子菜单时我们将如何处理这种情况。例如,我有以下场景:

if(strcmp(cRegKeyVal,"Connected")==0)
    {
        //g_bConnectStatus=TRUE;
        InsertMenu ( m_hSubmenu , 0,  MF_BYPOSITION|MF_GRAYED, m_uCmdID++, _T("Connect") );
        InsertMenu ( m_hSubmenu , 1,  MF_BYPOSITION, m_uCmdID++, _T("DicConnect") );
        InsertMenu ( m_hSubmenu , 2,  MF_BYPOSITION, m_uCmdID++, _T("Configure") );
        InsertMenu ( m_hSubmenu , 3,  MF_BYPOSITION, m_uCmdID++, _T("Menu4") );
        InsertMenu ( m_hSubmenu , 4,  MF_BYPOSITION, m_uCmdID++, _T("About") );
    }
    else
    {
        //g_bConnectStatus=FALSE;
        InsertMenu ( m_hSubmenu , 0,  MF_BYPOSITION, m_uCmdID++, _T("Connect") );
        InsertMenu ( m_hSubmenu , 3,  MF_BYPOSITION, m_uCmdID++, _T("Help") );
        InsertMenu ( m_hSubmenu , 4,  MF_BYPOSITION, m_uCmdID++, _T("About") );
    }
..  
..  
InsertMenuItem ( hmenu, uMenuIndex, TRUE, &mii );
}

现在,如果我使用pCmdInfo->lpVerbswitch case ,如下所示,那么Case 1无论我点击DisConnect(在上面的代码片段中的 If条件中插入的Help菜单项)还是(在上面的代码片段中的其他部分插入的菜单项),都将被执行

switch ( LOWORD( pCmdInfo->lpVerb) )
        {
        case 0:
            {
            //Your Logi for Case-0
            }
            break;
        case 1:
            {
        //Your Logi for Case-I      
            }
        break;
      }
4

1 回答 1

1

您应该将菜单项标识符(或偏移量?)存储在 QueryContextMenu 中,以便稍后在 InvokeCommand 中使用:

QueryContextMenu()
{
    m_uConnectId = m_uCmdID++;
    InsertMenu( m_hSubMenu, "Connect" );
    m_uHelpId = m_uCmdID++;
    InsertMenu( m_hSubMen, "Help" );
}

InvokeCommand()
{
    ULONG uCmdID = LOWORD( pCmdInfo->lpVerb );
    if( uCmdID == m_uConnectId )
    {
        // do "Connect"
    }
    else if( uCmdID == m_uHelpId )
    {
        // do "Help"
    }
}
于 2010-09-27T15:04:06.723 回答