void PaintGradiantRect(CDC *pDC, const RECT &rect,
COLORREF clrFrom, COLORREF clrTo =RGB(255,255,255),
BOOL hori = true, BOOL ascend = true)
{
int cxCap = rect.right - rect.left;
int cyCap = rect.bottom - rect.top;
// Get the intensity values for the ending color
int r1 = GetRValue(clrTo); // red
int g1 = GetGValue(clrTo); // green
int b1 = GetBValue(clrTo); // blue
// Get the intensity values for the begining color
int r2 = GetRValue(clrFrom); // red
int g2 = GetGValue(clrFrom); // green
int b2 = GetBValue(clrFrom); // blue
int r, g, b;
if(hori) //paint horizontal rect;
{
int x = cxCap;
int w = x; // width of area to shade
int xDelta= max(w/NCOLORSHADES,1); // width of one shade band
while (x >= xDelta) {
x -= xDelta;
if (r1 > r2)
r = r1 - (r1-r2)*(w-x)/w;
else
r = r1 + (r2-r1)*(w-x)/w;
if (g1 > g2)
g = g1 - (g1-g2)*(w-x)/w;
else
g = g1 + (g2-g1)*(w-x)/w;
if (b1 > b2)
b = b1 - (b1-b2)*(w-x)/w;
else
b = b1 + (b2-b1)*(w-x)/w;
if(ascend) // Paint from left to right;
PaintRect(pDC, rect.left+x, rect.top, xDelta, cyCap, RGB(r, g, b));
else // Paint from right to left;
PaintRect(pDC, rect.right-x-xDelta, rect.top, xDelta, cyCap, RGB(r, g, b));
}
}
else //paint vertical rect;
{
int y = cyCap;
int w = y; // height of area to shade
int yDelta= max(w/NCOLORSHADES,1); // height of one shade band
while (y >= yDelta) {
y -= yDelta;
if (r1 > r2)
r = r1 - (r1-r2)*(w-y)/w;
else
r = r1 + (r2-r1)*(w-y)/w;
if (g1 > g2)
g = g1 - (g1-g2)*(w-y)/w;
else
g = g1 + (g2-g1)*(w-y)/w;
if (b1 > b2)
b = b1 - (b1-b2)*(w-y)/w;
else
b = b1 + (b2-b1)*(w-y)/w;
if(ascend) // Paint from top to bottom;
PaintRect(pDC, rect.left, rect.top+y, cxCap, yDelta, RGB(r, g, b));
else // Paint from bottom to top;
PaintRect(pDC, rect.left, rect.bottom-y-yDelta, cxCap, yDelta, RGB(r, g, b));
}
}