Drawing a Shaded Rectangle by using GradientFill

'

GradientFill found in 'msimg32.dll'

This Delphi source code shows how the GradientFill function can be used to add smooth shading to a triangle or rectangle. For a triangle call the GradientFill function with the three triangle endpoints. GDI will linearly interpolate and fill the triangle.

To add smooth shading to a rectangle, call GradientFill with the upper-left and lower-right coordinates of the rectangle. There are two shading modes used when drawing a rectangle. In horizontal mode, the rectangle is shaded from left-to-right. In vertical mode, the rectangle is shaded from top-to-bottom. First, you need the GradientFill function declaration (Windows.pas contains wrong dectaration of GradientFill function):

type
  TRIVERTEX = packed record
    X, Y : DWORD;
    Red, Green, Blue, Alpha : Word;
  end;

function GradientFill(DC : hDC; pVertex : Pointer; dwNumVertex : DWORD;
  pMesh : Pointer; dwNumMesh, dwMode: DWORD) : DWord; stdcall;
  external 'msimg32.dll';

The following example shows a horizontal rectangle call.

procedure TForm1.Button1Click(Sender: TObject);
var
 vert : array[0..1] of TRIVERTEX;
 gRect   : GRADIENT_RECT;
begin
  vert [0] .x      := 0;
  vert [0] .y      := 0;
  vert [0] .Red    := $0000;
  vert [0] .Green  := $0000;
  vert [0] .Blue   := $0000;
  vert [0] .Alpha  := $0000;

  vert [1] .x      := 100;
  vert [1] .y      := 32;
  vert [1] .Red    := $0000;
  vert [1] .Green  := $0000;
  vert [1] .Blue   := $ff00;
  vert [1] .Alpha  := $0000;

  gRect.UpperLeft  := 0;
  gRect.LowerRight := 1;
  GradientFill(Form1.Canvas.Handle, @vert,2,@gRect,1,GRADIENT_FILL_RECT_H);
end;

This tutorial was kindly provided by Serge Perevoznyk


Google
Web www.Delphi-Central.com
Delphi Central - Delphi Programming Tutorials, Hints and Tips