// WEProgressCtrl.cpp : 实现文件
//
#include "stdafx.h"
#include "自绘进度条.h"
#include "WEProgressCtrl.h"
#include ".\weprogressctrl.h"
// CWEProgressCtrl
IMPLEMENT_DYNAMIC(CWEProgressCtrl, CProgressCtrl)
CWEProgressCtrl::CWEProgressCtrl()
{
m_nRangeMin = 0; //变化范围的最小值
m_nRangeMax = 0; //变化范围的最大值
m_nPos = 0; //当前位置
m_bmpProgress.LoadBitmap(IDB_BITMAP);
m_bmpProgress.GetBitmap(&m_bmpData);
}
CWEProgressCtrl::~CWEProgressCtrl()
{
}
void CWEProgressCtrl::SetRange(int nMin, int nMax)
{
m_nRangeMin = nMin; //变化范围的最小值
m_nRangeMax = nMax; //变化范围的最大值
}
void CWEProgressCtrl::SetPos(int nPos)
{
m_nPos=nPos;
Invalidate();
}
void CWEProgressCtrl::DrawPicture(CDC* pDC, CRect rect)
{
CBitmap *pOldBitmap;
CDC* pMemDC = new CDC;
//读位图到内存
pMemDC->CreateCompatibleDC(pDC);
pOldBitmap = pMemDC->SelectObject(&m_bmpProgress);
pDC->StretchBlt(rect.left, rect.top, rect.Width(), rect.Height(), pMemDC, 0, 0, m_bmpData.bmWidth, m_bmpData.bmHeight, SRCCOPY);
pMemDC->SelectObject(pOldBitmap);
//释放内存
ReleaseDC(pMemDC);
delete pMemDC;
}
BEGIN_MESSAGE_MAP(CWEProgressCtrl, CProgressCtrl)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_NCPAINT()
END_MESSAGE_MAP()
// CWEProgressCtrl 消息处理程序
void CWEProgressCtrl::OnPaint()
{
CRect rectProgress, rectClient;
float fPercent;
int nLength;
CPaintDC dc(this);
CDC* pMemDC=new CDC;
CBitmap bitmapTemp, *pOldBitmap;
//获取并计算相关数据
GetClientRect(rectClient);
fPercent = (float)(m_nPos-m_nRangeMin)/(m_nRangeMax-m_nRangeMin);
nLength = int(rectClient.Width()*fPercent);
rectProgress.SetRect(0, 0, nLength, rectClient.Height());
//创建位图内存
bitmapTemp.CreateCompatibleBitmap(&dc, rectClient.Width(), rectClient.Height());
pMemDC->CreateCompatibleDC(&dc);
pOldBitmap=pMemDC->SelectObject(&bitmapTemp);
//填充客户区
pMemDC->FillSolidRect(&rectClient, RGB(215, 160, 130));
DrawPicture(pMemDC, rectProgress);
dc.BitBlt(0, 0, rectClient.Width(), rectClient.Height(), pMemDC, 0, 0, SRCCOPY);
pMemDC->SelectObject(pOldBitmap);
ReleaseDC(pMemDC);
delete pMemDC;
}
BOOL CWEProgressCtrl::OnEraseBkgnd(CDC* pDC)
{
return true;
}
void CWEProgressCtrl::OnNcPaint()
{
CRect rectProgress;
CBrush brushFrame;
CDC* pDC= GetWindowDC();
//设置编辑框的区域
GetWindowRect(rectProgress);
ScreenToClient(rectProgress);
rectProgress.OffsetRect(CSize(1, 1));
//创建画笔
brushFrame.CreateSolidBrush(RGB(215, 85, 15));
//画边框
pDC->FrameRect(rectProgress, &brushFrame);
ReleaseDC(pDC);
}