VC中使用CMenuSpawn类向菜单项目添加图标

Step 1: including files

We need to include the CSpawnMenu class in our application and the Bitmap resource named IDB_MENUCHK from the sample application which comes along (it will be used to draw "check marks" aside menu item which need it - you can freely change it).  添加一个Bmp向资源中ID为IDB_MENUCHK .

Step 2: initializing the CSpawnClass 初始化CSpawn类在MainFrame中.
public:
 CMenuSpawn cSpawn;
Step3:添加消息映射:
 afx_msg void OnInitMenu(CMenu* pMenu);
 afx_msg void OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu);
 afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
 afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
 //{{AFX_MSG_MAP(CMainFrame)
 ON_WM_CREATE()
 ON_WM_INITMENU()
 ON_WM_INITMENUPOPUP()
 ON_WM_DRAWITEM()
 ON_WM_MEASUREITEM()

 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

step4.实现消息函数调用自画类:
void CMainFrame::OnInitMenu(CMenu* pMenu)
{
 CMDIFrameWnd::OnInitMenu(pMenu);
 
 // TODO: Add your message handler code here
  cSpawn.RemapMenu(pMenu);
 
}

void CMainFrame::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
 CMDIFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
 
 // TODO: Add your message handler code here
 cSpawn.RemapMenu(pPopupMenu);
}
void CMainFrame::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
 if (!cSpawn.DrawItem(lpDrawItemStruct))
  CFrameWnd::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

void CMainFrame::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
 if (!cSpawn.MeasureItem(lpMeasureItemStruct))
  CFrameWnd::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}

Step5 在构造函数中实现:
   cSpawn.LoadToolBarResource(IDR_MAINFRAME);
  cSpawn.AddToolBarResource(IDR_SYSTEMBAR);

当然也可直接继承一个类从cmdiframewnd:
the simplest is to derive the cmainframe from the cspawnmdiframewnd class instead of cmdiframewnd. this means you have to (using search/replace) replace all istance of cmdiframewnd with cspawnmdiframewnd (and of course include in your project file the files spawnmdiframewnd.cpp and spawnmdiframewnd.h). you will also need to do this for the cchildframe class, deriving from cspawnmdichildwnd and including spawnmdichildwnd.cpp/.h. the other way is to look at how this cspawnframexxx classes are been made and copy it (that means putting a cmenuspawn member in the frame class header and handling the wm_drawitem, wm_measureitem, wm_menuchar, wm_initmenu and wm_initmenupopup).
具体实现不在赘述.具体可参考:
http://boole.cs.iastate.edu/book/0-Programming/%B3%CC%D0%F2%D4%B1%B4%F3%B1%BE%D3%AA-%C4%BF%C2%BC/Visual%20C++/Source%20Code/Menu/owner_drawn_menu4.shtml.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值