If menu item x selected?
Mario Contestabile -- Mario_Contestabile.UOS__MTL@UOSMTL2.universal.com
Friday, December 29, 1995
To determine if a certain menu item is checked or not I use the following
method:
// Let's just display the item name to see if we are selecting the right one
CString x;
AfxGetApp()->m_pMainWnd->GetMenu()->GetSubMenu(1)->GetMenuString(3, x,
MF_BYPOSITION);
AfxMessageBox(x);
AfxGetApp()->m_pMainWnd->GetMenu()->GetSubMenu(1)->GetMenuString(ID_VIEW_FILES,
x, MF_BYCOMMAND);
AfxMessageBox(x);
// we are
UINT state =
AfxGetApp()->m_pMainWnd->GetMenu()->GetSubMenu(1)->GetMenuState(3,
MF_BYPOSITION);
if(state==-1)AfxMessageBox(_T("error"));
if(state==MF_CHECKED) AfxMessageBox(_T("CHECKED"));
if(state==MF_UNCHECKED) AfxMessageBox(_T("UNCHECKED"));
state =
AfxGetApp()->m_pMainWnd->GetMenu()->GetSubMenu(1)->GetMenuState(ID_VIEW_FILES,
MF_BYCOMMAND);
if(state==-1)AfxMessageBox(_T("error"));
if(state==MF_CHECKED) AfxMessageBox(_T("CHECKED"));
if(state==MF_UNCHECKED) AfxMessageBox(_T("UNCHECKED"));
If I open a new document, and the item is checked, state wil equal "unchecked".
Why?
mcontest@universal.com
John & Annette Elsbree -- elsbree@msn.com
Sunday, December 31, 1995
Mario -
I'm not sure why the state is equal to MF_UNCHECKED in this case. In general,
however, comparing against MF_UNCHECKED is not likely to produce the results
you expect, since its value is 0.
I would recommend a using an & test with MF_CHECKED, rather than two == tests:
if (state & MF_CHECKED)
AfxMessageBox(_T("CHECKED"));
else
AfxMessageBox(_T("UNCHECKED"));
There could be other menu flags set, but you don't care about them -- and
don't want them to influence your results -- when you're simply testing
whether the item is checked.
mfcTeam.m_johnels; // does not represent Microsoft
----------
From: owner-mfc-l@netcom.com on behalf of Mario Contestabile
To determine if a certain menu item is checked or not I use the following
method:
UINT state =
AfxGetApp()->m_pMainWnd->GetMenu()->GetSubMenu(1)->GetMenuState(3,
MF_BYPOSITION);
if(state==-1)AfxMessageBox(_T("error"));
if(state==MF_CHECKED) AfxMessageBox(_T("CHECKED"));
if(state==MF_UNCHECKED) AfxMessageBox(_T("UNCHECKED"));
state =
AfxGetApp()->m_pMainWnd->GetMenu()->GetSubMenu(1)->GetMenuState(ID_VIEW_FILES,
MF_BYCOMMAND);
if(state==-1)AfxMessageBox(_T("error"));
if(state==MF_CHECKED) AfxMessageBox(_T("CHECKED"));
if(state==MF_UNCHECKED) AfxMessageBox(_T("UNCHECKED"));
If I open a new document, and the item is checked, state wil equal
"unchecked".
Brad Wilson -- bradw@netnet.net
Tuesday, January 02, 1996
>> To determine if a certain menu item is checked or not I use the following
>> method:
MFC uses Command UI handlers to check and uncheck menu items. The menu
items are not checked or unchecked until the WM_INITMENU message is
delivered (or WM_INITMENUPOPUP), which results in the slew of calls
to the command UI handlers.
In order to see if the item is checked, you need to make sure that the menu
has been shown so that the command UI handlers have been called.
I would recommend looking for another way to determine if the menu item
has been checked (ie, look in the command handler for the menu item ...
how does it decide when to check/uncheck the menu item?)
Brad
Fredrik Gunne -- fge@ada.agema.se
Wednesday, January 03, 1996
A neat way to do it would be to simulate that the menu is to be drawn.
I don't know exactly how this can be done, but I'm sure it's possible.
CFrameWnd::OnINitMenuPopup could be a place to start. It creates a CCmdUI for
each menu item, calls CCmdUI::DoUpdate which calls CFrameWnd::OnCmdMsg. In other
words, the CCmdUI goes the same way a WM_COMMAND goes, i.e
CFrameWnd->ActiveView->Document
->CWnd
->CApp
I think that the CCmdUI, when it comes back from DoUpdate, will have some flags set.
Unfortuanetly, they are not documented and should thus not be used.
Instead, subclass a CCmdUI with the appropriate member functions overridden
(probably Enable or SetCheck). In these functions, set flags that can be accessed
by member functions, such as CMyCmdUI::IsEnabled.
Then "all you need to do" is to do what OnOnitMenuPopup does...
Fredrik
| Вернуться в корень Архива
|