views:

54

answers:

1

Have created a ATL COM project through which I am inserting Menu Items to The rightclick menu like this:

STDMETHODIMP CSimpleShlExt::QueryContextMenu (
                      HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd,
                      UINT uidLastCmd, UINT uFlags )
{
    gHMenu=hmenu;
    UINT uCmdID = uidFirstCmd;

    // If the flags include CMF_DEFAULTONLY then we shouldn't do anything.

    if ( uFlags & CMF_DEFAULTONLY )
        return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );

    InsertMenu ( hmenu, uMenuIndex,  MF_BYPOSITION, uCmdID++, _T("Connect To Server") );
    uMenuIndex++;
    InsertMenu ( hmenu, uMenuIndex, MF_BYPOSITION, uCmdID++,
               _T("DisConnect From Server") );
    return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 2 );
}

Now I wan to Disable the Connect submenu as soon as the user clicks on it and for this I am using EnableSubMenuItem ,

STDMETHODIMP CSimpleShlExt::InvokeCommand ( LPCMINVOKECOMMANDINFO pCmdInfo )
{
    // If lpVerb really points to a string, ignore this function call and bail out.
    if ( 0 != HIWORD( pCmdInfo->lpVerb ) )
        return E_INVALIDARG;


    switch ( LOWORD( pCmdInfo->lpVerb) )
        {
        case 0:
            {

            TCHAR szMsg [MAX_PATH + 32];

            wsprintf ( szMsg, _T("The selected file was:\n\n%s"), m_szFile );

            MessageBox ( pCmdInfo->hwnd, "Connected", _T("SimpleShlExt"),
                         MB_ICONINFORMATION );

             //InsertMenu ( hmenu, uMenuIndex, MF_STRING | MF_BYPOSITION, uCmdID++, _T("Connect => NDS") );
            //EnableMenuItem(gHMenu,0,MF_GRAYED);

Edit:

EnableMenuItem(gHMenu,0,MF_DISABLED | MF_GRAYED | MF_BYPOSITION);


            return S_OK;
            }
            break;

.....
....
...
}

But this is not helping with disabling the Menu Item. What am I doing Wrong??

+1  A: 

Try this:

EnableMenuItem(gHMenu,ITEM_ID,MF_DISABLED | MF_GRAYED);

ITEM_ID should be resource menu item ID. Or:

EnableMenuItem(gHMenu,ITEM_POSITION,MF_DISABLED | MF_GRAYED | MF_BYPOSITION);

ITEM_POSITION - menu item index.

Alex Farber
@Alex Farber,What is Resource Menu ? If resource menu is the menu where I have inserted the MenuItems , How will I obtain the ITEM_ID from it?
Subhen
Looking at your menu creation code, I see that you use InsertMenu with MF_BYPOSITION flag. In this case, you can only access menu items by position, so use the second version: EnableMenuItem(gHMenu,ITEM_POSITION,MF_DISABLED | MF_GRAYED | MF_BYPOSITION);
Alex Farber
@Alex Farber, Tried the option. Is not helping?Edited the Code for reference.
Subhen