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