【WTL】控件自绘之ProgressBar

头文件


class CXLProgressBar : public CWindowImpl<CXLProgressBar, CProgressBarCtrl>
{
public:
	CXLProgressBar(void);
	~CXLProgressBar(void);

	BEGIN_MSG_MAP(CXLProgressBar)
		MSG_WM_PAINT(OnPaint)
	END_MSG_MAP()

protected:
	void OnPaint(CDCHandle);

private:
	void LoadPngs();

	void DrawInMemory(CDC& dc, CRect& rcClient);
	void DrawBkGnd(CDC& dc, CRect& rcClient);
	void DrawProgress(CDC& dc, CRect& rcClient);

private:
	CImage              m_ProgressLeft;
	CImage				m_ProgressCenter;
	CImage				m_ProgressRight;
	CImage				m_ProgressBKLeft;
	CImage				m_ProgressBKCenter;
	CImage				m_ProgressBKRight;
};

源文件 


CXLProgressBar::CXLProgressBar(void)
{
	LoadPngs();
}

CXLProgressBar::~CXLProgressBar(void)
{
}

void CXLProgressBar::OnPaint(CDCHandle)
{
	CPaintDC dcPaint(m_hWnd); 

	CRect rcClient; 
	GetClientRect(rcClient); 

	CMemoryDC dcMemory(dcPaint.m_hDC, rcClient); 

 	CRect rcClip; 
 	if (dcPaint.GetClipBox( rcClip ) == ERROR)  return;
	dcMemory.BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), dcPaint, 0, 0, SRCCOPY);

	DrawInMemory(dcMemory, rcClient);
}

void CXLProgressBar::DrawInMemory(CDC& dc, CRect& rcClient)
{
	DrawBkGnd(dc, rcClient);
	DrawProgress(dc, rcClient);
}

void CXLProgressBar::DrawBkGnd(CDC& dc, CRect& rcClient)
{
	CRect rcBKLeft;
	CRect rcBKCenter;
	CRect rcBKRight;

	rcBKLeft.SetRect(rcClient.left, rcClient.top, m_ProgressBKLeft.GetWidth(), rcClient.bottom);
	rcBKRight.SetRect(rcClient.right-m_ProgressBKRight.GetWidth(), rcClient.top, rcClient.right, rcClient.bottom);
	rcBKCenter.SetRect(rcBKLeft.right, rcBKLeft.top, rcBKRight.left, rcBKLeft.bottom);

	m_ProgressBKLeft.Draw(dc, rcBKLeft);
	m_ProgressBKCenter.Draw(dc, rcBKCenter);
	m_ProgressBKRight.Draw(dc, rcBKRight);
}

void CXLProgressBar::DrawProgress(CDC& dc, CRect& rcClient)
{
	int nMin, nMax;
	GetRange(nMin, nMax);

	int nPos = GetPos(); if(nPos == 0) return;
	int nProgressWidth = (rcClient.Width() * nPos) / (nMax - nMin);

	//保证左右俩个进度条图片被画出来
	if(nProgressWidth <= m_ProgressLeft.GetWidth()+m_ProgressRight.GetWidth())
	{
		CRect rcLeft;
		CRect rcRight;

		rcLeft.SetRect(rcClient.left, rcClient.top, m_ProgressLeft.GetWidth(), rcClient.bottom);
		rcRight.SetRect(m_ProgressLeft.GetWidth(), rcClient.top, m_ProgressLeft.GetWidth()+m_ProgressRight.GetWidth(), rcClient.bottom);

		m_ProgressLeft.Draw(dc, rcLeft);
		m_ProgressRight.Draw(dc, rcRight);
	}
	else
	{
		CRect rcProgress;
		rcProgress.SetRect(rcClient.left, rcClient.top, nProgressWidth, rcClient.bottom);

		CRect rcLeft;
		CRect rcCenter;
		CRect rcRight;

		rcLeft.SetRect(rcProgress.left, rcProgress.top, m_ProgressLeft.GetWidth(), rcProgress.bottom);
		rcRight.SetRect(rcProgress.right-m_ProgressRight.GetWidth(), rcProgress.top, rcProgress.right, rcProgress.bottom);
		rcCenter.SetRect(rcLeft.right, rcLeft.top, rcRight.left, rcLeft.bottom);

		m_ProgressLeft.Draw(dc, rcLeft);
		m_ProgressCenter.Draw(dc, rcCenter);
		m_ProgressRight.Draw(dc, rcRight);
	}
}

void CXLProgressBar::LoadPngs()
{
	Utility::LoadImageFromResource(&m_ProgressLeft, IDR_PNG_LEFT, _T("PNG"));
	Utility::LoadImageFromResource(&m_ProgressCenter, IDR_PNG_CENTER, _T("PNG"));
	Utility::LoadImageFromResource(&m_ProgressRight, IDR_PNG_RIGHT, _T("PNG"));
	Utility::LoadImageFromResource(&m_ProgressBKLeft, IDR_PNG_BKG_LEFT, _T("PNG"));
	Utility::LoadImageFromResource(&m_ProgressBKCenter, IDR_PNG_BKG_CENTER, _T("PNG"));
	Utility::LoadImageFromResource(&m_ProgressBKRight, IDR_PNG_BKG_RIGHT, _T("PNG"));

	Utility::PreMultiply(m_ProgressLeft);
	Utility::PreMultiply(m_ProgressCenter);
	Utility::PreMultiply(m_ProgressRight);
	Utility::PreMultiply(m_ProgressBKLeft);
	Utility::PreMultiply(m_ProgressBKCenter);
	Utility::PreMultiply(m_ProgressBKRight);
}


你可能感兴趣的:(【WTL】控件自绘之ProgressBar)