1.先添加绘制函数:
void CGradualColorWndDlg::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=GetGValue(begin);
nblue=GetBValue(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);
}
}
2.增加消息函数:afx_msg BOOL OnEraseBkgnd(CDC* pDC); // .h文件中
BOOL CGradualColorWndDlg::OnEraseBkgnd(CDC* pDC) // .cpp文件中
{
CRect rc;
pDC->GetClipBox(&rc);
DrawGradient(pDC->GetSafeHdc(), rc, RGB(128,255,0), RGB(0,0,255), 1);
return TRUE;
}
3.在OnPaint()函数中加入消息发送:SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);