?? gradientprogressctrl.cpp
字號:
// GradientProgressCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "GradientProgressCtrl.h"
#include "MemDC.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGradientProgressCtrl
CGradientProgressCtrl::CGradientProgressCtrl()
{
m_nLower=0;
m_nUpper=100;
m_nCurrentPosition=0;
m_nStep=10;
//Initial Color of show
m_clrStart= COLORREF(RGB(255,0,0));
m_clrEnd= COLORREF(RGB(0,0,255));
m_clrBkGround=::GetSysColor(COLOR_3DFACE);
m_clrText=COLORREF(RGB(255,255,255));
//Not show Word
m_bShowPercent=FALSE;
}
CGradientProgressCtrl::~CGradientProgressCtrl()
{
}
BEGIN_MESSAGE_MAP(CGradientProgressCtrl, CProgressCtrl)
//{{AFX_MSG_MAP(CGradientProgressCtrl)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGradientProgressCtrl message handlers
void CGradientProgressCtrl::SetRange(int nLower,int nUpper)
{
//This Function is to Set Range of the progress
m_nLower=nLower;
m_nUpper=nUpper;
m_nCurrentPosition=nLower;
CProgressCtrl::SetRange(nLower,nUpper);
}
int CGradientProgressCtrl::SetStep(int nStep)
{
m_nStep=nStep;
return (CProgressCtrl::SetStep(nStep));
}
BOOL CGradientProgressCtrl::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;//CProgressCtrl::OnEraseBkgnd(pDC);
}
void CGradientProgressCtrl::DrawGradient(CPaintDC *pDC, const RECT &rectClient, const int &nMaxWidth)
{
RECT rectFill; //顯示區域
float fStep; //每一步的幅寬
CBrush brush; //顯示的顏色畫刷
CMemDC memDC(pDC);
int r,g,b;
float rStep,gStep,bStep;
//得到不同顏色并相減,返回顏色之間的最大差值
r=(GetRValue(m_clrEnd)-GetRValue(m_clrStart));
g=(GetGValue(m_clrEnd)-GetGValue(m_clrStart));
b=(GetBValue(m_clrEnd)-GetBValue(m_clrStart));
//使進程條顯示的總數 等于最大的顏色差值
int nSteps=max(abs(r),max(abs(g),abs(b)));
//確定每一顏色填充多大的矩形區域
fStep=(float)rectClient.right/(float)nSteps;
//設置每一顏色填充的步數
rStep=r/(float)nSteps;
gStep=g/(float)nSteps;
bStep=b/(float)nSteps;
r=GetRValue(m_clrStart);
g=GetGValue(m_clrStart);
b=GetBValue(m_clrStart);
//繪制顏色漸變的進程條
for(int iOnBand=0;iOnBand<nSteps;iOnBand++)
{
::SetRect(&rectFill,
//以下為填充矩形區域的左上角x,y和右下角x,y
(int)(iOnBand*fStep),
0,
(int)((iOnBand+1)*fStep),
rectClient.bottom+1);
VERIFY(brush.CreateSolidBrush(RGB(r+rStep*iOnBand,g+gStep*iOnBand,b+bStep*iOnBand)));
memDC.FillRect(&rectFill,&brush);
VERIFY(brush.DeleteObject());
//在結束繪制之前,使用背景色填充乘下的的客戶區域
if(rectFill.right>nMaxWidth)
{
::SetRect(&rectFill,rectFill.right,0,rectClient.right,rectClient.bottom);
VERIFY(brush.CreateSolidBrush(m_clrBkGround));
memDC.FillRect(&rectFill,&brush);
VERIFY(brush.DeleteObject());
return;
}
}
}
void CGradientProgressCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
if(m_nCurrentPosition<=m_nLower||m_nCurrentPosition>=m_nUpper)
{
CRect rect;
GetClientRect(rect);
CBrush brush;
brush.CreateSolidBrush(::GetSysColor(COLOR_3DFACE));
dc.FillRect(&rect,&brush);
VERIFY(brush.DeleteObject());
return;
}
CRect rectClient;
GetClientRect(rectClient);
float maxWidth((float)m_nCurrentPosition/(float)m_nUpper*(float)rectClient.right);
//繪制
DrawGradient(&dc,rectClient,(int)maxWidth);
//顯示進程條進度文字
if(m_bShowPercent)
{
CString percent;
percent.Format("%d%%",(int)(100*(float)m_nCurrentPosition/m_nUpper));
dc.SetTextColor(m_clrText);
dc.SetBkMode(TRANSPARENT);
dc.DrawText(percent,&rectClient,DT_VCENTER|DT_CENTER|DT_SINGLELINE);
}
// Do not call CProgressCtrl::OnPaint() for painting messages
}
int CGradientProgressCtrl::SetPos(int nPos)
{
//Set the Position of the Progress
m_nCurrentPosition=nPos;
return (CProgressCtrl::SetPos(nPos));
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -