Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MFC Menu checked on Doc/View app?

MFC Menu checked on Doc/View app?

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lordgreg
    wrote on last edited by
    #1

    hi all,.. i'm creating a Doc/View program (single window). this program also has a menu (IDR_MAINFRAME), which looks like this: [ my_program ] [ some option ] [ 2nd option ] [ submenu ] |- [ option_to_check ] '- [ another_option_to_check ] [ exit_option ] and, i created a handler which runs when i click on "another_option_to_check". when i do that, i would like to UNCHECK "option_to_check" and CHECK another_option_to_check or vice versa if 2nd option is checked. i tried to do it like this: void CVsebnostniTestView::OnMenuRisanjeTocke() { // TODO: Add your command handler code here CMenu* mmenu; mmenu = GetMenu(); if( mmenu->GetMenuState(ID_RISANJE_MNOGOKOTNIK, MF_CHECKED) ) MessageBox( "Option checked!" ); else MessageBox( "Option unchecked!" ); } ID_RISANJE_MNOGOKOTNIK = id from option_to_check option in menu. i also tried with CMenu* mmenu, and then submenu(3) etc. and even that didn't work. what am i doing wrong then? i would really apretiate any help offered. thanks and best regards ;)

    B D 2 Replies Last reply
    0
    • L lordgreg

      hi all,.. i'm creating a Doc/View program (single window). this program also has a menu (IDR_MAINFRAME), which looks like this: [ my_program ] [ some option ] [ 2nd option ] [ submenu ] |- [ option_to_check ] '- [ another_option_to_check ] [ exit_option ] and, i created a handler which runs when i click on "another_option_to_check". when i do that, i would like to UNCHECK "option_to_check" and CHECK another_option_to_check or vice versa if 2nd option is checked. i tried to do it like this: void CVsebnostniTestView::OnMenuRisanjeTocke() { // TODO: Add your command handler code here CMenu* mmenu; mmenu = GetMenu(); if( mmenu->GetMenuState(ID_RISANJE_MNOGOKOTNIK, MF_CHECKED) ) MessageBox( "Option checked!" ); else MessageBox( "Option unchecked!" ); } ID_RISANJE_MNOGOKOTNIK = id from option_to_check option in menu. i also tried with CMenu* mmenu, and then submenu(3) etc. and even that didn't work. what am i doing wrong then? i would really apretiate any help offered. thanks and best regards ;)

      B Offline
      B Offline
      Bob Ciora
      wrote on last edited by
      #2

      Sorry, I'm at work right now and don't have much time to respond. You should consider using the OnUpdate... handlers to check and uncheck the items. These are called by MFC for each menu item just prior to displaying the menu. Here, you operate on a CCmdUI object for that menu item, telling it exactly how it should be displayed. So if you keep a flag indicating the checked state of "option," you can easily check/uncheck "option" and "another_option" based on that state. Examples would be something like this, using both the On... and OnUpdate... handlers:

      BOOL m_bOptionSelected = FALSE; // should be member variable...

      void OnOption() // called when Option selected
      {
      m_bOptionChecked = FALSE; // turn off check for this option

      // ... all other processing for Option
      

      }

      void OnAnotherOption()
      {
      m_bOptionSelected = TRUE; // turn on check for "option"

      // ... all other processing for AnotherOption
      

      }

      void OnUpdateOption(CCmdUI * pCmdUI)
      {
      // This line "checks" Option if the flag is marked, or clears the
      // check if not.
      pCmdUI->SetCheck( m_bOptionSelected ? BST_CHECKED :
      BST_UNCHECKED);

      pCmdUI->Enable(TRUE);
      

      }

      void OnUpdateAnotherOption(CCmdUI * pCmdUI)
      {
      // Reverse: "uncheck" AnotherOption if Option is checked, check
      // it if not.
      pCmdUI->SetCheck( m_bOptionSelected ? BST_UNCHECKED :
      BST_CHECKED);

      pCmdUI->Enable(TRUE);
      

      }

      Hope it helps. Bob Ciora

      L 1 Reply Last reply
      0
      • B Bob Ciora

        Sorry, I'm at work right now and don't have much time to respond. You should consider using the OnUpdate... handlers to check and uncheck the items. These are called by MFC for each menu item just prior to displaying the menu. Here, you operate on a CCmdUI object for that menu item, telling it exactly how it should be displayed. So if you keep a flag indicating the checked state of "option," you can easily check/uncheck "option" and "another_option" based on that state. Examples would be something like this, using both the On... and OnUpdate... handlers:

        BOOL m_bOptionSelected = FALSE; // should be member variable...

        void OnOption() // called when Option selected
        {
        m_bOptionChecked = FALSE; // turn off check for this option

        // ... all other processing for Option
        

        }

        void OnAnotherOption()
        {
        m_bOptionSelected = TRUE; // turn on check for "option"

        // ... all other processing for AnotherOption
        

        }

        void OnUpdateOption(CCmdUI * pCmdUI)
        {
        // This line "checks" Option if the flag is marked, or clears the
        // check if not.
        pCmdUI->SetCheck( m_bOptionSelected ? BST_CHECKED :
        BST_UNCHECKED);

        pCmdUI->Enable(TRUE);
        

        }

        void OnUpdateAnotherOption(CCmdUI * pCmdUI)
        {
        // Reverse: "uncheck" AnotherOption if Option is checked, check
        // it if not.
        pCmdUI->SetCheck( m_bOptionSelected ? BST_UNCHECKED :
        BST_CHECKED);

        pCmdUI->Enable(TRUE);
        

        }

        Hope it helps. Bob Ciora

        L Offline
        L Offline
        lordgreg
        wrote on last edited by
        #3

        hi Bob, sure it helped- alot. thanks for informing me about ON_UPDATE message handlers. thanks again and best regards ;)

        1 Reply Last reply
        0
        • L lordgreg

          hi all,.. i'm creating a Doc/View program (single window). this program also has a menu (IDR_MAINFRAME), which looks like this: [ my_program ] [ some option ] [ 2nd option ] [ submenu ] |- [ option_to_check ] '- [ another_option_to_check ] [ exit_option ] and, i created a handler which runs when i click on "another_option_to_check". when i do that, i would like to UNCHECK "option_to_check" and CHECK another_option_to_check or vice versa if 2nd option is checked. i tried to do it like this: void CVsebnostniTestView::OnMenuRisanjeTocke() { // TODO: Add your command handler code here CMenu* mmenu; mmenu = GetMenu(); if( mmenu->GetMenuState(ID_RISANJE_MNOGOKOTNIK, MF_CHECKED) ) MessageBox( "Option checked!" ); else MessageBox( "Option unchecked!" ); } ID_RISANJE_MNOGOKOTNIK = id from option_to_check option in menu. i also tried with CMenu* mmenu, and then submenu(3) etc. and even that didn't work. what am i doing wrong then? i would really apretiate any help offered. thanks and best regards ;)

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          lordgreg wrote:

          if( mmenu->GetMenuState(ID_RISANJE_MNOGOKOTNIK, MF_CHECKED) )

          Shouldn't this be:

          if (mmenu->GetMenuState(ID_RISANJE_MNOGOKOTNIK, MF_BYCOMMAND) == MF_CHECKED)

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups