2

创建了一个 ATL COM 项目,通过它我将菜单项插入到右键菜单中,如下所示:

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

现在,一旦用户单击它,我就想禁用Connect子菜单,为此我正在使用 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);

编辑:

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


            return S_OK;
            }
            break;

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

但这无助于禁用菜单项。我究竟做错了什么??

4

1 回答 1

6

试试这个:

EnableMenuItem(gHMenu,ITEM_ID,MF_DISABLED | MF_GRAYED);

ITEM_ID应该是菜单项的资源 ID。

或者:

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

其中ITEM_POSITION是菜单项的零索引位置。

请注意,调用后EnableMenuItem,您可能需要调用DrawMenuBar以确保显示已更新。

于 2010-09-23T07:48:29.663 回答