Search code examples
c++windowsshell-extensionsinvoke-command

Can we execute rightclick without using pCmdInfo->lpVerb


I am not agnaist using pCmdInfo->lpVerb but my problem is how will we handle the situation when we create the rightclick submenus dynamically. For example, I have the following scenario:

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 );
}

Now If I am using pCmdInfo->lpVerb with switch case , as demonstrated below, then Case 1 is getting executed Whether I click on DisConnect (Menu Item Inserted in If condition in above code snippet) or Help (Menu Item Inserted in Else part as above code snippet)

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

Solution

  • You are supposed to store the menu item identifiers (or offsets?) in QueryContextMenu for use later in 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"
        }
    }