如何手动显示CMFCToolBarComboBoxButton子菜单? [英] How to manually show CMFCToolBarComboBoxButton sub-menu?

查看:63
本文介绍了如何手动显示CMFCToolBarComboBoxButton子菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<代码的标准行为> CMFCToolBarComboBoxButton 将具有一个可单击的按钮以及一个用于显示子菜单的下拉箭头.我想独立于单击位置显示子菜单.我该怎么办?

Standard behaviour for CMFCToolBarComboBoxButton is to have a clickable button plus a drop-down arrow for displaying a submenu. I want to show the submenu independently of where the click was made. How can I do it?

我创建按钮的代码大致如下(它是从一个较大的项目中提取出来的,因此,对于任何缺少但不太重要的代码,我深表歉意):

My code to create the button is, more or less, the following (it has been extracted from a larger project, so I apologize for any missing not-too-important piece of code):

// In class declaration:
CMenu m_menu;
CMFCToolBar m_toolbar;

// Where toolbar initialization takes place:
m_menu.CreateMenu();
// ... populate menu

// ID_BUTTON is the ID in the resource file for the toolbar button, 0 is the index for the button icon
CMFCToolBarMenuButton button(ID_BUTTON, m_menu.GetSafeHmenu(), 0);
m_toolbar.ReplaceButton(ID_BUTTON, button);

我已经逛了一会儿了,找不到相关的答案.

I've been looking around for awhile and cannot find a related answer.

推荐答案

该解决方案非常简单,只需调用

The solution happened to be very straightforward, just call the OnClick function of the CMFCToolBarComboBoxButton button from its associated ON_COMMAND.

// ... message map
ON_COMMAND(ID_BUTTON, OnToolbarMenuButtonClicked)
// ...

void MyWnd::OnToolbarMenuButtonClicked()
{
  const int index = m_toolbar.CommandToIndex(ID_BUTTON);
  auto button = (CMFCToolBarComboBoxButton*)m_toolbar.GetButton(index);
  button->OnClick(NULL, TRUE);
}

此行为没有记录,并且与常识告诉我的相反,它不会创建无限递归调用.似乎主"按钮仍由 CMFCToolBarButton 控制,而只有箭头按钮"由 CMFCToolBarComboBoxButton 控制.

This behaviour is not documented and, contrary to what common sense told me, it doesn't create an infinite recursive call. It seems that the "main" button is still controlled by CMFCToolBarButton, while just the "arrow-button" is controlled by the CMFCToolBarComboBoxButton.

PS:显然,在问题范围之外, OnToolbarMenuButtonClicked 可以用于非常不同的用途,例如默认操作,而子菜单包含其他不常用的选项

PS: obviously, and out of the scope of the question, the OnToolbarMenuButtonClicked can be used for a very different purpose, such as the default action while the sub-menu contains other less-frequent options.

这篇关于如何手动显示CMFCToolBarComboBoxButton子菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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