locked
How can I disable a context menu item when I use MFC Feature Pack? RRS feed

  • Question

  • Dear all,

    In my program(MFC Feature Pack), I want to disable a menu item. Here is my code, however, it doesn't work. Could you please tell me why? Thank you!



    void CMyView::OnContextMenu( CWnd* pWnd, CPoint point )
    
    {
    
        HMENU MyContextMenu = theApp.GetContextMenuManager()->GetMenuById( IDR_MYCONTEXTMENU );
    
        EnableMenuItem( MyContextMenu, ID_CONTEXT_FIRST, MF_DISABLED | MF_GRAYED );
    
    
    
        theApp.GetContextMenuManager()->ShowPopupMenu(
    
    		IDR_MYCONTEXTMENU, 
    
    		point.x, point.y, 
    
    		this, TRUE );
    
    }
    
    
    Tuesday, December 15, 2009 2:36 PM

Answers

  • In an MFC app, you enable or disable menu items by using ON_ UPDATE_COMMAND_UI this way:

    BEGIN_MESSAGE_MAP(CMyView, CView)
    	ON_COMMAND(ID_CONTEXT_FIRST, &CMyView::OnContextFirst)
    	ON_UPDATE_COMMAND_UI(ID_CONTEXT_FIRST, &CMyView::OnUpdateContextFirst)
    END_MESSAGE_MAP()
    
    void CMyView::OnUpdateContextFirst(CCmdUI *pCmdUI)
    {
    	pCmdUI->Enable( false );
    }


    • Proposed as answer by Wesley Yao Thursday, December 17, 2009 4:01 AM
    • Marked as answer by AddOil Thursday, December 17, 2009 7:26 AM
    Tuesday, December 15, 2009 4:00 PM

All replies

  • In an MFC app, you enable or disable menu items by using ON_ UPDATE_COMMAND_UI this way:

    BEGIN_MESSAGE_MAP(CMyView, CView)
    	ON_COMMAND(ID_CONTEXT_FIRST, &CMyView::OnContextFirst)
    	ON_UPDATE_COMMAND_UI(ID_CONTEXT_FIRST, &CMyView::OnUpdateContextFirst)
    END_MESSAGE_MAP()
    
    void CMyView::OnUpdateContextFirst(CCmdUI *pCmdUI)
    {
    	pCmdUI->Enable( false );
    }


    • Proposed as answer by Wesley Yao Thursday, December 17, 2009 4:01 AM
    • Marked as answer by AddOil Thursday, December 17, 2009 7:26 AM
    Tuesday, December 15, 2009 4:00 PM
  • Please try setting CFrameWnd::m_bAutoMenuEnable to FALSE before calling EnableMenuItem():

    GetParentFrame()->m_bAutoMenuEnable = FALSE;
    CMenu menu;
    menu.LoadMenuW(IDR_DIALOG_MENU);
    menu.EnableMenuItem(ID_CONTEXT_FIRST, MF_DISABLED | MF_GRAYED);
    theApp.GetContextMenuManager()->ShowPopupMenu(IDR_MYCONTEXTMENU, point.x, point.y, this, TRUE);

    And hgn's suggestion is the proper way to configure menu item's state.

    Sincerely,
    Wesley


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Wesley Yao Thursday, December 17, 2009 4:01 AM
    Thursday, December 17, 2009 4:01 AM