本例效果图(颜色失真严重, 因为 GIF 最多只能接受 256 中颜色):
代码文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
TrackBar1: TTrackBar;
TrackBar2: TTrackBar;
RadioGroup1: TRadioGroup;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure TrackBar2Change(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses GDIPOBJ, GDIPAPI;
procedure TForm1.FormCreate(Sender: TObject);
begin
TrackBar1.Max := 100;
TrackBar2.Max := 100;
TrackBar1.Position := TrackBar1.Max;
TrackBar2.Position := TrackBar2.Max;
TrackBar1.ShowSelRange := False;
TrackBar2.ShowSelRange := False;
TrackBar1.Height := 23;
TrackBar2.Height := 23;
RadioGroup1.Items.CommaText := 'BlendTriangularShape, SetBlendBellShape';
RadioGroup1.ItemIndex := 0;
DoubleBuffered := True;
end;
procedure TForm1.FormPaint(Sender: TObject);
const
colors: array[0..0] of TGPColor = (aclRed);
var
g: TGPGraphics;
path: TGPGraphicsPath;
pb: TGPPathGradientBrush;
num: Integer;
begin
g := TGPGraphics.Create(Canvas.Handle);
path := TGPGraphicsPath.Create;
path.AddRectangle(MakeRect(30,10,160,150));
pb:= TGPPathGradientBrush.Create(path);
num := Length(colors);
pb.SetSurroundColors(@colors, num);
pb.SetCenterColor(aclYellow);
{SetBlendTriangularShape 与 SetBlendBellShape 参数2的默认值都是 1.0}
case RadioGroup1.ItemIndex of
0: pb.SetBlendTriangularShape(TrackBar1.Position/100, TrackBar2.Position/100);
1: pb.SetBlendBellShape(TrackBar1.Position/100, TrackBar2.Position/100);
end;
g.FillPath(pb, path);
pb.Free;
path.Free;
g.Free;
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
Repaint;
end;
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
Repaint;
end;
procedure TForm1.TrackBar2Change(Sender: TObject);
begin
Repaint;
end;
end.
窗体文件:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 261
ClientWidth = 221
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnCreate = FormCreate
OnPaint = FormPaint
PixelsPerInch = 96
TextHeight = 13
object TrackBar1: TTrackBar
Left = 0
Top = 167
Width = 113
Height = 45
TabOrder = 0
OnChange = TrackBar1Change
end
object TrackBar2: TTrackBar
Left = 108
Top = 167
Width = 113
Height = 45
TabOrder = 1
OnChange = TrackBar2Change
end
object RadioGroup1: TRadioGroup
Left = 8
Top = 192
Width = 206
Height = 61
Caption = 'RadioGroup1'
TabOrder = 2
OnClick = RadioGroup1Click
end
end