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

📄 owner_drawn_menu2.shtml

📁 mfc资源大全包含MFC编程各个方面的源码
💻 SHTML
📖 第 1 页 / 共 2 页
字号:
<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 (2)</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed">
<table WIDTH="100%">
<tr WIDTH="100%">
<td align=center><!--#exec cgi="/cgi/ads.cgi"--><td>
</tr>
</table>


<CENTER>
<H3>
<FONT COLOR="#AOAO99">Owner Drawn Menu with Icons (2)</FONT></H3></CENTER>

<CENTER>
<H3>

<HR></H3></CENTER>
This code was contributed by <A HREF="mailto:BenA@sirplc.co.uk">Ben Ashley</A>.


<p>This code is an extension to Girish Bharadwaj's Owner Drawn
menu. The code he produced does not lack anything, but I thought
it would be advantageous to the user to be able to load a Menu
Resource in. Currently, you have to construct the menu yourself,
item-by-item. This can be very time consuming. Instead, I have
introduced 4 new functions to the Owner Drawn menu class...</p>

<ol>
    <li>AppendODMenu(). Appends an Owner-Drawn Item to the menu.
        You can pass an icon, a selected icon and a disabled
        icon.</li>
    <li>ModifyODMenu(), as above but modifies an existing menu
        given it's command Identifier (Not positional
        information)</li>
    <li>LoadMenu(int) &amp; LoadMenu(LPCTSTR). Given a menu
        resource, these commands (which effectively replace
        CMenu's LoadMenu()), will load the specified menu
        resource, creating each item as an owner-drawn item. You
        may then use the ModifyODMenu() member function to assign
        icon states to the items. It is not possible at this time
        to specify icons within the menu resource, although I
        suspect it is possible by defining a custom resource
        type. This is something I will be looking in to in the
        next few weeks.</li>
</ol>

<p>Some problems the new code does <em>not</em> address, is the
drawing of separators, checkboxes or the correct placement of the
shortcut key text. This is also something I will be looking at,
but of course, anyone else is free to implement it.</p>

<p>As I renamed the menu class TCMenu, just giving you the added
snippets is probably not enough. It is for this reason that the
entire code is here. Apart from a few changes to the drawing
behaviour of the menu, I have only added those functions
mentioned above. The rest of the code must be credited to Girish
Bharadwaj.</p>

<h2>The header file.... (TCMenu.h)...</h2>

<p>&nbsp;</p>

<pre><font color="#990000">/*
===============================================================================
CMenu
TCMenu
--------------- </font></pre>

<pre><font color="#990000">Implements Owner-drawn menu behaviour
===============================================================================
*/

</font></pre>

<pre><font color="#990000">#ifndef TCMENU_H
#define TCMENU_H

</font></pre>

<pre><font color="#990000">// TCMenuData class. Fill this class structure to define a single menu item:

</font></pre>

<pre><font color="#990000">class TCMenuData
{
public:
TCMenuData () { menuIconNormal = -1; menuIconSelected = -1; menuIconDisabled = -1;nID=0;};
char menuText[32];
UINT menuIconNormal;
UINT menuIconSelected;
UINT menuIconDisabled;
UINT nID;
};


</font></pre>

<pre><font color="#990000">typedef enum {Normal,TextOnly} HIGHLIGHTSTYLE;


</font></pre>

<pre><font color="#990000">class TCMenu : public CMenu	// Derived from CMenu
{
// Construction
public:
TCMenu(); </font></pre>

<pre><font color="#990000">// Attributes
protected:
CTypedPtrArray&lt;CPtrArray, TCMenuData*&gt; m_MenuList;	// Stores list of menu items </font></pre>

<pre><font color="#990000">// When loading an owner-drawn menu using a Resource, TCMenu must keep track of
// the popup menu's that it creates. Warning, this list *MUST* be destroyed
// last item first :)

</font></pre>

<pre><font color="#990000">CTypedPtrArray&lt;CPtrArray, TCMenu*&gt;	m_SubMenus;	// Stores list of sub-menus </font></pre>

<pre><font color="#990000">// Operations
public: </font></pre>

<pre><font color="#990000">// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCustomMenu)
//}}AFX_VIRTUAL </font></pre>

<pre><font color="#990000">// Implementation
public:
virtual ~TCMenu();	// Virtual Destructor </font></pre>

<pre><font color="#990000">// Drawing: </font></pre>

<pre><font color="#990000">virtual void DrawItem( LPDRAWITEMSTRUCT);	// Draw an item
virtual void MeasureItem( LPMEASUREITEMSTRUCT );	// Measure an item

</font></pre>

<pre><font color="#990000">// Custamizing:

</font></pre>

<pre><font color="#990000">void SetTextColor (COLORREF );	// Set the text color
void SetBackColor (COLORREF);	// Set background color
void SetHighlightColor (COLORREF);	// Set highlight Color
void SetIconSize (int, int);	// Set icon size
void SetHighlightStyle (HIGHLIGHTSTYLE );	// Set Highlight style
void SetHighlightTextColor (COLORREF);	// Set Highlight text color

</font></pre>

<pre><font color="#990000">virtual BOOL AppendODMenu(LPCTSTR lpstrText, 
UINT nFlags = MF_OWNERDRAW,
UINT nID = 0,
UINT nIconNormal = -1, 
UINT nIconSelected = -1,
UINT nIconDisabled = -1);	// Owner-Drawn Append </font></pre>

<pre><font color="#990000">virtual BOOL ModifyODMenu(LPCTSTR lpstrText,
UINT	nID = 0,
UINT	nIconNormal = -1,
UINT	nIconSelected = -1,
UINT	nIconDisabled = -1);	// Owner-Drawn Modify </font></pre>

<pre><font color="#990000">virtual BOOL LoadMenu(LPCTSTR lpszResourceName);	// Load a menu
virtual BOOL LoadMenu(int nResource);	// ... </font></pre>

<pre><font color="#990000">// Destoying:

</font></pre>

<pre><font color="#990000">virtual BOOL DestroyMenu();

</font></pre>

<pre><font color="#990000">// Generated message map functions
protected:
COLORREF m_crText;
COLORREF m_clrBack;
COLORREF m_clrText;
COLORREF m_clrHilight;
COLORREF m_clrHilightText;
LOGFONT m_lf;
CFont m_fontMenu;
UINT m_iMenuHeight;
BOOL m_bLBtnDown;
CBrush m_brBackground,m_brSelect;
CPen m_penBack;
int m_iconX,m_iconY;
HIGHLIGHTSTYLE m_hilightStyle; </font></pre>

<pre><font color="#990000">//{{AFX_MSG(CCustomMenu)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
}; </font></pre>

<pre><font color="#990000">///////////////////////////////////////////////////////////////////////////
// </font></pre>

<pre><font color="#990000">//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line. </font></pre>

<pre><font color="#990000">#endif // CCUSTOMMENU_H </font></pre>

<h2>The Implementation File (TCMenu.cpp)</h2>

<p>&nbsp;</p>

<pre><font color="#990000">//*************************************************************************</font></pre>

<pre><font color="#990000">// TCMenu.cpp : implementation file
//

#include &quot;stdafx.h&quot;				// Standard windows header file
#include &quot;TCMenu.h&quot;				// TCMenu class declaration

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/*
	=================================================================================
	TCMenu::TCMenu()
	TCMeny::~TCMenu()
	-----------------

	Constructor and Destructor.
	=================================================================================
*/


TCMenu::TCMenu()
{
	m_clrText =  GetSysColor (COLOR_MENUTEXT);				// Default menu text colour
    m_clrBack = GetSysColor (COLOR_MENU);
    m_brBackground.CreateSolidBrush (m_clrBack);
    m_penBack.CreatePen (PS_SOLID,0,m_clrBack);
    m_crText = m_clrText;
    m_bLBtnDown = FALSE;

    m_iconX = 16;						// Icon sizes default to 16 x 16
    m_iconY = 16;						// ...

    m_clrHilight = GetSysColor (COLOR_HIGHLIGHT);			// User Colours...
    m_brSelect.CreateSolidBrush (m_clrHilight);				// Create a brush
    m_clrHilightText = GetSysColor (COLOR_HIGHLIGHTTEXT);	// User Colours...

    ZeroMemory ((PVOID) &amp;m_lf,sizeof (LOGFONT));
    NONCLIENTMETRICS nm;
    nm.cbSize = sizeof (NONCLIENTMETRICS);

    //Get the system metrics for the Captionfromhere

	VERIFY (SystemParametersInfo (SPI_GETNONCLIENTMETRICS,0,&amp;nm,0)); 

    m_lf =  nm.lfMenuFont;
    m_iMenuHeight = nm.iMenuHeight;
    m_fontMenu.CreateFontIndirect (&amp;m_lf);
}


TCMenu::~TCMenu()
{
	DestroyMenu();
}


BOOL TCMenu::DestroyMenu()
{
	// Destroy Sub menus:

	int m;
	int numSubMenus = m_SubMenus.GetUpperBound();
	for(m = numSubMenus; m &gt;= 0; m--)
		delete(m_SubMenus[m]);

	m_SubMenus.RemoveAll();


	// Destroy brushes:


    if ((HBRUSH) m_brBackground != NULL)		// Background brush
        m_brBackground.DeleteObject ();
    if ((HFONT)m_fontMenu !=NULL)				// Font for the menu
        m_fontMenu.DeleteObject ();
    if ((HBRUSH)m_brSelect != NULL)				// Selected background brush
        m_brSelect.DeleteObject ();


	// Destroy menu data


	int numItems = m_MenuList.GetUpperBound();
	for(m = 0; m &lt;= numItems; m++)
			delete(m_MenuList[m]);


	m_MenuList.RemoveAll();


	// Call base-class implementation last:


	return(CMenu::DestroyMenu());
};


///////////////////////////////////////////////////////////////////////////
//
// TCMenu message handlers


/*
	=================================================================================
	void TCMenu::DrawItem(LPDRAWITEMSTRUCT)
	---------------------------------------

	Called by the framework when a particular item needs to be drawn.  We overide
	this to draw the menu item in a custom-fashion, including icons and the 3D
	rectangle bar.
	=================================================================================
*/


void TCMenu::DrawItem (LPDRAWITEMSTRUCT lpDIS)
{
    ASSERT(lpDIS != NULL);

    CDC* pDC = CDC::FromHandle(lpDIS-&gt;hDC);
    CRect rect;
    HICON hIcon;
    COLORREF crText = m_crText;
    
	
	// draw the colored rectangle portion
    
	
	rect.CopyRect(&amp;lpDIS-&gt;rcItem);

    
	// draw the up/down/focused/disabled state

    
    UINT action = lpDIS-&gt;itemAction;
    UINT state = lpDIS-&gt;itemState;
    CString strText;
    LOGFONT lf;
    lf = m_lf;

    CFont dispFont;
    CFont *pFont;
        
	//GetWindowText(strText);
    if (lpDIS-&gt;itemData != NULL)
    {
		UINT nIconNormal = (((TCMenuData*)(lpDIS-&gt;itemData))-&gt;menuIconNormal);
		UINT nIconSelected = (((TCMenuData*)(lpDIS-&gt;itemData))-&gt;menuIconSelected);
		UINT nIconDisabled = (((TCMenuData*)(lpDIS-&gt;itemData))-&gt;menuIconDisabled);
        strText = (((TCMenuData*) (lpDIS-&gt;itemData))-&gt;menuText);

        if(nIconNormal == -1)
			hIcon = NULL;
        else 
		{
			hIcon = NULL;

			// Obtain the IDs for the appropriate Icons:


			if (state &amp; ODS_SELECTED &amp;&amp; !(state &amp; ODS_GRAYED))		// Selected (but not disabled)
			{
                if(nIconSelected != -1)
					hIcon = AfxGetApp ()-&gt;LoadIcon(nIconSelected);
			}
	        else
			{
				if(state &amp; ODS_GRAYED)								// Disabled (selected or not)
					if(nIconDisabled != -1)
						hIcon = AfxGetApp()-&gt;LoadIcon(nIconDisabled);
			};


			// If we didn't manage to select a specific icon, we'll use the
			// default (normal) one...


			if(hIcon == NULL)
				hIcon = AfxGetApp()-&gt;LoadIcon(nIconNormal);
		}
    }
    else
    {
        strText.Empty();
        hIcon = NULL;
    }

    if ( (state &amp; ODS_SELECTED) )
    {
        // draw the down edges

        CPen *pOldPen = pDC-&gt;SelectObject (&amp;m_penBack);


        // You need only Text highlight and thats what you get


        if (m_hilightStyle != Normal)

⌨️ 快捷键说明

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