# GDI+ 学习记录(10): 线性渐变画刷 - LinearGradientBrush

//线性渐变画刷
var
g: TGPGraphics;
rect: TGPRect;
begin
g := TGPGraphics.Create(Canvas.Handle);
rect := MakeRect(11,11,222,111);

g.FillEllipse(lb, rect);

lb.Free;
g.Free;
end;

//定义渐变的位置与颜色
var
g : TGPGraphics;
p: TGPPen;
p1,p2: TGPPoint;
c1,c2: TGPColor;
begin
g := TGPGraphics.Create(Canvas.Handle);

p1 := MakePoint(0, 0);
p2 := MakePoint(200, 100);
c1 := MakeColor(255, 0, 0, 255);
c2 := MakeColor(255, 0, 255, 0);

lb := TGPLinearGradientBrush.Create(p1, p2, c1, c2);

p:= TGPPen.Create(lb, 10);

g.DrawLine(p, 0, 0, 600, 300);
g.FillEllipse(lb, 10, 100, 200, 100);

lb.Free;
p.Free;
g.Free;
end;

//自定义渐变过渡效果
var
g : TGPGraphics;
p1,p2: TGPPoint;
c1,c2: TGPColor;
const
Factors: array[0..2] of Single = (0.0, 0.5, 1.0);   {颜色比例}
Positions: array[0..2] of Single = (0.0, 0.2, 1.0); {位置比例, 两个数组元素须相同}
begin
g := TGPGraphics.Create(Canvas.Handle);

p1 := MakePoint(0, 10);
p2 := MakePoint(200, 10);
c1 := aclBlack;
c2 := aclRed;

lb := TGPLinearGradientBrush.Create(p1, p2, c1, c2);

lb.SetBlend(@Factors, @Positions, 3); {设置渐变因子}

g.FillEllipse(lb, 0, 30, 200, 100);
g.FillRectangle(lb, 0, 155, 500, 30);

lb.Free;
g.Free;
end;



Delphi 微软 说明