www.pudn.com > PercentProgress.rar > PercentProgressCtrl.cpp


// PercentProgressCtrl.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "testProgress.h" 
#include "PercentProgressCtrl.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CPercentProgressCtrl 
 
CPercentProgressCtrl::CPercentProgressCtrl() 
{ 
	this->m_Colors[BORDER_COLOR] = RGB(0,0,0); 
	this->m_Colors[PROGRESS_BK_COLOR] = ::GetSysColor(COLOR_BTNFACE); 
	this->m_Colors[PROGRESS_FT_COLOR] = ::GetSysColor(COLOR_HIGHLIGHT); 
	this->m_bPercentShow = TRUE; 
} 
 
CPercentProgressCtrl::~CPercentProgressCtrl() 
{ 
} 
 
 
BEGIN_MESSAGE_MAP(CPercentProgressCtrl, CProgressCtrl) 
	//{{AFX_MSG_MAP(CPercentProgressCtrl) 
	ON_WM_ERASEBKGND() 
	ON_WM_NCPAINT() 
	ON_WM_PAINT() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CPercentProgressCtrl message handlers 
void CPercentProgressCtrl::SetColor(COLOR_DEF item, COLORREF colr, BOOL bReDraw) 
{ 
	ASSERT(item>=BORDER_COLOR && itemm_Colors[item] = colr & 0x00FFFFFF; 
	if ( bReDraw ) 
		this->Invalidate(); 
} 
 
COLORREF CPercentProgressCtrl::GetColor(COLOR_DEF item) 
{ 
	ASSERT(item>=BORDER_COLOR && itemm_Colors[item]; 
} 
 
void CPercentProgressCtrl::SetPercentShow(BOOL bShow, BOOL bReDraw) 
{ 
	this->m_bPercentShow = bShow; 
	if ( bReDraw ) 
		this->Invalidate(); 
} 
 
BOOL CPercentProgressCtrl::IsPercentShow() 
{ 
	return this->m_bPercentShow; 
} 
 
BOOL CPercentProgressCtrl::OnEraseBkgnd(CDC* pDC)  
{ 
	return TRUE; 
} 
 
void CPercentProgressCtrl::OnNcPaint()  
{ 
	CDC *pDC=GetWindowDC(); 
	CRect rect; 
	this->GetWindowRect(&rect); 
	rect.OffsetRect(-rect.left, -rect.top); 
 
	pDC->Draw3dRect(&rect,m_Colors[BORDER_COLOR],m_Colors[BORDER_COLOR]); 
 
	rect.InflateRect(-1,-1,-1,-1); 
	pDC->Draw3dRect(&rect,m_Colors[PROGRESS_BK_COLOR],m_Colors[PROGRESS_BK_COLOR]); 
	this->ReleaseDC(pDC); 
} 
 
void CPercentProgressCtrl::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	CDC *pDC=&dc; 
	pDC->SetBkMode(TRANSPARENT); 
	 
	//获得区域及百分比 
	int nMin, nMax, nPos; 
	this->GetRange(nMin, nMax); 
	nPos = this->GetPos(); 
	float Percent = (nPos-nMin)/(float)(nMax-nMin); 
	CString strTxt; 
	strTxt.Format(_T("%d%%"), (int)(Percent*100)); 
 
	//计算左右/上下区域 
	CRect rt; 
	this->GetClientRect(&rt); 
	CRect LeftRect(rt), RightRect(rt); 
	if ( (this->GetStyle() & PBS_VERTICAL)== PBS_VERTICAL )//垂直 
	{ 
		CRect &TopRect = RightRect; 
		CRect &BottomRect = LeftRect; 
 
		TopRect.bottom = TopRect.bottom * (1-Percent); 
		BottomRect.top = TopRect.bottom; 
	} 
	else 
	{ 
		LeftRect.right = int(LeftRect.right * Percent); 
		RightRect.left = LeftRect.right; 
	} 
	 
	//绘制左右区域 
	pDC->FillSolidRect(&LeftRect, this->m_Colors[PROGRESS_FT_COLOR]); 
	pDC->FillSolidRect(&RightRect, this->m_Colors[PROGRESS_BK_COLOR]); 
 
	//绘制文字 
	if ( this->m_bPercentShow ) 
	{ 
		//先绘制LeftRect上的反色文字 
		CRgn rgn; 
		COLORREF txtColor; 
 
		rgn.CreateRectRgn(LeftRect.left, LeftRect.top, LeftRect.right,  
			LeftRect.bottom); 
		pDC->SelectClipRgn(&rgn); 
		txtColor = 0x00FFFFFF - m_Colors[PROGRESS_FT_COLOR]; 
		pDC->SetTextColor(txtColor);//crBkgnd); 
		pDC->DrawText(strTxt, &rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE); 
		 
		rgn.DeleteObject(); 
		rgn.CreateRectRgn(RightRect.left, RightRect.top, RightRect.right,  
			RightRect.bottom); 
		pDC->SelectClipRgn(&rgn); 
		txtColor = 0x00FFFFFF - m_Colors[PROGRESS_BK_COLOR]; 
		pDC->SetTextColor(txtColor); 
		pDC->DrawText(strTxt, &rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE); 
		rgn.DeleteObject(); 
		pDC->SelectClipRgn(NULL); 
	} 
}