There are 2 ways to draw gradient colors collected on the Internet:
The first:
void CChatterDlg::GradientRect(CDC* pDC,CRect rc,COLORREF color1,COLORREF color2,BOOL bvh)
{
TRIVERTEX vert[2] ;
GRADIENT_RECT gRect;
vert [0] .x = rc.left;//0;
vert [0] .y = rc.top;//0;
vert [0] .Red = COLOR16( COLOR16( GetRValue( color1 ) ) << 8);//一个16位的颜色值,但低8位为0
vert [0] .Green = COLOR16( COLOR16( GetGValue( color1 ) ) << 8);
vert [0] .Blue = COLOR16( COLOR16( GetBValue( color1 ) ) << 8);
//vert [1] .Alpha = 0x0000;
vert [1] .x = rc.right;
vert [1] .y = rc.bottom;
vert [1] .Red = COLOR16( COLOR16( GetRValue( color2 ) ) << 8);
vert [1] .Green = COLOR16( COLOR16( GetGValue( color2 ) ) << 8);
vert [1] .Blue = COLOR16( COLOR16( GetBValue( color2 ) ) << 8);
//vert [0] .Alpha = 0xf000;
gRect.UpperLeft = 0;
gRect.LowerRight = 1;
GradientFill( pDC->GetSafeHdc(), //hDC是表示要填充对象的窗口句柄;
vert, //pVertex常常是一个数组,用来存放各顶点的位置及颜色信息,顶点在TRIVERTEX中定义;
2, //dwNumVertex表示顶点的个数;
&gRect, //pMesh也常常是一个数组结构,表示组成图形的各顶点顺序,表示一个矩形用两个顶点,三角形要用三个顶点;
1, //dwNumMesh表示矩形或三角形的个数;
bvh == TRUE ? GRADIENT_FILL_RECT_V : GRADIENT_FILL_RECT_H);
//GRADIENT_FILL_RECT_H );//dwMode表示填充的模式:水平填充,垂直填充,三角形填充。
}
The second:
//得到客户区域的填充矩形
int nWidth = rect.Width();
int nHeight = rect.Height();
CRect rectangle;
//分割客户区域成小矩形,逐个填充
for(int i=0; i<nHeight; ++i)
{
rectangle.SetRect(0,i,nWidth,i+1);
BufferDC.FillSolidRect(&rectangle, RGB(255, 255, 255-MulDiv(i, 255, nHeight)));
}