WinapiZone.net




Tutorials > WinAPI > Functions > GradientFill

GradientFill

This function is a replacement of the GradientFill function of Windows, that is bugged under windows 9x and ME.

void GradientFillRect(HDC hdc, LPRECT rcGradient, COLORREF firstColor, COLORREF secondColor, BOOL isVertical = FALSE) { BYTE startRed = GetRValue(firstColor); BYTE startGreen = GetGValue(firstColor); BYTE startBlue = GetBValue(firstColor); BYTE endRed = GetRValue(secondColor); BYTE endGreen = GetGValue(secondColor); BYTE endBlue = GetBValue(secondColor); HBRUSH endColor = CreateSolidBrush(secondColor); FillRect(hdc, rcGradient, endColor); DeleteObject(endColor); //Gradient line width/height int dy = 2; int length = (isVertical ? rcGradient->bottom - rcGradient->top : rcGradient->right - rcGradient->left) - dy; for (int dn = 0; dn <= length; dn += dy) { BYTE currentRed = (BYTE)MulDiv(endRed-startRed, dn, length) + startRed; BYTE currentGreen = (BYTE)MulDiv(endGreen-startGreen, dn, length) + startGreen; BYTE currentBlue = (BYTE)MulDiv(endBlue-startBlue, dn, length) + startBlue; RECT currentRect = {0}; if (isVertical) { currentRect.left = rcGradient->left; currentRect.top = rcGradient->top + dn; currentRect.right = currentRect.left + rcGradient->right - rcGradient->left; currentRect.bottom = currentRect.top + dy; } else { currentRect.left = rcGradient->left + dn; currentRect.top = rcGradient->top; currentRect.right = currentRect.left + dy; currentRect.bottom = currentRect.top + rcGradient->bottom - rcGradient->top; } HBRUSH currentColor = CreateSolidBrush( RGB(currentRed,currentGreen,currentBlue) ); FillRect(hdc, &amp;currentRect, currentColor); DeleteObject(currentColor); } }

Arguments

hdc
A handle to the device context the gradient must be drawn to
rcGradient
A pointer to a RECT structure that specify the rect of the device context where to draw the gradient
firstColor
The first color of the gradient. If it's a vertical gradient, this is the color at the top. If it's an horizontal gradient, this is the leftmost color.
secondColor
The second color of the gradient. If it's a vertical gradient, this is the color at the bottom. If it's an horizontal gradient, this is rightmost color.
isVertical
Use FALSE if you want a horizontal gradient (from left to right), or TRUE if you want a vertical gradient (from top to bottom). The default value is FALSE (horizontal gradient).

Comments

Anonimo wrote:

nice
(18.05.2009, 00:21)

Your comment:

Name:
E-mail or homepage:
Captcha:
captcha

Script by Alex



This page was last modified 34 months, 3 weeks, 2 days and 9 hours ago