www.pudn.com > BitMapToolBar.rar > ToolBarEx.cpp


// ToolBarEx.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "TestToolBar.h" 
#include "ToolBarEx.h" 
#include  
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CToolBarEx message handlers 
 
BEGIN_MESSAGE_MAP(CToolBarEx, CToolBar) 
	//{{AFX_MSG_MAP(CToolBarEx) 
	ON_WM_PAINT() 
	ON_WM_MOUSEMOVE() 
	ON_WM_CREATE() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_LBUTTONUP() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CToolBarEx Constructor/Creator. 
 
CToolBarEx::CToolBarEx () 
{ 
	// Init variables. 
 	all_frame = true; 
	ImageStyle = IM_TRANSPARENT; 
	MaskColor = RGB(198, 195, 198); 
	nbButtons = 1; 
	DrawGripper = false; 
	button_flat = false; 
	init = false; 
	button_down = false; 
} 
 
CToolBarEx::~CToolBarEx () 
{ 
	dcMemSave.DeleteDC(); 
} 
 
BOOL CToolBarEx::CreateEx ( CWnd* pParentWnd, 
							DWORD dwCtrlStyle, 
							DWORD dwStyle, 
							UINT BkBitMapId, 
							UINT TbBitMapId, 
							UINT NBButtons, 
							bool strechBackGround ) 
{ 
	// Get the button's style. 
	if ( dwCtrlStyle == TBSTYLE_FLAT ) 
		button_flat = true; 
 
	// Set the existing toolbar as transparent to avoid flickering. 
	dwCtrlStyle = TBSTYLE_TRANSPARENT; 
 
	// Check if the gripper must be drawn. 
	if ( (dwStyle & CBRS_GRIPPER) != 0 ) 
		DrawGripper = true; 
 
	// Load the background image. 
	bmpBackground.LoadBitmap(BkBitMapId); 
	bmpBackground.GetObject(sizeof(bk_bm),&bk_bm); 
 
	BkSave.LoadBitmap(BkBitMapId); 
	BkSave.GetObject(sizeof(bk_bm),&bk_bm); 
 
	// Load the toolbar image. 
	bmptoolbar.LoadBitmap(TbBitMapId);	 
	bmptoolbar.GetObject(sizeof(bmt),&bmt); 
 
	StrechBackGround = strechBackGround; 
	nbButtons =  NBButtons; 
	image_len = ((bmt.bmWidth)/nbButtons); 
 
	BOOL rc = CToolBar::CreateEx ( pParentWnd, dwCtrlStyle, dwStyle ); 
 
	dcMemSave.CreateCompatibleDC(this->GetDC()); 
	dcMemSave.SelectObject(&BkSave); 
	 
	return rc; 
} 
 
/////////////////////////////////////////////////////////////////////////////// 
// OnPaint method. 
 
void CToolBarEx::OnPaint()  
{ 
	// Get a paint handle to disable the system paint. 
	CPaintDC pDC(this); 
 
	DrawBackGround(); 
	DrawButtonEdge(); 
} 
 
/////////////////////////////////////////////////////////////////////////////// 
// Draw the toolbar. 
 
