⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 owner_drawn_menu4.shtml.htm

📁 随书类文件![随书类]MFC_SOURCEBOOK
💻 HTM
字号:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Zafir Anjum">
   <TITLE>Menu - Owner Drawn Menu with Icons (4)</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed">
<table WIDTH="100%">
<tr WIDTH="100%">
<td><A HREF="http://209.66.99.126/cgi/ads.cgi?advert=myad2"><IMG SRC="../../209.66.99.126/advertise2.gif" tppabs="http://209.66.99.126/advertise2.gif" ALT="" BORDER=2></A><td>
</tr>
</table>



<CENTER>
<H3>
<FONT COLOR="#AOAO99">Owner Drawn Menu with Icons (4) (automatically uses toolbar res)</FONT></H3></CENTER>

<CENTER>
<H3>

<HR></H3></CENTER>
This article was contributed by <A HREF="mailto:warch@tin.it">Iuri Apollonio</A>. 

<P>This is another way to draw a menu with bitmaps item.
Basically, this thing was created to let the user see visually the correspondence of
a menu item and a toolbar button, so that he can learn easily the meanings of sometime
strange button drawing.

<P>Now, the Visual C++ give us that new nice toolbar resources .. why bother with icons or
bitmaps and link them to menus while we can automatically "connect" the toolbar resource
to them ?

<P>So I wrote a small class, CMenuSpawn, which takes care with some help from FrameWnd of
menu remapping and drawing.

<P>Now I will explain how to use it in a SDI application:

<H4>Step 1: including files</H4>
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).


<H4>Step 2: initializing the CSpawnClass</H4>
We need to modify the CMainFrame class in this way:
<ul>
<li>add a CMenuSpawn cSpawn instance in the include file
<li>initialize the CMenuSpawn class in the CMainFrame constructor (cSpawn.LoadToolBarResource(IDR_MAINFRAME))
  this will load the toolbar resource into the class; you can alse use the AddToolBarResource function
  to add more than toolbar resource to the class
</ul>

<PRE><TT><FONT COLOR="#990000">
CMainFrame::CMainFrame()
{
	cSpawn.LoadToolBarResource(IDR_MAINFRAME);
}
</FONT></TT></PRE>


<H4>Step 3: handling menu remapping</H4>
To automatically remap the menus and make them ownerdraw, we need to get the handlers
(via ClassWizard) of WM_INITMENU and WM_INITPOPUPMENU messages

<P>We will modify the functions as below:

<PRE><TT><FONT COLOR="#990000">
void CMainFrame::OnInitMenu(CMenu* pMenu) 
{
	CFrameWnd::OnInitMenu(pMenu);
	cSpawn.RemapMenu(pMenu);	
}

void CMainFrame::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) 
{
	CFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
	if (!bSysMenu) cSpawn.RemapMenu(pPopupMenu);	
}
</FONT></TT></PRE>


<H4>Step 4: handling menu draw and measure</H4>
To draw the menu items we need to get handlers to the WM_DRAWITEM and WM_MEASUREITEM
messages. We will modify the functions as below:

<PRE><TT><FONT COLOR="#990000">
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);
}
</FONT></TT></PRE>


<H4>Step 5: Note to Stingray Software's Objective Toolkit Pro users</H4>
If you are using the OT Pro, maybe along with the SECMenuBar to obtain Office97 menu
button look and feel, you will also need to override the WindowProc virtual of the
CMainFrame as noted below:

<PRE><TT><FONT COLOR="#990000">
LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	if (message != WM_MEASUREITEM) return SECFrameWnd::WindowProc(message, wParam, lParam);
	else return CFrameWnd::WindowProc( message, wParam, lParam );	
}
</FONT></TT></PRE>

<P>If you are using another OTPro class other than SECFrameWnd as base class, change SECFrameWnd
to reflect your base class.

<P>And that's all to have basic coolmenu support.




<P>Looking at the sample applications, which implements an SDI Edit application and an MDI Edit
application, you will find a way to implement popup coolmenu.

<P>The MDI application differs from the SDI in this things:
<ul>
<li>the CChildFrame has been modified exactly as the CMainFrame, except for the WM_INITMENU
  handler
<li>you will find how to use 2 toolbar resource; the second will contain bitmaps for the
  system menu and correctly owner draw them too
</ul>

<P>Here follows a small description of the most useful function of the CSpawnMenu class:

<PRE><TT><FONT COLOR="#990000">
bool MeasureItem(..)
</FONT></TT></PRE>
  To be called from the CWnd OnMeasureItem(..); call the base class if it return false

<PRE><TT><FONT COLOR="#990000">
bool DrawItem(..)
</FONT></TT></PRE>
  To be called from the CWnd OnDrawItem(..); call the base class if it return false

<PRE><TT><FONT COLOR="#990000">bool LoadToolBarResource(unsigned int resId)
</FONT></TT></PRE>
  Load a toolbar resource in the class; an array of command id will be created and
  mapped to index of an imagelist

<PRE><TT><FONT COLOR="#990000">bool AddToolBarResource(unsigned int resId)
</FONT></TT></PRE>
  Add a toolbar resource to the class; works as the LoadToolBarResource

<PRE><TT><FONT COLOR="#990000">void RemapMenu(CMenu * pMenu)
</FONT></TT></PRE>
  Makes all the items of the menu OwnerDraw, (eventually) maps them with the appropiate
  images index based on the item command id; has to be called from the WM_INITMENU and
  WM_INITPOPUPMENU and before the TrackPopupMenu function

<PRE><TT><FONT COLOR="#990000">void EnableMenuItems(CMenu * pMenu, CWnd * pWnd)
</FONT></TT></PRE>
  Use the MFC command enabler mechanism to enable/disable menu items; need the pointer of
  the menu and the pointer of the CWnd of which the command enablers are to be used;
  it is designed to be used with popups menu from the right button click in Views
  (see the CEditView in sample apps for an example)


<P>The function for drawing disabled menu items has been stolen from the CToolbarEx of
Joerg Koenig (thanx!).


<P>Feel free to use and modify this class with all your software - I will only be glad to know
for which apps it will be used and of any improvement you will do to it!


<P><A HREF="owner_drawn_menu4_sdi.zip" tppabs="http://www.codeguru.com/menu/owner_drawn_menu4_sdi.zip">Download SDI demo project</A> 60K
<P><A HREF="owner_drawn_menu4_mdi.zip" tppabs="http://www.codeguru.com/menu/owner_drawn_menu4_mdi.zip">Download MDI demo project</A> 61K


<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="../index.htm" tppabs="http://www.codeguru.com/">Goto HomePage</A></FONT></TD>

<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>&copy; 1997 Zafir Anjum</FONT>&nbsp;</CENTER>
</TD>

<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A>&nbsp;</FONT></DIV>
</TD>
</TR>
</TABLE>
<CENTER><FONT SIZE=-2>4242</FONT></CENTER>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -