CMFCMenuButton的示例代码? [英] Example code for CMFCMenuButton?

查看:408
本文介绍了CMFCMenuButton的示例代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于新手问题很抱歉,但是没有人能指出示例代码说明CMFCMenuButton的用法吗? Microsoft帮助指的是"New Controls示例",但是这些示例似乎位于Visual Studio 2008的功能包"中,并且由于我运行的是VS 2013并且没有VS 2008,因此拒绝在我的系统上安装.我还没有找到作为独立代码的示例. 具体来说,我有一个对话框,其中有一个标有保存"的按钮,带有全部保存"和保存可见"(默认为全部保存")的下拉选项.但是任何有效的代码至少都可以帮助我入门.

Sorry for the newbie question, but can anyone point me at sample code that illustrates the use of the CMFCMenuButton? The Microsoft help refers to "New Controls samples", but these samples seem to be in the Visual Studio 2008 "Feature Pack", and this refuses to install on my system since I'm running VS 2013 and don't have VS 2008. I haven't been able to find the samples as stand-alone code. To be specific, I have a dialog bar in which I want a button labelled Save with drop-down options of Save All and Save Visible (with Save All the default). But any working code would at least get me started.

推荐答案

声明数据成员:

CMFCMenuButton m_button_menu;
CMenu m_menu;

还将按钮的ID添加到消息映射和数据交换中:

Also add the button's id to message map and data exchange:

BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx)
    ON_BN_CLICKED(IDC_MFCMENUBUTTON1, OnButtonMenu)
    ...
END_MESSAGE_MAP

void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_MFCMENUBUTTON1, m_button_menu);
} 

定义:

BOOL CMyDialog::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    //...
    m_menu.LoadMenu(IDR_MENU1);
    m_button_menu.m_hMenu = m_menu.GetSubMenu(0)->GetSafeHmenu();

    return TRUE;  
}

其中IDR_MENU1是常规菜单栏,我们得到它的第一个子菜单.例如:

Where IDR_MENU1 is a regular menu bar and we get its first submenu. For example:

IDR_MENU1 MENU
BEGIN
    POPUP "Dummy"
    BEGIN
        MENUITEM "&Item1", ID_FILE_ITEM1
        MENUITEM "&Item2", ID_FILE_ITEM2
    END
END

如果单击按钮的下拉箭头,则会出现一个弹出菜单,菜单结果将传递到OnButtonMenu.如果单击按钮的左侧,则会直接调用OnButtonMenu,而不会显示弹出菜单.

If button's drop-down arrow is clicked, a popup menu appears, menu result is passed to OnButtonMenu. If left side of button is clicked, then OnButtonMenu is called directly, without showing a popup menu.

void CMyDialog::OnButtonMenu()
{
    CString str;
    switch (m_button_menu.m_nMenuResult)
    {
    case ID_FILE_ITEM1:
        str = L"first menu item clicked";
        break;
    case ID_FILE_ITEM2:
        str = L"second menu item clicked";
        break;
    default:
        str = L"Button click (popup menu did not appear, or menu ID is not handled)";
        break;
    }
    MessageBox(str);
}

**在使用停靠控件,对话框栏等时.MFC可能运行自己的子类,我不认为会调用DoDataExchange. m_button_menu可能无效. GetDlgItem可用于查找正确的指针:

** When working with docking controls, dialog bars, etc. MFC may run its own subclass, I don't think DoDataExchange gets called. m_button_menu could be invalid. GetDlgItem can be used to find the correct pointer:

CMFCMenuButton* CMyDlgBar::GetButtonMenu()
{
    CMFCMenuButton* pButton = &m_button_menu;
    if (!IsWindow(pButton->m_hWnd))
        pButton = (CMFCMenuButton*)GetDlgItem(IDC_MFCMENUBUTTON1);
    return pButton;
}

在其他任何地方,我们都使用GetButtonMenu()而不是m_button_menu.例如:

Everywhere else we use GetButtonMenu() instead of m_button_menu. For example:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    //...
    m_dlgbar.Create(...);
    m_dlgbar.m_menu.LoadMenu(IDR_MENU1);
    m_dlgbar.GetButtonMenu()->m_hMenu = m_dlgbar.m_menu.GetSubMenu(0)->GetSafeHmenu();

    return 0;
}

void CMainFrame::OnButtonMenu()
{
    CString str;
    switch (GetButtonMenu()->m_nMenuResult)
    ...
}

如果没有显示下拉箭头怎么办?

然后在此处阅读答案,解释对RC文件所需的更改.

What if the Drop-Down Arrow does not show?

Then read the answer here that explains the changes needed to your RC file.

这篇关于CMFCMenuButton的示例代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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