MFC Menu checked on Doc/View app?
-
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 ;) -
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 ;)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
-
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
-
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 ;)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