添加WM_ERASEBKGND的消息相应函数,在函数体如下:
BOOL OnEraseBkgnd(CDC *pDC)
{
CRect rc;
pDC->GetClipBox(&rc);
DrawGradient(pDC->GetSafeHdc(),rc,RGB(255,255,255),RGB(244,128,128),1);
return TRUE;
}
添加绘背景色函数。
void DrawGradient(HDC pDC,const RECT&rect,COLORREF begin,COLORREF end,const int&width)
{
RECT rcstep;
HBRUSH br;
int n,m;
float step = 0.0;
int nred = 0, ngreen = 0, nblue = 0;
float red = 0.0, green = 0.0, blue = 0.0;
nred = (GetRValue(end)-GetRValue(begin));
ngreen = (GetGValue(end)-GetGValue(begin));
nblue = (GetBValue(end)-GetBValue(begin));
step = (float)abs(rect.top-rect.bottom)/(float)width;
red = nred/(float)step;
green = ngreen/(float)step;
blue = nblue/(float)step;
nred = GetRValue(begin);
ngreen = GetRValue(begin);
nblue = GetRValue(begin);
for(int start = 0; start <= step;start++)
{
n=min((int)(rect.top+start*width),rect.bottom);
m=min((int)(rect.top+(start+1)*width),rect.bottom);
::SetRect(&rcstep,rect.left,n,rect.right+1,m);
br = CreateSolidBrush(RGB(nred + red*start,ngreen + green*start,nblue + blue*start));
HBRUSH oldbr = (HBRUSH)::SelectObject(pDC,br);
FillRect(pDC,&rcstep,br);
::SelectObject(pDC,oldbr);
DeleteObject(br);
}
}