按钮更改为切换而不是按下

按钮更改为切换而不是按下

本文介绍了MFC 将 CMFCToolBar 按钮更改为切换而不是按下/释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找到了一篇文章,说要将工具栏按钮设置为保持按下的类型,您只需在按钮上设置样式 TBBS_CHECKBOX 但它对我不起作用(它仍然有效)就像一个普通的按钮).我确认样式已设置,就在创建和 CMainFrame::OnCreate()SetWindowText() MFC 向导设置之后.我做错了什么?

I found an article online that said to setup the toolbar button to be a type that stays pressed you just set a style TBBS_CHECKBOX on the button but it doesn't work for me (it still acts like a normal button). I confirmed the style is set, just after created and the SetWindowText() MFC wizard setup of CMainFrame::OnCreate(). What am I doing wrong?

 for (int i=0; ; i++) {
    int id=m_wndToolBar.GetItemID(i);
    if (id==0) {
      break;
    }
    if (id == ID_THE_ID) {
      m_wndToolBar.SetButtonStyle(i, TBBS_CHECKBOX);
    }
  }

推荐答案

使用 命令处理程序 是此处推荐的实现.命令 ID 可用于多个 UI 项,例如菜单项和工具栏按钮.处理程序影响具有相同 ID 的所有项目,因此您不需要为每个项目单独设置一个.CCmdUI 类 提供的方法除了启用/禁用外,还可以使菜单或工具栏按钮等 UI 项目表现为按钮、复选框或单选按钮.

Using Command Handlers is the recommended implementation here. A command ID may be used in multiple UI items, eg a menu item and a toolbar button. A handler affects all items with the same ID, so you don't need a separate one for each item. The CCmdUI Class provides methods that can cause UI items like menus or toolbar buttons to behave as push-buttons, check-boxes or radio-buttons, in addition to enabling/disabling.

在您的示例中,假设是否过滤选项是在每个文档的基础上实例化的,即文档的所有视图都将同时被过滤或未过滤.您应该在文档类中定义一个布尔变量:

In your example, suppose that the option whether to filter is instantiated on a per document basis, ie all views of the document would be filtered or non-filtered, all at the same time. You should define a boolean variable in your document class:

BOOL m_bFilterData = FALSE;

然后是带有过滤器图片(可能还有菜单项)的工具栏按钮的 ON_COMMANDON_UPDATE_COMMAND_UI 处理程序:

Then the ON_COMMAND and ON_UPDATE_COMMAND_UI handlers for the toolbar button with the Filter pic (and possibly a menu item as well):

BEGIN_MESSAGE_MAP(CMyDoc, CDocument)
  .
  .
    ON_COMMAND(ID_VIEW_FILTERDATA, OnViewFilterData)
    ON_UPDATE_COMMAND_UI(ID_VIEW_FILTERDATA, OnUpdateViewFilterData)
  .
  .
END_MESSAGE_MAP()

void CMyDoc::OnViewFilterData()
{
    // Toggle filtered state
    m_bFilterData = !m_bFilterData;
    // Tell all views to refresh - You can limit this using the lHint/pHint params
    UpdateAllViews(NULL, 0L, NULL);
}

void CMyDoc::OnUpdateViewFilterData(CCmdUI* pCmdUI)
{
    // Enable/Disable as needed
    pCmdUI->Enable(m_nTotalItems>0);
    // Show pressed/checked if data filtered
    pCmdUI->SetCheck(m_bFilterData);
}

现在,如果过滤器选项是针对每个视图实例化的,即每个视图都可以独立过滤或不过滤,则上述内容必须转到您的视图类(-es):

Now, if the filter option is instantiated per view, ie each view can indpendently be filtered or non-filtered, the above must go to your view class(-es):

void CMyView::OnViewFilterData()
{
    // Toggle filtered state
    m_bFilterData = !m_bFilterData;
    // Refresh this view only
    .
    .
}

void CMyView::OnUpdateViewFilterData(CCmdUI* pCmdUI)
{
    // Enable/Disable as needed
    pCmdUI->Enable(GetDocument()->m_nTotalItems > 0);
    // Show pressed/checked if data filtered
    pCmdUI->SetCheck(m_bFilterData);
}

这篇关于MFC 将 CMFCToolBar 按钮更改为切换而不是按下/释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:22