1,183
社区成员
发帖
与我相关
我的任务
分享
procedure FillGradient(Canvas: TCanvas; Color1, Color2: TColor; rect: TRect);
var
Bitmap: TBitmap;
Width, Height: Integer;
Delta: Integer;
x, y, Alpha: Integer;
P, P0: PRGBQuad;
c1, c2, c: TRGBQuad;
begin
if Color1 < 0 then
Color1 := GetSysColor(Color1 and $ff);
if Color2 < 0 then
Color2 := GetSysColor(Color2 and $ff);
c1 := TRGBQuad(RGB(Color1 shr 16, (Color1 shr 8) and $ff, Color1 and $ff));
c2 := TRGBQuad(RGB(Color2 shr 16,(Color2 shr 8) and $ff, Color2 and $ff));
Width := rect.Right - rect.Left;
Height := rect.Bottom - rect.Top;
Delta := Round($1000000 / Width);
Bitmap := TBitmap.Create;
Bitmap.PixelFormat := pf32Bit;
Bitmap.Width := Width;
Bitmap.Height := Height;
P0 := Bitmap.ScanLine[Height - 1];
for x := 0 to Width - 1 do
begin
Alpha := x * Delta;
c.rgbRed := ((Alpha * (c2.rgbRed - c1.rgbRed)) shr 24) + c1.rgbRed;
c.rgbGreen := ((Alpha * (c2.rgbGreen - c1.rgbGreen)) shr 24) + c1.rgbGreen;
c.rgbBlue := ((Alpha * (c2.rgbBlue - c1.rgbBlue)) shr 24) + c1.rgbBlue;
P := P0;
for y := 1 to Height do
begin
P^ := c;
Inc(P, Width);
end;
Inc(P0);
end;
Canvas.Draw(rect.Left, rect.Top, Bitmap);
Bitmap.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FillGradient(Canvas, clYellow, clRed, Rect(0, 0, 1200, 500));
end;