在Visual Studio 2008 Feature Pack中使用CMFCMenuBar时如何禁用/启用菜单项 [英] How to disable/enable menu items when using CMFCMenuBar in Visual Studio 2008 Feature Pack

查看:130
本文介绍了在Visual Studio 2008 Feature Pack中使用CMFCMenuBar时如何禁用/启用菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用以下方法禁用菜单项时,只能禁用弹出菜单项,顶级菜单和其他子菜单似乎什么都不做!


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);




在VS 2005中使用CMenu可以使用此方法.

我听说可以使用UPDATE_COMMAND_UI解决我的问题,但我仍然没有头绪.

感谢您的帮助!




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!

推荐答案

写道:​​

我听说我可以使用UPDATE_COMMAND_UI解决我的问题,但是我仍然没有头绪.

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



是的,您可以使用UPDATE_COMMAND_UI解决您的问题. :)
当菜单将要显示时,MFC查询所有UPDATE_COMMAND_UI处理程序.

您可以使用VS IDE生成此更新消息,也可以自己编写.

例如,如果要启用/禁用类中的 Window/New Window 菜单项,则必须:

1)添加afx_msg void OnUpdateWindowNew(CCmdUI *pCmdUI);在 MainFrm.h ;

2)在CMainFrame的按摩图中添加ON_UPDATE_COMMAND_UI(ID_WINDOW_NEW, &CMainFrame::OnUpdateWindowNew).

3)提供OnUpdateWindowNew(CCmdUI *pCmdUI)处理程序的实现.此实现可能类似于:



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);
}


这只是一个简单的示例,但我希望它对您有用.

问候,
努里·伊斯梅尔(Nuri Ismail)


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

Regards,
Nuri Ismail


嗨Blade_Bao,

ID_WINDOW_NEW的内容仅是如何使用UPDATE_COMMAND_UI的示例. :)

我希望您将使用此示例并根据需要实现自己的UPDATE_COMMAND_UI处理程序.但是,如果您需要使用以下示例:
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


你会得到这样的例子. :)

默认情况下,VS IDE向导将为您生成一个项目,其中 File/New File/Open 命令由您的应用程序类的父级处理(在您的情况下,由CWinAppEx).
因此,假设您的应用程序类为CTestApp(这是示例名称,您必须使用应用程序类的名称更改此名称).您必须找到:
您的应用程序类的消息映射中的


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)



之后,将BOOL m_bEnabled;成员添加到您的应用程序类,并在构造函数中将该成员作为TRUE初始化.
afx_msg void 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();
}



现在,您可以将 Copy Cut UPDATE_COMMAND_UI处理程序添加到应用程序类的消息映射中:



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);
}



现在,单击文件/打开之后,您会注意到复制剪切菜单项将被禁用.您还应该知道,在上述情况下,没有禁用该菜单项的机制.

您可以使用IDE生成UPDATE_COMMAND_UI处理程序.请调查我为您提供的示例,并尝试使用它们来解决您的问题. :)

问候,
Nuri Ismail



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


致Nuri Ismail:

但是我的程序基于SDI,因此将只创建一个窗口.使用ID_WINDOW_NEW无法解决我的问题.

我要做的是按一个菜单项然后禁用其他菜单项.

例如:
按[File]-> [open],然后[edit]-> [cut]和[edit]-> [copy]将被禁用

那你能帮我吗?

非常感谢!
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!


这篇关于在Visual Studio 2008 Feature Pack中使用CMFCMenuBar时如何禁用/启用菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