GDI+ 有五種畫刷:
實心畫刷: IGPSolidBrush
陰影畫刷: IGPHatchBrush
紋理畫刷: IGPTextureBrush
線性漸變畫刷: IGPLinearGradIEntBrush
路徑漸變畫刷: IGPPathGradIEntBrush
它們分別有對應的實現類:
TGPSolidBrush
TGPHatchBrush
TGPTextureBrush
TGPLinearGradIEntBrush
TGPPathGradIEntBrush.
五個畫刷都繼承自 IGPBrush, 但 IGPBrush 只能通過其子類實現, 沒有對應的 TGPBrush.
IGPBrush 只有兩個有意義的成員:
IGPBrush.Clone 方法: 克隆對象; 系列中大多接口提供了這個方法以方便對象復制.
IGPBrush.BrushType 只讀屬性: 其對應的枚舉當然會有五個元素, 分別對應上面五種畫刷.
下面的例子是 BrushType 屬性的測試:
uses GdiPlus, TypInfo;
procedure TForm1.Button1Click(Sender: TObject);
var
B1,B2,B3,B4,B5: IGPBrush; //定義五個畫刷, 將要生成五種不同類型的畫刷
T1,T2,T3,T4,T5: string; //用於獲取畫刷類型
C1,C2: TGPColor;
Pt1,Pt2: TGPPoint;
Image: IGPImage;
Path: IGPGraphicsPath;
begin
{生成下面畫刷需要的參數}
C1 := $FFFF0000;
C2 := $FF666666;
Pt1.Initialize(0, 0);
Pt2.Initialize(10, 10);
Image := TGPImage.Create('C:\GdiPlusImg\Texture.jpg');
Path := TGPGraphicsPath.Create;
Path.AddEllipse(0, 0, 10, 10);
{分別生成五種不同類型的畫刷}
B1 := TGPSolidBrush.Create(C1);
B2 := TGPHatchBrush.Create(HatchStyleHorizontal, C1, C2);
B3 := TGPTextureBrush.Create(Image);
B4 := TGPLinearGradIEntBrush.Create(Pt1, Pt2, C1, C2);
B5 := TGPPathGradIEntBrush.Create(Path);
{獲取各畫刷的枚舉名稱}
T1 := GetEnumName(TypeInfo(TGPBrushType), Ord(B1.BrushType));
T2 := GetEnumName(TypeInfo(TGPBrushType), Ord(B2.BrushType));
T3 := GetEnumName(TypeInfo(TGPBrushType), Ord(B3.BrushType));
T4 := GetEnumName(TypeInfo(TGPBrushType), Ord(B4.BrushType));
T5 := GetEnumName(TypeInfo(TGPBrushType), Ord(B5.BrushType));
ShowMessage(T1); //BrushTypeSolidColor
ShowMessage(T2); //BrushTypeHatchFill
ShowMessage(T3); //BrushTypeTextureFill
ShowMessage(T4); //BrushTypePathGradIEnt
ShowMessage(T5); //BrushTypeLinearGradIEnt
end;
有必要先總覽一下各畫刷的基本效果, 下面是測試程序:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses GdiPlus, GdiPlusHelpers;
var Rect: TGPRect = (X: 30; Y: 10; Width: 150; Height: 200);
procedure TForm1.Button1Click(Sender: TObject);
var
Brush: IGPSolidBrush;
begin
Brush := TGPSolidBrush.Create($FFFF0000);
Canvas.ToGPGraphics.FillEllipse(Brush, Rect);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Brush: IGPHatchBrush;
begin
Brush := TGPHatchBrush.Create(HatchStyleDiagonalCross, $FF0000FF, $FFCCCCCC);
Canvas.ToGPGraphics.FillEllipse(Brush, Rect);
end;
procedure TForm1.Button3Click(Sender: TObject);
var
Brush: IGPTextureBrush;
begin
Brush := TGPTextureBrush.Create(TGPImage.Create('C:\GdiPlusImg\Texture.jpg'));
Canvas.ToGPGraphics.FillEllipse(Brush, Rect);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
Brush: IGPLinearGradIEntBrush;
begin
Brush := TGPLinearGradIEntBrush.Create(Rect, $FFFF0000, $FF0000FF, 90);
Canvas.ToGPGraphics.FillEllipse(Brush, Rect);
end;
procedure TForm1.Button5Click(Sender: TObject);
var
Brush: IGPPathGradIEntBrush;
Path: IGPGraphicsPath;
begin
Path := TGPGraphicsPath.Create;
Path.AddEllipse(Rect);
Brush := TGPPathGradIEntBrush.Create(Path);
Brush.CenterColor := $FFFF0000;
Brush.SetSurroundColors([$FFFFFF00]);
Canvas.ToGPGraphics.FillEllipse(Brush, Rect);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption := '實心畫刷';
Button2.Caption := '陰影畫刷';
Button3.Caption := '紋理畫刷';
Button4.Caption := '線性漸變畫刷';
Button5.Caption := '路徑漸變畫刷';
end;
end.
測試效果圖: