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

📄 toolmenu.cpp

📁 深入剖析Visual C++编程技术及应用实例
💻 CPP
字号:
#include "stdafx.h"
#include "ToolMenu.h"
#include "afxpriv.h"

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

/////////////////////////////////////////////////////////////////////////////
// CToolMenu
/*
	Toooooo lazy to calculate or to obtain from the system
*/
#define		MENU_SPACETEXT			10
#define		MENU_TOPFACTORS			2
#define		MENU_LEFTFACTORS		4
#define		MENU_RIGHTFACTORS		4
#define		MENU_BOTTOMFACTORS		2
#define		MENU_MAXWIDTH			100

CToolMenu::CToolMenu()
{
}

CToolMenu::~CToolMenu()
{
}

/////////////////////////////////////////////////////////////////////////////
/*
	This is the drawing routine for the owner drawn menu
	The toolbar pointer have to be valid ! 
*/
void CToolMenu::DrawItem( LPDRAWITEMSTRUCT lpds ) 
{

	// If toolbar not set then you cannot proceed drawing so just skip
	if ( !m_pToolbar )
		return;

	// Get the image list, gotta do this because i have to draw the bitmap
	CImageList * pImgList = m_pToolbar->GetToolBarCtrl().GetImageList();
	ASSERT( pImgList );

	// Get the size of the image
	int cx, cy, iIndex, iImage;
	UINT iID, iStyle;
	::ImageList_GetIconSize(*pImgList, &cx, &cy);

	// Have to find which image the button is using, image list wants index
	iID = lpds->itemID;
	iIndex = m_pToolbar->CommandToIndex ( iID );
	m_pToolbar->GetButtonInfo ( iIndex, iID, iStyle, iImage );

	CDC	dc;
	dc.Attach ( lpds->hDC );

	// Get the drawing rectangle
	CRect	rectDraw = lpds->rcItem;
	
	// Refer DrawIndirect( ) API for the drawstyle, raster op, etc...
	// Only selection/checked/grayed state is handled here
	// Add or change here for all the drawing operations...
	int		iRasterOP = SRCCOPY;
	int		iDrawStyle= ILD_NORMAL;

	COLORREF	clrTextBack;
	COLORREF	clrTextFore;
	COLORREF	clrMaskFore;

	clrTextBack = ::GetSysColor (COLOR_MENU);
	clrTextFore = ::GetSysColor (COLOR_MENUTEXT);
	
	clrMaskFore = CLR_DEFAULT;

	// Get appropriate colors from the system for painting our menu
	if ((lpds->itemState & ODS_SELECTED) &&
		(lpds->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
	{
		clrTextBack = ::GetSysColor (COLOR_HIGHLIGHT);
	}
	dc.FillSolidRect ( rectDraw, clrTextBack );

	if (lpds->itemState & ODS_GRAYED || lpds->itemState & ODS_DISABLED )
	{
		clrTextFore = ::GetSysColor (COLOR_GRAYTEXT);
		iDrawStyle= ILD_BLEND25;
		clrMaskFore = CLR_NONE;
	}


	CPoint lefttop;
	lefttop.x = rectDraw.left + MENU_LEFTFACTORS;
	lefttop.y = rectDraw.top + MENU_TOPFACTORS ;

	// A  special 3d rect for the checked state
	if (lpds->itemState & ODS_CHECKED )
	{
		clrTextFore = ::GetSysColor (COLOR_3DHILIGHT);

		CRect	rect3D;
		rect3D.SetRect ( lefttop.x, lefttop.y, lefttop.x+cx, lefttop.y+cy );
		rect3D.InflateRect (2,2);
		dc.Draw3dRect( &rect3D, RGB(0,0,0), clrTextFore );
	}

	// Draw the toolbar button bitmap
	pImgList->DrawIndirect( &dc, iImage, lefttop, CSize(cx, cy), CPoint(0, 0), 
							iDrawStyle, iRasterOP, clrTextBack, clrMaskFore);

	// Now time to draw the text
	CRect	rectText = rectDraw;
	rectText.DeflateRect ( MENU_SPACETEXT + MENU_LEFTFACTORS + cx, MENU_TOPFACTORS, MENU_RIGHTFACTORS, MENU_BOTTOMFACTORS );

	COLORREF	clrTextForeold = dc.SetTextColor( clrTextFore );
	dc.SetBkMode ( TRANSPARENT );


	// Try to load the string from resource
	// The resource of the toolbar buttons usually have the format
	// Status bar text\ntooltip
	// this extracts the tooltip text and displays
	// Users who dont like this, please change whatever you wanted to display
	CString	strText;
	if ( strText.LoadString ( iID ) )
	{
		int iPos = strText.Find ( '\n', 0 );
		if ( iPos != -1 )
		{
			CString strTemp = strText.Mid( iPos+1 );
			dc.DrawText ( strTemp, -1, &rectText, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS );
		}
	}

	dc.SetTextColor( clrTextForeold );

	// Detach the dc
	dc.Detach ();
}

void CToolMenu::MeasureItem( LPMEASUREITEMSTRUCT lpms ) 
{
	// no tool bar, then no op
	if ( !m_pToolbar )
		return;

	// Get imagelist
	CImageList * pImgList = m_pToolbar->GetToolBarCtrl().GetImageList();
	ASSERT( pImgList );

	// get image size
	int cx, cy;
	::ImageList_GetIconSize(*pImgList, &cx, &cy);

	// Calculate width and height of the item.
	// To lazy to get the info from system or to calculate.
	lpms->itemHeight = cy + MENU_BOTTOMFACTORS + MENU_TOPFACTORS;
	lpms->itemWidth = cx +	MENU_LEFTFACTORS + MENU_RIGHTFACTORS + 
							MENU_MAXWIDTH + MENU_SPACETEXT;
}

⌨️ 快捷键说明

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