启用MFC功能包的功能区按钮

启用MFC功能包的功能区按钮

本文介绍了禁用/启用MFC功能包的功能区按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MFC功能包,我在功能区栏上有一些按钮,CMFCRibbonButton的实例。问题是,我想在特定条件下,但在运行时启用和禁用其中的一些。我如何做到这一点?因为没有具体的方法...我听说一个解决方案是在运行时附加/分离事件处理程序,但我不知道如何...

对象时,必须指定相关的命令ID(请参阅的文档) > CMFCRibbonButton 构造函数)。然后,使用MFC中的常用命令更新机制完成功能区按钮的启用和禁用,使用类。



例如,如果您有命令ID为 ID_MYCOMMAND ,并且您希望在应用程序的视图类中处理此命令,应该将这些函数添加到类中:

  // MyView.h 
class CMyView:public CView {
// ...
private:
afx_msg void OnMyCommand();
afx_msg void OnUpdateMyCommand(CCmdUI * pCmdUI);
DECLARE_MESSAGE_MAP()
};

并在.cpp文件中实现它们:

  // MyView.cpp 
void CMyView :: OnMyCommand(){
//添加命令处理程序代码。
}

void CMyView :: OnUpdateMyCommand(CCmdUI * pCmdUI){
BOOL enable = ...; //设置标志以启用或禁用命令。
pCmdUI->启用(启用);
}

您还应添加 ON_COMMAND 类的消息映射中的和 ON_UPDATE_COMMAND_UI 条目:

  // MyView.cpp 
BEGIN_MESSAGE_MAP(CMyView,CView)
ON_COMMAND(ID_MYCOMMAND,& CMyView :: OnMyCommand)
ON_UPDATE_COMMAND_UI ID_MYCOMMAND,& CMyView :: OnUpdateMyCommand)
END_MESSAGE_MAP()

MFC中的地图,请参阅MSDN中的。



我希望这有助于!


I am using the MFC Feature Pack and I have some buttons on a ribbon bar, instances of CMFCRibbonButton. The problem is that I would like to enable and disable some of them in certain conditions, but at runtime. How can I do this? because there is no specific method for this...I heard that a solution would be to attach/detach the event handlers at runtime, but I do not know how...

解决方案

When you create the CMFCRibbonButton object you have to specify the associated command ID (see the documentation for the CMFCRibbonButton constructor here). Enabling and disabling of ribbon buttons is then done using the usual command update mechanism in MFC, using the CCmdUI class.

For example, if you have a ribbon button whose command ID is ID_MYCOMMAND and you want to handle this command in your application's view class, you should add these functions to the class:

// MyView.h
class CMyView : public CView {
    // ...
    private:
        afx_msg void OnMyCommand();
        afx_msg void OnUpdateMyCommand(CCmdUI* pCmdUI);
        DECLARE_MESSAGE_MAP()
};

and implement them in the .cpp file:

// MyView.cpp
void CMyView::OnMyCommand() {
    // add command handler code.
}

void CMyView::OnUpdateMyCommand(CCmdUI* pCmdUI) {
    BOOL enable = ...; // set flag to enable or disable the command.
    pCmdUI->Enable(enable);
}

You should also add ON_COMMAND and ON_UPDATE_COMMAND_UI entries to the message map for the CMyView class:

// MyView.cpp
BEGIN_MESSAGE_MAP(CMyView, CView)
    ON_COMMAND(ID_MYCOMMAND, &CMyView::OnMyCommand)
    ON_UPDATE_COMMAND_UI(ID_MYCOMMAND, &CMyView::OnUpdateMyCommand)
END_MESSAGE_MAP()

For more information on message maps in MFC, refer to TN006: Message Maps in MSDN.

I hope this helps!

这篇关于禁用/启用MFC功能包的功能区按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:13