[英]MFC Change CMFCToolBar button to Toggle instead of press/release?
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).我在网上找到一篇文章说将工具栏按钮设置为保持按下状态的类型,您只需在按钮上设置一个样式
TBBS_CHECKBOX
但它对我不起作用(它仍然像普通按钮一样工作)。 I confirmed the style is set, just after created and the SetWindowText()
MFC wizard setup of CMainFrame::OnCreate()
.我确认样式已设置,就在创建和
CMainFrame::OnCreate()
的SetWindowText()
MFC 向导设置之后。 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);
}
}
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.
一个命令 ID 可以用在多个 UI 项中,例如一个菜单项和一个工具栏按钮。 A handler affects all items with the same ID, so you don't need a separate one for each item.
处理程序会影响具有相同 ID 的所有项目,因此您不需要为每个项目单独设置一个。 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.
除了启用/禁用之外, CCmdUI Class还提供了可以使菜单或工具栏按钮等 UI 项目充当按钮、复选框或单选按钮的方法。
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:
您应该在文档 class 中定义一个 boolean 变量:
BOOL m_bFilterData = FALSE;
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):然后是带有过滤器图片(也可能是菜单项)的工具栏按钮的
ON_COMMAND
和ON_UPDATE_COMMAND_UI
处理程序:
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);
}
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):现在,如果过滤器选项是按视图实例化的,即每个视图都可以单独过滤或不过滤,则上述必须 go 到您的视图类(-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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.