TMyPainter =
class(TcxSheetPainter)
private
procedure GradientRect(
const ARect: TRect; IsHorizontal, IsSelected: Boolean);
end;
procedure TMyPainter.GradientRect(
const ARect: TRect;
IsHorizontal, IsSelected: Boolean);
//Returns the Red, Green and Blue components of the color
procedure ColorToRGBValues(AColor: TColor;
var AR, AG, AB: Byte);
begin
AColor := ColorToRGB(AColor);
AR := GetRValue(AColor);
AG := GetGValue(AColor);
AB := GetBValue(AColor);
end;
var
I, dR, dG,
dB: Integer;
R, G, B, R1, G1, B1: Byte;
//rectangle filled previously
PrevLine: TRect;
//rectangle to fill currently
LineRect: TRect;
ABrush: HBrush;
const
Colors:
array[Boolean, 0..1]
of TColor =
((clWhite, clBtnShadow), (clHighLight, clWhite));
begin
//get the Red, Green and Blue components of the starting color
//for the gradient
ColorToRgbValues(Colors[IsSelected, 0], R1, G1, B1);
//get the Red, Green and Blue components of the ending color
//for the gradient
ColorToRgbValues(Colors[IsSelected, 1], R, G, B);
dR := R - R1;
dG := G - G1;
dB := B - B1;
LineRect := ARect;
PrevLine := ARect;
for I := 0
to 255
do
begin
with ARect
do
begin
if IsHorizontal
then
//determine rectangle boundaries for horizontal filling
begin
LineRect.Top := Top + MulDiv(I, Bottom - Top, $100);
LineRect.Bottom := Top + MulDiv(I + 1, Bottom - Top, $100);
end
else
//determine rectangle boundaries for vertical filling
begin
LineRect.Left := Left + MulDiv(I, Right - Left, $100);
LineRect.Right := Left + MulDiv(I + 1, Right - Left, $100);
end;
//step over if the same rectangle was filled before
if EqualRect(PrevLine, LineRect)
then
Continue;
//define colors to fill
R := R1 + MulDiv(I, dR, $FF);
G := G1 + MulDiv(I, dG, $FF);
B := B1 + MulDiv(I,
dB, $FF);
//create a brush to fill
ABrush := CreateSolidBrush(
RGB(R, G, B));
try
FillRect(Canvas.Canvas.Handle, LineRect, ABrush);
finally
DeleteObject(ABrush);
end;
end;
end;
end;