Grey out the menu in MFC

Posted on 2005-05-16
Last Modified: 2013-11-20
I am trying to grey out the submenu item if a flag is set to false.

how do i acheive this ? Can anyone send a snippet please


void XgView::OnToolsDelete()
  XgDeleteDlg Cdlg(AGROUP);

      if (Cdlg.DoModal() == IDOK) {}
Question by:hari4130
    LVL 30

    Accepted Solution

    You can disable a menu item via following method:

    CMenu* pMenu = GetMenu();

    However, I recommend you use the ON_UPDATE_COMMAND_UI MFC macro to do this
    LVL 30

    Expert Comment

    To use the ON_UPDATE_COMMAND_UI MFC macro, select the view in ClassWizard, select the menu item (ID_FILE_NEW or whatever), and create a handle for ON_UPDATE_COMMAND_UI.
    LVL 30

    Expert Comment

    Here's example code for your ON_UPDATE_COMMAND_UI function:

    void CMainFrame::OnUpdateCommand...(CCmdUI* pCmdUI)
         pCmdUI->Enable(bEnableThisMenuItem );    

    In above example function, bEnableThisMenuItem is a member BOOL variable you create to enable or disable your menu item.
    LVL 3

    Expert Comment

    Hi either use the OnUpdate or Modify Menu

    void CMainFrame::OnUpdateFileTest(CCmdUI* pCmdUI)

          //ModifyMenu(hMenu, SC_CLOSE, MF_BYCOMMAND|MF_GRAYED, SC_CLOSE, "Close")

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now