Click here to Skip to main content
15,914,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I use the following method to disable menu items, only popup ones can be disabled, the top-level menus and other sub-menus seems to do nothing!


CMenu* menu = CMenu::FromHandle(m_wndMenuBar.GetDefaultMenu());
menu->GetSubMenu(1)->EnableMenuItem(1, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);



This method is OK with CMenu in VS 2005.

I've heard that I can solve my problems by using UPDATE_COMMAND_UI, but I still haven't got a clue.

Thanks for the helps!
Posted

wrote:
I've heard that I can solve my problems by using UPDATE_COMMAND_UI, but I still haven't got a clue.


Yes, you can solve your problems by using UPDATE_COMMAND_UI. :)
MFC queries all the UPDATE_COMMAND_UI handlers when the menu is about to be shown.

You can generate this update massages by using the VS IDE or you can write them yourself.

For example, if you want to enable/disable a Window/New Window menu item from CMainFrame class, you have to:

1) Add afx_msg void OnUpdateWindowNew(CCmdUI *pCmdUI); in MainFrm.h;

2) Add ON_UPDATE_COMMAND_UI(ID_WINDOW_NEW, &CMainFrame::OnUpdateWindowNew) in the massage map of CMainFrame.

3) Provide an implementation for OnUpdateWindowNew(CCmdUI *pCmdUI) handler. This implementation could be something like:

void CMainFrame::OnUpdateWindowNew(CCmdUI *pCmdUI)
{
        // Here m_bEnabled is a boolean member 
        // of CMainFrame which determines whether 
        // the menu item have to be enabled or not.
	pCmdUI->Enable(m_bEnabled);
}

This is just a simple example, but I hope that it could be useful for you.

Regards,
Nuri Ismail
 
Share this answer
 
Hi Blade_Bao,

This stuff with ID_WINDOW_NEW was just an example how to use UPDATE_COMMAND_UI. :)

My hope was that you will use this example and implement your own UPDATE_COMMAND_UI handlers for your needs. But if you need an example with:
press [File]->[open], then [edit]->[cut] & [edit]->[copy] will be disabled

you will get such an example. :)

By default the VS IDE wizard will generate you a project where the File/New and File/Open commands are handled by the parent of your application class (in your case by CWinAppEx).
So lets assume that your application class is CTestApp (this is an example name, you have to change this name with the name of your application class). You have to find the:
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, &CWinAppEx::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
lines in the message map of your application class, and replace this lines with:
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, &CWinAppEx::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, &CTestApp::OnFileOpen)


After that add the BOOL m_bEnabled; member to your application class and init this member as TRUE in the constructor.

Add the afx_msg void OnFileOpen(); in you application class (int he header file) and provide implementation for this handler. The implementation could be something like:
void CTestApp::OnFileOpen()
{
	m_bEnabled = FALSE;
	CWinAppEx::OnFileOpen();
}


Now you can add the UPDATE_COMMAND_UI handlers for Copy and Cut to the message map of your application class:
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, &CTestApp::OnUpdateEditCopy)
ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, &CTestApp::OnUpdateEditCut)


In you application class header add:
afx_msg void OnUpdateEditCopy(CCmdUI *pCmdUI);
afx_msg void OnUpdateEditCut(CCmdUI *pCmdUI);


And provide implementations for this handlers:
void CTestApp::OnUpdateEditCopy(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_bEnabled);
}

void CTestApp::OnUpdateEditCut(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_bEnabled);
}


And now, after clicking the File/Open you will notice that your Copy and Cut menu items will be disabled. Also you should know that in the above scenario there is no mechanism to enable this menu items once they are disabled.

You can generate the UPDATE_COMMAND_UI handlers by using your IDE. Please investigate the examples that I provided for you and try to solve your problem by using them. :)

Regards,
Nuri Ismail
 
Share this answer
 
v3
To Nuri Ismail:

But my programe is based on SDI, so only one window will be created. Using ID_WINDOW_NEW can not solve my problem.

What I want to do is to press one menu item then diable some of others.

eg:
press [File]->[open], then [edit]->[cut] & [edit]->[copy] will be disabled

So can you help me with that?

Thank you so much!
 
Share this answer
 
Hi Nuri Ismail:

What you said really helps me a lot. Thanks so much for your help. :)

Cheers!

Blade Bao
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900