CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2008
    Posts
    93

    Changing the check status of a menu item

    Hi at all,

    i have a SDI Applikation made by MFC. I want to change the check status of one Menu Item.
    Ho can I do that?

    I´ve tried this:
    Adding the Command Event Handler
    Code:
    void MyClass::CommandHandler()
    {
          MENUITEMINFOW mItemInfo;
        CWnd* pMainWnd = AfxGetMainWnd(); 
        CMenu *mMenu = pMainWnd->GetMenu();
        CMenu *mSubMenu = mMenu->GetSubMenu(0);
        mSubMenu->GetMenuItemInfo(ID_CONFIGURE_SETTOFOREGROUNDININCOMINGCALL, &mItemInfo);
        if(mItemInfo.fState == MF_CHECKED)
        {
            mSubMenu->CheckMenuItem(ID_CONFIGURE_SETTOFOREGROUNDININCOMINGCALL, MF_UNCHECKED);
            m_bSetForeground = false;
        }
        else if(mItemInfo.fState == MF_UNCHECKED)
        {
            mSubMenu->CheckMenuItem(ID_CONFIGURE_SETTOFOREGROUNDININCOMINGCALL, MF_CHECKED);
            m_bSetForeground = true;
        }
    }
    This doesn´t work.
    Is there a way to do it with the ON_UPDATE_COMMAND_UI?
    But I don´t know how I can get the state of the menu item entry and if I do it with this handler
    everytime since I click the Menuheader without clicking the wanted item the function will be executed and I can´t find out how to catch only if i click my item.

    Regards
    Karsten

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Changing the check status of a menu item

    Yes, using the ON_UPDATE_COMMAND_UI mechanism would be much better and easier.
    Just do it in "reverse" oder comparing with your current algorithm:
    set the variable m_bSetForeground to true if you expect the menu item to be checked and to false otherwise. Then in the ON_UPDATE_COMMAND_UI handler just check/uncheck menu item:
    Code:
    	ON_UPDATE_COMMAND_UI(ID_CONFIGURE_SETTOFOREGROUNDININCOMINGCALL, OnUpdateForegroundIncommingCall)
    END_MESSAGE_MAP()
    .....
    
    //  somewhere set the flag to correspond the checked state
    void MyClass::SomeMethod()
    {
           m_bSetForeground = true;
           ...
    }
    
    void MyClass::OnUpdateForegroundIncommingCall(CCmdUI* pCmdUI)
    {
           pCmdUI->SetCheck(m_bSetForeground ? 1 : 0); 
    }
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured