本文為GDI+ for VCL基礎系列文章之一,主要供GDI+初學者入門參考,例子使用GDI+版本和說明見《GDI+ for VCL基礎 -- GDI+ 與 VCL》。如有錯誤或者建議請來信:maozefa@hotmail.com
GDI+由二維向量圖形、映像和版面等三部分組成,其中的二維向量圖形的圖元,包括點、線條、曲線和圖形等的繪製工具就是畫筆和畫刷,而畫筆的特徵又是由畫刷決定的(在以後的關於畫筆的文章中介紹),因此,熟練地掌握GDI+的各種畫刷特性和使用方法是繪製GDI+圖形的前提條件。
GDI+提供了SolidBrush(實色刷)、HatchBrush(陰影刷)、TextureBrush(紋理刷)、LinearGradientBrush(漸層刷)和PathGradientBrush(路徑刷)等五種畫刷,在GDI+ for VCL中,各種畫刷在原C++類類名基礎上加了TGp首碼,均派生於TGpBrush,其中的TGpSolidBrush和TGpHatchBrush相當於VCL中傳統的GDI的畫刷TBrush。
VCL的TBrush通過對其Style設定,可以使用幾種填充圖案,而GDI+ for VCL則提供了單獨的陰影刷TGpHatchBrush。TGpHatchBrush不僅提供了多達53種的陰影樣式(填充圖案),而且可以設定圖案的前景色彩和背景色。下面的代碼片斷示範了GDI+建立各種陰影刷填充圖案的例子:
Delphi例子:
procedure TForm1.FormCreate(Sender: TObject);
var
backgroundImage: TGpImage;
begin
backgroundImage := TGpBitmap.Create('../../Media/marble.jpg');
try
FBackgroundBrush := TGpTextureBrush.Create(backgroundImage);
finally
backgroundImage.Free;
end;
FShadowBrush := TGpSolidBrush.Create(ARGB(80, kcBlack));
DoubleBuffered := True;
PaintProc := HatchPaint;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FBackgroundBrush.Free;
FShadowBrush.Free;
end;
procedure TForm1.HatchPaint(g: TGpGraphics);
var
I: THatchStyle;
x, y: Integer;
brush: TGpHatchBrush;
begin
y := -22;
for I := Low(THatchStyle) to High(THatchStyle) do
begin
if Integer(I) mod 8 = 0 then
begin
x := 10;
Inc(y, 38);
end;
if Integer(I) mod 2 = 0 then
brush := TGpHatchBrush.Create(I, kcWhite, kcBlue)
else
brush := TGpHatchBrush.Create(I, kcYellow, kcRed);
try
if Integer(I) mod 2 = 0 then
begin
g.FillRectangle(FShadowBrush, x + 3, y + 3, 35, 30);
g.FillRectangle(brush, x, y, 35, 30);
end
else
begin
g.FillEllipse(FShadowBrush, x + 3, y + 3, 35, 30);
g.FillEllipse(brush, x, y, 35, 30);
end;
finally
Brush.Free;
end;
Inc(x, 44);
end;
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
g: TGpGraphics;
begin
g := TGpGraphics.Create(PaintBox1.Canvas.Handle);
try
g.FillRectangle(FBackgroundBrush, GpRect(PaintBox1.ClientRect));
PaintProc(g);
finally
g.Free;
end;
end;procedure TForm1.SetPaintProc(const Value: TPaintProc);
begin
FProc := Value;
PaintBox1.Invalidate;
end;
其中PaintProc是個方法屬性。TGpHatchBrush有陰影樣式、前景色彩和背景色三個屬性,但是都是唯讀,所以要改變陰影樣式、前景色彩和背景色唯一的辦法就是重建立立。
C++ Builder例子:
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TGpImage *backgroundImage = new TGpBitmap("../../Media/marble.jpg");
try
{
FBackgroundBrush = new TGpTextureBrush(backgroundImage);
}
__finally
{
delete backgroundImage;
}
FShadowBrush = new TGpSolidBrush(TGpColor(80, kcBlack));
DoubleBuffered = true;
PaintProc = &HatchPaint;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete FBackgroundBrush;
delete FShadowBrush;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SetPaintProc(const TPaintProc Value)
{
FProc = Value;
PaintBox1->Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HatchPaint(TGpGraphics *g)
{
TGpHatchBrush *brush;
int x = 0, y = -22;
for (int I = hsHorizontal; I <= hsSolidDiamond; I ++)
{
if (I % 8 == 0)
{
x = 10;
y += 38;
}
if (I % 2 == 0)
brush = new TGpHatchBrush((THatchStyle)I, kcWhite, kcBlue);
else
brush = new TGpHatchBrush((THatchStyle)I, kcYellow, kcRed);
try
{
if (I % 2 == 0)
{
g->FillRectangle(FShadowBrush, x + 3, y + 3, 35, 30);
g->FillRectangle(brush, x, y, 35, 30);
}
else
{
g->FillEllipse(FShadowBrush, x + 3, y + 3, 35, 30);
g->FillEllipse(brush, x, y, 35, 30);
}
}
__finally
{
delete brush;
}
x += 44;
}
}//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
TGpGraphics *g = new TGpGraphics(PaintBox1->Canvas->Handle);
try
{
g->FillRectangle(FBackgroundBrush, TGpRect(PaintBox1->ClientRect));
PaintProc(g);
}
__finally
{
delete g;
}
}
//---------------------------------------------------------------------------
運行:
很遺憾,TGpHatchBrush沒有提供自訂圖案樣式功能,否則會增色不少。由於可以設定陰影圖案的前景色彩和背景色,如果把前景色彩或者背景色設定為透明色或者半透明色,再配上適當的底色圖片,可以產生意想不到的美術效果。