void CToolBarEx::DrawBackGround() 
{ 
	// Get a paint handle to the parent frame control. 
	CFrameWnd*  frame = this->GetParentFrame();	 
	CClientDC frameDC(frame); 
 
	// Get a paint handle to the toolbar control. 
	CClientDC toolbarDC(this); 
	 
	// Get the size of the toolbar parent control. 
	frame->GetClientRect(&Frame_rect); 
	 
	// Get the size of the toolbar control. 
	GetClientRect(&ToolBar_rect); 
	ToolBar_rect.right = Frame_rect.right;	 
 
	// Handle to display the background. 
	CDC dcMem; 
	dcMem.CreateCompatibleDC(&frameDC); 
	dcMem.SelectObject(&bmpBackground); 
 
	// Check if the gripper is to be drawn. 
	if ( DrawGripper == true && all_frame == true ) 
	{ 
		CPoint frame_client_pt(Frame_rect.left, Frame_rect.top); 
		frame->ClientToScreen(&frame_client_pt); 
 
		CPoint control_client_pt(ToolBar_rect.left, ToolBar_rect.top); 
		ClientToScreen(&control_client_pt); 
		 
		ToolBar_rect.left += control_client_pt.x - frame_client_pt.x; 
		ToolBar_rect.top += control_client_pt.y - frame_client_pt.y; 
		ToolBar_rect.bottom += 2; 
 
		frameDC.BitBlt (	Frame_rect.left, Frame_rect.top + 1, ToolBar_rect.left - 7, ToolBar_rect.bottom - 1, 
							&dcMem, 0, 0, SRCCOPY ); 
	} 
 
 
	// 
	// Draw the background image. 
	// 
 
	if ( all_frame == true ) 
	{ 
		if ( StrechBackGround == false ) 
			frameDC.BitBlt (	ToolBar_rect.left, ToolBar_rect.top, ToolBar_rect.Width(), ToolBar_rect.Height(), 
								&dcMem, 0, 0, SRCCOPY ); 
		 
		else 
			frameDC.StretchBlt (	ToolBar_rect.left, ToolBar_rect.top, ToolBar_rect.Width(), ToolBar_rect.Height(), 
									&dcMem, 0, 0, bk_bm.bmWidth, bk_bm.bmHeight, SRCCOPY ); 
	} 
	else 
	{ 
		if ( StrechBackGround == false ) 
			toolbarDC.BitBlt (	ToolBar_rect.left, ToolBar_rect.top, ToolBar_rect.Width(), ToolBar_rect.Height(), 
								&dcMem, 0, 0, SRCCOPY ); 
		 
		else 
			toolbarDC.StretchBlt ( ToolBar_rect.left, ToolBar_rect.top, ToolBar_rect.Width(), ToolBar_rect.Height(), 
								&dcMem, 0, 0, bk_bm.bmWidth, bk_bm.bmHeight, SRCCOPY ); 
	} 
 
 
	// 
	// Draw the button's image. 
	// 
 
	// Get a handle to the toolbar control to get the button's info. 
	CToolBarCtrl& control = GetToolBarCtrl(); 
 
	// Handle to display the toolbar buttons. 
	CDC dcMemt; 
	dcMemt.CreateCompatibleDC(&toolbarDC); 
	dcMemt.SelectObject(&bmptoolbar); 
 
	// Loop to get each button. 
	for ( short i = 0 ; i < control.GetButtonCount() ; i++ ) 
	{ 
		// Get button's info. 
		GetButtonInfo ( i, id, style, index ); 
		GetItemRect ( i, &button_rect ); 
 
		// Position of the button's bitmap into the toolbar bitmap row. 
		image_pos = ((bmt.bmWidth)/nbButtons) * index; 
 
		// Insert the button's bitmap if this is a button. 
		if ( ImageStyle == IM_STRECH && style != TBBS_SEPARATOR  ) 
		{ 
			if ( style == TBBS_DISABLED ) 
			{ 
				for ( short x = 0 ; x < image_len ; x++ ) 
				{ 
					for ( short y = 0 ; y < bmt.bmHeight ; y++ ) 
					{ 
						if ( dcMemt.GetPixel(x + image_pos, y) != MaskColor ) 
						{ 
							if ( init == false ) 
							{ 
								dcMemSave.SetPixel ( x + image_pos, y, GetSysColor(COLOR_GRAYTEXT) ); 
							} 
							 
							dcMemt.SetPixel ( x + image_pos, y, GetSysColor(COLOR_GRAYTEXT) ); 
						} 
					} 
				} 
			} 
 
			// Strech the bitmap onto the toolbar button. 
			toolbarDC.StretchBlt (	button_rect.left + 2, button_rect.top + 3, button_rect.right - button_rect.left - 4, button_rect.bottom - button_rect.top - 4, 
								&dcMemt, image_pos, 0, image_len, bmt.bmHeight,	SRCCOPY ); 
 
			if ( init == false ) 
			{				 
				dcMemSave.StretchBlt (	button_rect.left + 2, button_rect.top + 3, button_rect.right - button_rect.left - 4, button_rect.bottom - button_rect.top - 4, 
								&dcMemt, image_pos, 0, image_len, bmt.bmHeight,	SRCCOPY ); 
			} 
		} 
 
		else if ( ImageStyle == IM_TRANSPARENT && style != TBBS_SEPARATOR ) 
		{ 
			// If the button size < image size, ot process this one, could also strech the image...) 
			if ( (button_rect.right - button_rect.left) < image_len || (button_rect.bottom - button_rect.top) < bmt.bmHeight ) 
			{ 
				//**toolbarDC.StretchBlt (	button_rect.left + 2, button_rect.top + 3, button_rect.right - button_rect.left - 4, button_rect.bottom - button_rect.top - 4, 
				//**				&dcMemt, image_pos, 0, image_len, bmt.bmHeight,	SRCCOPY ); 
			} 
 
			else 
			{ 
				// Remove mask background. 
				for ( short x = 0 ; x < image_len ; x++ ) 
				{ 
					for ( short y = 0 ; y < bmt.bmHeight ; y++ ) 
					{ 
						COLORREF color = dcMemt.GetPixel ( x + image_pos, y ); 
 
						int center_x = (button_rect.right - button_rect.left)/2; 
						int center_y = (button_rect.bottom - button_rect.top)/2; 
 
						dest_pt.x = button_rect.left + center_x - image_len/2 + x; 
						dest_pt.y = button_rect.top + center_y - bmt.bmHeight/2 + y; 
 
						if ( color != MaskColor ) 
						{ 
							if ( style == TBBS_DISABLED ) 
							{ 
								if ( init == false ) 
								{				 
									dcMemSave.SetPixel ( dest_pt.x, dest_pt.y, GetSysColor(COLOR_GRAYTEXT) ); 
								} 
 
								toolbarDC.SetPixel ( dest_pt.x, dest_pt.y, GetSysColor(COLOR_GRAYTEXT) ); 
							} 
						 
							else 
							{ 
								if ( init == false ) 
								{				 
									dcMemSave.SetPixel ( dest_pt.x, dest_pt.y, color ); 
								} 
							 
								toolbarDC.SetPixel ( dest_pt.x, dest_pt.y, color ); 
							} 
						} 
					} 
				} 
			} 
		} 
	} 
 
	init = true; 
	 
	// Free the device context. 
	dcMem.DeleteDC(); 
	dcMemt.DeleteDC(); 
} 
 
