A Journey 2 Eternity

Draw a Gradient Rectangle in MFC

Posted on: September 9, 2008

void CEMCHotButton::DrawGradientRect (CDC *pDC, CRect r, COLORREF cLeft, COLORREF cRight, BOOL bVertical)
{
	CRect stepR; // rectangle for color’s band
	COLORREF color; // color for the bands
	float fStep;
	
	if(bVertical) {
		fStep = ((float)r.Height())/255.0f;
	} else {
		fStep = ((float)r.Width())/255.0f; // width of color’s band
	}

	for (int iOnBand = 0; iOnBand < 255; iOnBand++) {
		// set current band
		if(bVertical) {
			SetRect(&stepR, r.left, r.top+(int)(iOnBand * fStep), r.right, r.top+(int)((iOnBand+1)* fStep));
		} else {
			SetRect(&stepR, r.left+(int)(iOnBand * fStep), r.top, r.left+(int)((iOnBand+1)* fStep), r.bottom);
		}

		// set current color
		color = RGB((GetRValue(cRight)-GetRValue(cLeft))*((float)iOnBand)/255.0f+GetRValue(cLeft),
						(GetGValue(cRight)-GetGValue(cLeft))*((float)iOnBand)/255.0f+GetGValue(cLeft),
						(GetBValue(cRight)-GetBValue(cLeft))*((float)iOnBand)/255.0f+GetBValue(cLeft));

		// fill current band
		pDC->FillSolidRect(stepR,color);
	}
}
Advertisements
Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Pages

Categories

September 2008
M T W T F S S
« Aug   Dec »
1234567
891011121314
15161718192021
22232425262728
2930  

Blog Stats

  • 27,176 hits
%d bloggers like this: