Draw a gradient fill on a form

Author: Doctor Nam 

 

implementation

 

{$R *.DFM}

 

procedure FillGradientRect(Canvas: TCanvas; Recty: TRect; fbcolor, fecolor: TColor; fcolors: Integer);

var

  i, j, h, w, fcolor: Integer;

  R, G, B: Longword;

  beginRGBvalue, RGBdifference: array[0..2] of Longword;

begin

  beginRGBvalue[0] := GetRvalue(colortoRGB(FBcolor));

  beginRGBvalue[1] := GetGvalue(colortoRGB(FBcolor));

  beginRGBvalue[2] := GetBvalue(colortoRGB(FBcolor));

 

  RGBdifference[0] := GetRvalue(colortoRGB(FEcolor)) - beginRGBvalue[0];

  RGBdifference[1] := GetGvalue(colortoRGB(FEcolor)) - beginRGBvalue[1];

  RGBdifference[2] := GetBvalue(colortoRGB(FEcolor)) - beginRGBvalue[2];

 

  Canvas.pen.Style := pssolid;

  Canvas.pen.mode := pmcopy;

  j := 0;

  h := recty.Bottom - recty.Top;

  w := recty.Right - recty.Left;

 

  for i := fcolors downto 0 do

  begin

    recty.Left  := muldiv(i - 1, w, fcolors);

    recty.Right := muldiv(i, w, fcolors);

    if fcolors1 then

    begin

      R := beginRGBvalue[0] + muldiv(j, RGBDifference[0], fcolors);

      G := beginRGBvalue[1] + muldiv(j, RGBDifference[1], fcolors);

      B := beginRGBvalue[2] + muldiv(j, RGBDifference[2], fcolors);

    end;

    Canvas.Brush.Color := RGB(R, G, B);

    patBlt(Canvas.Handle, recty.Left, recty.Top, Recty.Right - recty.Left, h, patcopy);

    Inc(j);

  end;

end;

 

//case 1

 

procedure TForm1.FormPaint(Sender: TObject);

begin

  FillGradientRect(Form1.Canvas, rect(0, 0, Width, Height), $FF0000, $00000, $00FF);

end;

 

 

//case 2

procedure TForm1.FormPaint(Sender: TObject);

var

  Row, Ht: Word;

  IX: Integer;

begin

  iX := 200;

  Ht := (ClientHeight + 512) div 256;

  for Row := 0 to 512 do

  begin

    with Canvas do 

    begin

      Brush.Color := RGB(Ix, 0, row);

      FillRect(Rect(0, Row * Ht, ClientWidth, (Row + 1) * Ht));

      IX := (IX - 1);

    end;

  end;

end;

 

 

{

  Note, that the OnResize event should also call the FormPaint

  method if this form is allowed to be resizable.

  This is because if it is not called then when the

  window is resized the gradient will not match the rest of the form.

}

 

stopniowa zmiana koloru okienka...