/////////////////////////////////////////////////////////////////////////////// 
// Draw the edge of the button. 
 
void CToolBarEx::DrawButtonEdge() 
{ 
	short i = 0; 
 
	// Get a paint handle to the toolbar control. 
	CClientDC toolbarDC(this); 
	 
	// Get a handle to the toolbar control to get the button's info. 
	CToolBarCtrl& control = GetToolBarCtrl(); 
	 
	// Loop to get each button. 
	for ( i = 0 ; i < control.GetButtonCount() ; i++ ) 
	{ 
		// Get button's info. 
		GetButtonInfo ( i, id, style, index ); 
		GetItemRect ( i, &button_rect ); 
 
		// Do no draw separators. 
		//if ( style != TBBS_SEPARATOR && style != TBBS_DISABLED ) 
		{ 
			button_rect.left += 1; 
			button_rect.right -= 1; 
			button_rect.top += 1; 
			button_rect.bottom -= 1; 
 
			if ( button_flat == true ) 
			{ 
				// If flat, only the selected button will be displayed. 
				if ( button_rect.left <= mousePosition.x && 
					 button_rect.right >= mousePosition.x && 
					 button_rect.top <= mousePosition.y  && 
					 button_rect.bottom >= mousePosition.y ) 
				{ 
					CClientDC toolbarDC(this); 
					toolbarDC.BitBlt (	0, 0, 
										bk_bm.bmWidth, bk_bm.bmHeight, 
										&dcMemSave, 0, 0, SRCCOPY ); 
 
					if ( style != TBBS_SEPARATOR && style != TBBS_DISABLED ) 
					{ 
						toolbarDC.DrawEdge ( &button_rect, EDGE_BUMP, BF_BOTTOMLEFT ); 
						return; 
					} 
				} 
			} 
  			 
			else 
			{ 
				if ( style != TBBS_SEPARATOR && style != TBBS_DISABLED ) 
				{ 
					if ( button_down == true && i == control.HitTest(&mousePosition) ) 
						toolbarDC.DrawEdge ( &button_rect, EDGE_BUMP, BF_BOTTOMLEFT ); 
					else 
						toolbarDC.DrawEdge ( &button_rect, EDGE_RAISED, BF_RECT ); 
				} 
			} 
		} 
	} 
} 
 
/////////////////////////////////////////////////////////////////////////////// 
// Indicate the mouse moved. 
 
void CToolBarEx::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	mousePosition = point; 
	DrawButtonEdge(); 
	 
	CToolBar::OnMouseMove(nFlags, point); 
} 
 
/////////////////////////////////////////////////////////////////////////////// 
// Indicate a button down/up. 
 
void CToolBarEx::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	button_down = true; 
	DrawButtonEdge(); 
	CToolBar::OnLButtonDown(nFlags, point); 
} 
 
void CToolBarEx::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	button_down = false;	 
	CToolBar::OnLButtonUp(nFlags, point); 
} 
 
/////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////