渐变 2

来源:互联网 发布:域名怎么看 编辑:程序博客网 时间:2024/05/21 09:02

绘制渐变色背景

添加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);
}
}

原创粉丝点击