MFC中如何在工具条动态增加菜单

发布于:2024-12-06 ⋅ 阅读:(55) ⋅ 点赞:(0)

在C:\temp\VCSamples-master\VC2010Samples\MFC\Visual C++ 2008 Feature Pack\WordPad
这个例子中倒是有在工具条上动态增加菜单的方法,但有个缺陷,必须预先将需要的按钮定死。现将方法总结如下:
效果如下:点击前:在这里插入图片描述
点击后:在这里插入图片描述
响应每个按钮的函数为:

ON_COMMAND_RANGE(ID_BORDER_1, ID_BORDER_13, OnBorderType)
ON_UPDATE_COMMAND_UI_RANGE(ID_BORDER_1, ID_BORDER_13, OnUpdateBorderType)
void CWordPadView::OnBorderType (UINT id)
{
m_nBorderType = id;
MessageBox (_T("Add your code here..."));
}

创建的地方的代码:在void CFormatBar::OnReset ()函数中,此函数为CMFCToolBar的虚函数,在创建bar的时候就会调用

// Insert border type button:
CMFCToolBarMenuButton* pBorderTypeButton = CreateBorderTypeButton ();
ReplaceButton (ID_BORDER_1, *pBorderTypeButton);
delete pBorderTypeButton;

在原有的toolbar上有一个按钮ID为ID_BORDER_1,这里就将其替换为menubar
CreateBorderTypeButton此函数为具体实施函数:

CMFCToolBarMenuButton* CFormatBar::CreateBorderTypeButton ()
{
CMenu menu;
VERIFY(menu.LoadMenu (IDR_BORDER_PALETTE));
CMFCToolBarMenuButton* pBorderType = NULL;
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
if (pPopup != NULL)
{
    pBorderType = new CMFCToolBarMenuButton (ID_BORDER_1, pPopup->GetSafeHmenu (), GetCmdMgr ()->GetCmdImage  (ID_BORDER_1, FALSE), _T("Borders"));
    pBorderType->SetMenuPaletteMode (TRUE, 2 /* Rows number */);
    pBorderType->SetTearOff (ID_BORDER_TEAROFF);
}
return pBorderType;
}

toolbar的图像却来源于另外一个工具条:ID_BORDER_1
长这样:
在这里插入图片描述
网上解释这个函数是为仅在菜单里显示的命令指定图标。
当这个图标是怎么到了菜单上的,我没搞明白
明明这里:pPopup->GetSafeHmenu (), GetCmdMgr ()->GetCmdImage (ID_BORDER_1, FALSE)
获取的image是ID_BORDER_1,这个IDR_BORDER_TYPE是怎么弄上去的。


网站公告

今日签到

点亮在社区的每一天
去签到

热门文章