A Journey 2 Eternity

Posts Tagged ‘Gradient

Here’s a sample which draws background of a dialog with gradient colors.

BOOL CMyDlg::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(&rect);

TRIVERTEX vert[4] = {
{ rect.left, rect.top, 0xff00, 0xff00, 0xff00, 0 },
{ rect.right, rect.top, 0, 0xff00, 0xff00, 0 },
{ rect.right, rect.bottom, 0xff00, 0, 0xff00, 0 },
{ rect.left, rect.bottom, 0xff00, 0xff00, 0, 0 }
};

GRADIENT_TRIANGLE grad[2] = {
{ 0, 1, 2 },
{ 0, 2, 3 }
};

::GradientFill( pDC->m_hDC, vert, 4, grad, 2, GRADIENT_FILL_TRIANGLE );

return TRUE;
}

void CMyDlg::OnSize(UINT nType, int cx, int cy)
{
// To ensure the background of the whole dialog is re-drawn
Invalidate();
CDialog::OnSize(nType, cx, cy);
}
Advertisements
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);
	}
}
Tags:

Pages

Categories

August 2018
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Blog Stats

  • 28,711 hits
Advertisements