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

📄 0513001.htm

📁 VC知识库5_chm_decompile_20040520_210715
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
<link rel="stylesheet" type="text/css" href="../../vckbase.css">
</head>

<body>

<div align="justify">
  <table border="0" width="100%" class="font" height="57">
    <tr>
      <td width="27%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
      <font color="#800080">VC知识库(五)</font>
      </td>
      <td width="73%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
      <font color="#800080">www.vckbase.com</font>
      </td>
    </tr>
    <tr>
      <td width="100%" height="4" class="header" valign="top" align="center" colspan="2">
      <hr>
      </td>
    </tr>
    <tr>
      <td width="100%" height="17" class="header" valign="top" align="center" colspan="2">
      <big><b>自绘菜单</b></big>
      </td>
    </tr>
    <tr>
      <td width="100%" height="17" class="info" align="center" colspan="2">
      <big><b>闻怡洋译</b></big>
      </td>  
    </tr>  
    <tr> 
      <td width="100%" height="22" class="font" colspan="2">
        <hr>
      </td>  
    </tr> 
    <tr> 
      <td width="100%" height="5" class="font" colspan="2"> 



<p>在这里提供一个C++类(<b>CCustomMenu</b>),该类是CMenu的子类,并且拥有自绘能力。它可以向你提供以下的功能:



<ol>

  <li>设置字体颜色。</li>

  <li>设置高亮度颜色。</li>

  <li>设置高亮度时的风格。</li>

  <li>设置选中时和在普通状态下的菜单显示的图标。</li>

  <li>设置显示图标大小。</li>

</ol>



<p>在<b>CCustomMenu</b>中定义了结构MENUDATA,你必须根据你的需要填充该结构,并且在增加菜单时提供该结构的指针(调用AppendMenu,InsertMenu)。下面是一个例子:</p>



<p>1、定义<b>CCustomMenu</b>的实例,和MENUDATA结构变量。</p>



<pre><tt><font color="#990000">	CCustomMenu m_cCustomMenu;

	MENUDATA menuData [8];	// as many menu items are present , You should be able to use 

				//new and do the same

</font></tt>2、调用CreateMenu()设置有关参数。</pre>



<pre><tt><font color="#990000">	</font><font color="#0000FF"><strong>m_customMenu.CreateMenu ();</strong></font><font

color="#990000">

	</font><font color="#0000FF"><strong>m_customMenu.SetIconSize (25,25);</strong></font><font

color="#990000">	//This is to set the size of the Icon. 

						// This should be used only once for any menu

	// in order to resize it, destroy and create the menu again with  different size.

	</font><font

color="#0000FF"><strong>m_customMenu.SetHighlightStyle (Normal)</strong></font><font

color="#990000">; //Or TextOnly, if you want the

	// background color to remain the same

	// and the Text color to change to the Highlight color.

</font></tt>        // The following setXXXColor sets the menu colors. If you dont want to change any, Dont call these member functions. </pre>



<pre><tt><strong><font color="#0000FF">	       m_customMenu.SetTextColor (RGB (255,0,0));

	       m_customMenu.SetBackColor (RGB (255,255,255));

	       m_customMenu.SetHighlightColor (RGB (0,0,255));

</font></strong><font

color="#000000">3、设置MENUDATA变量,并增加菜单项。</font><font

color="#990000"> 

</font><strong><font color="#0000FF">	    lstrcpy (menuData[0].menuText , &quot;text1&quot;);

	    menuData[0].menuIconNormal= IDI_ICON1;

	    m_customMenu.AppendMenu (</font><font

color="#FF0000">MF_OWNERDRAW</font><font color="#0000FF">,3,</font><font color="#FF0000"><u>(LPCTSTR)menuData</u></font><font

color="#0000FF">);

</font></strong></tt></pre>



<pre>3、在你的窗口中重载OnMeasureItem(...)函数。</pre>



<pre><tt><font color="#990000">void CMyView::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)

{

	if ( lpMeasureItemStruct-&gt;CtlType == ODT_MENU &amp;&amp;

			IsMenu((HMENU)lpMeasureItemStruct-&gt;itemID) &amp;&amp;

			(lpMeasureItemStruct-&gt;itemID == (UINT)m_hMenuSub) )

	{

		m_customMenu.MeasureItem (lpMeasureItemStruct);

	}

	else

		// let MFC's self-drawing handle it

		CView::OnMeasureItem(nIDCtl, lpMeasureItemStruct);

}

</font></tt></pre>



<p>下面的函数将帮助你设置菜单属性。</p>



<pre><tt><font color="#990000">	void SetTextColor (COLORREF );

	void SetBackColor (COLORREF);

	void SetHighlightColor (COLORREF);

	void SetIconSize (int, int);

	void SetHighlightStyle (HIGHLIGHTSTYLE ); // HIGHLIGHTSTYLE : enum {Normal, TextOnly}

	void SetHighlightTextColor (COLORREF);







下面是文件代码:

//*************************************************************************

// CustomMenu.h : header file

//



#if

!defined(AFX_CUSTOMMENU_H__FE5B01C3_1E02_11D1_B87A_0060979CDF6D__INCLUDED_)

#define AFX_CUSTOMMENU_H__FE5B01C3_1E02_11D1_B87A_0060979CDF6D__INCLUDED_



#if _MSC_VER &gt;= 1000

#pragma once

#endif // _MSC_VER &gt;= 1000

class MENUDATA

{

public:

	MENUDATA () { menuIconNormal = -1; menuIconSelected = -1;};

	char  menuText[32];

	UINT menuIconNormal;

	UINT menuIconSelected;

};





typedef enum {Normal,TextOnly} HIGHLIGHTSTYLE;



///////////////////////////////////////////////////////////////////////////

//

// CCustomMenu window



class CCustomMenu : public CMenu

{

// Construction

public:

	CCustomMenu();



// Attributes

public:



// Operations

public:



// Overrides

	// ClassWizard generated virtual function overrides

	//{{AFX_VIRTUAL(CCustomMenu)

	//}}AFX_VIRTUAL



// Implementation

public:

	virtual ~CCustomMenu();

	virtual void DrawItem( LPDRAWITEMSTRUCT);

	virtual void MeasureItem( LPMEASUREITEMSTRUCT );

	void SetTextColor (COLORREF );

	void SetBackColor (COLORREF);

	void SetHighlightColor (COLORREF);

	void SetIconSize (int, int);

	void SetHighlightStyle (HIGHLIGHTSTYLE );

	void SetHighlightTextColor (COLORREF);



	// 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;



	//{{AFX_MSG(CCustomMenu)

		// NOTE - the ClassWizard will add and remove member functions here.

	//}}AFX_MSG

};



///////////////////////////////////////////////////////////////////////////

//



//{{AFX_INSERT_LOCATION}}

// Microsoft Developer Studio will insert additional declarations immediately before the previous line.



#endif //!defined(AFX_CUSTOMMENU_H__FE5B01C3_1E02_11D1_B87A_0060979CDF6D__INCLUDED_)



//*************************************************************************

// CustomMenu.cpp : implementation file

//



#include &quot;stdafx.h&quot;

#include &quot;CustomMenu.h&quot;



#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif



///////////////////////////////////////////////////////////////////////////

//

// CCustomMenu



CCustomMenu::CCustomMenu()

{

	m_clrText =  GetSysColor (COLOR_MENUTEXT);

	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 =		GetSystemMetrics ( SM_CXMENUCHECK);

	m_iconY =		GetSystemMetrics (SM_CYMENUCHECK );



	m_clrHilight = GetSysColor (COLOR_HIGHLIGHT);

	m_brSelect.CreateSolidBrush (m_clrHilight);

	m_clrHilightText = GetSysColor (COLOR_HIGHLIGHTTEXT);



	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);

}



CCustomMenu::~CCustomMenu()

{

	if ((HBRUSH) m_brBackground != NULL)

		m_brBackground.DeleteObject ();

	if ((HFONT)m_fontMenu !=NULL)

		m_fontMenu.DeleteObject ();

	if ((HBRUSH)m_brSelect != NULL)

		 m_brSelect.DeleteObject ();

}







///////////////////////////////////////////////////////////////////////////

//

// CCustomMenu message handlers





