转自:http://blog.csdn.net/abesky/archive/2009/08/14/4446863.aspx#
首先怎样弹出界面库的菜单呢?
首先模块向主窗口发出弹出菜单请求,然后在主程序中收到请求后向发出请求模块询问菜单,模块加载菜单返回然后由主窗口调用函数弹出菜单。
//定义查询菜单的消息
const UINT WM_GETCONTEXTMENU = ::RegisterWindowMessage(_T("GetContextMenu"));
//模块需要弹出菜单时向主窗口发送请求
::SendMessage(hMainWnd, WM_CONTEXTMENU, (WPARAM)m_hWnd,
MAKELPARAM(point.x, point.y));
//主窗口响应处理
BEGIN_MESSAGE_MAP(CMainFrame, CMDIChildWndEx)
![](http://www.cppblog.com/Images/dot.gif)
![](http://www.cppblog.com/Images/dot.gif)
..
ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()
void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint point)
{
HMENU hMenu(0);
if (pWnd)
{
hMenu = (HMENU)pWnd->SendMessage(WM_GETCONTEXTMENU);
}
if (hMenu)
{
theApp.GetContextMenuManager()->ShowPopupMenu(
hMenu, point.x, point.y, pWnd, TRUE);//注意此处参数,否则模块无法处理命令
}
//以下可根据情况注释掉
else
{
CMFCPopupMenu* pMenuActive = CMFCPopupMenu::GetActiveMenu();
if (pMenuActive != NULL)
{
pMenuActive->SendMessage(WM_CLOSE);
}
}
}
//模块收到主窗口菜单询问加载菜单
BEGIN_MESSAGE_MAP(CXXXXDlg, CDialog)
![](http://www.cppblog.com/Images/dot.gif)
![](http://www.cppblog.com/Images/dot.gif)
![](http://www.cppblog.com/Images/dot.gif)
ON_REGISTERED_MESSAGE(WM_GETCONTEXTMENU, &CXXXXDlg::OnGetContextMenu)
END_MESSAGE_MAP()
LRESULT CXXXXDlg::OnGetContextMenu(WPARAM wParam, LPARAM lParam)
{
HMENU hMenu = ::LoadMenu(
..);
return (LRESULT)hMenu;
}
BCGControlBar以及VC2008FeaturePack在菜单中添加图标一般来说需要在资源里添加一个专门的Toolbar工具条资源 然后调用AddToolBarForImageCollection这个方法来加载图标,此种方法适合资源都在主工程时使用。
如果是其他模块请求主程序弹出菜单时这时候怎么增加菜单图标?
我们可以利用在菜单中增加自定义的数据来保存图标,当然也可以直接设置菜单图标,这里我只以在菜单使用自定义数据为例子。
//在发送弹出菜单请求时设置好菜单的图标
int nCount = GetMenuItemCount(m_hContext);
MENUITEMINFO menuInfo;
menuInfo.cbSize = sizeof MENUITEMINFO;
menuInfo.wID = 0;
menuInfo.fMask = MIIM_ID | MIIM_DATA;
for (int i(0); i < nCount; i++)
{
::GetMenuItemInfo(m_hContext, i, TRUE, &menuInfo);
menuInfo.dwItemData = (ULONG_PTR)m_mMeunIcon[menuInfo.wID];
SetMenuItemInfo(m_hContext, i, TRUE, &menuInfo);
}
ok图标已经设置好了,然后需要在CMainframe中增加一个虚函数的实现即可
函数原型是
virtual BOOL OnDrawMenuImage ( CDC* pDC,
const CMFCToolBarMenuButton* pMenuButton,
const CRect& rectImage);
实现:
BOOL CMainFrame::OnDrawMenuImage ( CDC* pDC,
const CMFCToolBarMenuButton* pMenuButton,
const CRect& rectImage)
{
ASSERT_VALID (pDC);
ASSERT_VALID (pMenuButton);
if (pMenuButton->m_dwdItemData)
{
CRect rc = rectImage;
rc.DeflateRect(2, 3);
::DrawIconEx(pDC->m_hDC, rc.left, rc.top, (HICON)pMenuButton->m_dwdItemData,
rc.Width(), rc.Height(), 0, NULL, DI_NORMAL);
return TRUE;
}
return FALSE;
} 以上代码为VC2008FeaturePack环境,在bcg环境下类似只需要替换相应的函数即可
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/abesky/archive/2009/08/14/4446863.aspx#