VC++
Example: Draw or fill Gradient
|
|
Carrying out a gradient
fill is something very simple to do. The
following code carries this out.
Aside from this function,
you need to either have a CRect attribute in the class (m_rcClient),
or set the width and height of the fill
in the function itself. Also.
This example
carries out a blue gradient fill.
If you want to use other colours, then you can do so
easily for Red and Green (place the calculation in the Red
or Green component of the RGB call!).
It really depends how
sophisticated you want to get, you typically need to
balance flexibility with speed when it comes to drawing.
void PaintRect(CDC* pDC, const CRect &rc, const COLORREF& color)
{
CBrush brush(color);
pDC->FillRect(rc, &brush);
brush.DeleteObject();
}
// Fill
with gradient color
void GradientFill(CDC* pDC, CRect* prc, int iSegments,COLORREF cptStart, COLORREF cptEnd)
{
COLORREF cr;
int iR = GetRValue(cptStart);
int iG = GetGValue(cptStart);
int iB = GetBValue(cptStart);
int ieB = GetBValue(cptEnd);
int ieG = GetGValue(cptEnd);
int ieR = GetRValue(cptEnd);
if(iSegments > prc->Width())
{
iSegments = prc->Width();
}
int idR = 256 * (ieR - iR) / (iSegments);
int idG = 256 * (ieG - iG) / (iSegments);
int idB = 256 * (ieB - iB) / (iSegments);
iR *= 256;
iG *= 256;
iB *= 256;
ieR *= 256;
ieG *= 256;
ieB *= 256;
int icx = prc->Width() / iSegments, iLeft = prc->left, iRight;
pDC->SelectStockObject(NULL_PEN);
for (int i = 0; i < iSegments; i++, iR += idR, iG += idG, iB += idB)
{
if (i == (iSegments - 1))
{
iRight = prc->right;
}
else
{
iRight = iLeft + icx;
}
if(iR > ieR)
iR = ieR;
if(iG > ieG)
iG = ieG;
if(iB > ieB)
iB = ieB;
cr = RGB(iR / 256, iG / 256, iB / 256);
{
PaintRect(pDC,iLeft,prc->top,iRight+1-iLeft,prc->bottom - prc->top,cr);
}
iLeft = iRight;
}
}
|