void CCustomMenu::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)

	{

		strText = (((MENUDATA*) (lpDIS-&gt;itemData))-&gt;menuText);

		if ((((MENUDATA *)(lpDIS-&gt;itemData))-&gt;menuIconNormal) == -1)

			hIcon = NULL;

		else if (state &amp; ODS_SELECTED)

		{

			if ((((MENUDATA *)(lpDIS-&gt;itemData))-&gt;menuIconSelected) != -1)

				hIcon = AfxGetApp ()-&gt;LoadIcon (((MENUDATA *)(lpDIS-&gt;itemData))-&gt;menuIconSelected);

			else

				hIcon = AfxGetApp()-&gt;LoadIcon (((MENUDATA*)(lpDIS-&gt;itemData))-&gt;menuIconNormal);

		}

		else

			hIcon = AfxGetApp()-&gt;LoadIcon (((MENUDATA*)(lpDIS-&gt;itemData))-&gt;menuIconNormal);



		TRACE1 (&quot;Draw for %s\n&quot;, strText);

	}

	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)

		{

			pDC-&gt;FillRect (rect,&amp;m_brBackground);

		}

		else

		{

			pDC-&gt;FillRect (rect,&amp;m_brSelect);

		}



		pDC-&gt;SelectObject (pOldPen);

		pDC-&gt;Draw3dRect (rect,GetSysColor (COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW));

		lf.lfWeight = FW_BOLD;

		if ((HFONT)dispFont != NULL)

			dispFont.DeleteObject ();

		dispFont.CreateFontIndirect (&amp;lf);

		crText = m_clrHilightText;



		//While selected move the text a bit

		TRACE0 (&quot;SELECT,SELECTED\n&quot;);

	}

	else

	{

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

		pDC-&gt;FillRect (rect,&amp;m_brBackground);

		pDC-&gt;SelectObject (pOldPen);

		// draw the up edges

		pDC-&gt;Draw3dRect (rect,m_clrBack,m_clrBack);

		if ((HFONT)dispFont != NULL)

			dispFont.DeleteObject ();

		dispFont.CreateFontIndirect (&amp;lf); //Normal



		TRACE0 (&quot;SELECT, NORMAL\n&quot;);

	}



	// draw the text if there is any

	//We have to paint the text only if the image is nonexistant

	if (hIcon != NULL)

	{

		if(DrawIconEx (pDC-&gt;GetSafeHdc(),rect.left,rect.top,hIcon,

			(m_iconX)?m_iconX:32,(m_iconY)?m_iconY:32,0,NULL,DI_NORMAL))

			TRACE0(&quot;Wrote the icon successfully\n&quot;);

		else

			TRACE0 (&quot;SORRY.NOGO\n&quot;);

	}

	//This is needed always so that we can have the space for check marks

	rect.left = rect.left +((m_iconX)?m_iconX:32); 



	if ( !strText.IsEmpty())

	{

		//		pFont-&gt;GetLogFont (&amp;lf);



		int		  iOldMode = pDC-&gt;GetBkMode();



		pDC-&gt;SetBkMode( TRANSPARENT);



		pDC-&gt;SetTextColor( crText);



		pFont = pDC-&gt;SelectObject (&amp;dispFont);

		TRACE1( &quot;About To DrawText %s\n&quot;,strText);

		pDC-&gt;DrawText (strText,rect,DT_LEFT|DT_SINGLELINE|DT_VCENTER);

		TRACE0(&quot;Done\n&quot;);

		pDC-&gt;SetBkMode( iOldMode );

		pDC-&gt;SelectObject (pFont); //set it to the old font

	}

	dispFont.DeleteObject ();

}



void CCustomMenu::MeasureItem( LPMEASUREITEMSTRUCT lpMIS )

{

	CDC *pDC = AfxGetApp()-&gt;m_pMainWnd-&gt;GetDC();

	CFont* pFont = pDC-&gt;SelectObject (&amp;m_fontMenu);

	int iconX = 0,iconY= 0;

	TEXTMETRIC tm;

	pDC-&gt;GetTextMetrics (&amp;tm);

	pDC-&gt;SelectObject (pFont);

	AfxGetApp()-&gt;m_pMainWnd-&gt;ReleaseDC (pDC);



	if (m_iconX)

		iconX = m_iconX;

	if (m_iconY)

		iconY = m_iconY;



	lpMIS-&gt;itemWidth = iconX + tm.tmAveCharWidth *  lstrlen(((MENUDATA*)(lpMIS-&gt;itemData))-&gt;menuText) +10;

	lpMIS-&gt;itemHeight = (iconY &gt; (m_iMenuHeight+1))?iconY:m_iMenuHeight + 1;

}



void CCustomMenu::SetIconSize (int width, int height)

{

	m_iconX = width;

	m_iconY = height;

}



void CCustomMenu::SetTextColor (COLORREF clrText)

{

	m_crText = clrText;

}



void CCustomMenu::SetBackColor (COLORREF clrBack)

{

	m_clrBack = clrBack;

	if ((HBRUSH)m_brBackground != NULL)

		m_brBackground.DeleteObject ();

	m_brBackground.CreateSolidBrush (clrBack);

}



void CCustomMenu::SetHighlightColor (COLORREF clrHilight)

{

	m_clrHilight = clrHilight;

	if ((HBRUSH)m_brSelect != NULL)

			m_brSelect.DeleteObject ();

	m_brSelect.CreateSolidBrush (clrHilight);

}



void CCustomMenu::SetHighlightTextColor (COLORREF clrHilightText)

{

	m_clrHilightText = clrHilightText;

}





void CCustomMenu::SetHighlightStyle (HIGHLIGHTSTYLE hilightStyle)

{

	m_hilightStyle = hilightStyle;

}

//*************************************************************************</font></tt></pre>



      </td>    
    </tr>   
    <tr>
      <td width="100%" height="12" class="font" colspan="2"> 
      </td>    
    </tr>
    <tr>
      <td width="100%" height="6" class="font" colspan="2"> 
      </td>    
    </tr>
    <tr>
      <td width="100%" height="8" class="font" colspan="2"> 
      </td>    
    </tr>
    <tr>   
      <td width="100%" height="17" class="font" colspan="2"></td>    
    </tr>   
  </table>    
</div>    
    
</body>    
    
</html>    

⌨️ 快捷键说明

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