建立 GDI+ 的區域有五種辦法:
1、根據一個矩形建立(矩形區域);
2、根據路徑建立;
3、根據 GDI 區域的句柄建立;
4、根據從區域中獲取的數據建立;
5、無參數建立.
本例演示了前三種建立方法.
本例效果圖:
代碼文件:unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
RadioGroup1: TRadioGroup;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses GDIPOBJ, GDIPAPI;
procedure TForm1.FormCreate(Sender: TObject);
begin
RadioGroup1.Items.CommaText := '矩形通道,橢圓形通道,多邊形通道';
RadioGroup1.ItemIndex := 0;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
rt: TGPRect;
pts: array[0..2] of TPoint;
RgnHandle: HRGN;
g: TGPGraphics;
path: TGPGraphicsPath;
b: TGPBrush;
rgn: TGPRegion;
begin
rt := MakeRect(20, 20, 100, ClientHeight-40);
pts[0] := Point(rt.X + rt.Width p 2, rt.Y);
pts[1] := Point(rt.X, rt.Y + rt.Height);
pts[2] := Point(rt.X + rt.Width, rt.Y + rt.Height);
case RadioGroup1.ItemIndex of
0: rgn := TGPRegion.Create(rt); {根據矩形建立(矩形區域)}
1: begin {根據路徑建立}
path := TGPGraphicsPath.Create;
path.AddEllipse(rt);
rgn := TGPRegion.Create(path);
path.Free;
end;
2: begin {根據 GDI 區域的句柄建立}
RgnHandle := CreatePolygonRgn(pts, Length(pts), WINDING);
rgn := TGPRegion.Create(RgnHandle);
DeleteObject(RgnHandle);
end;
end;
g := TGPGraphics.Create(Canvas.Handle);
b := TGPHatchBrush.Create(HatchStyleDiagonalCross, aclSilver, aclGray);
g.FillRegion(b, rgn);
b.Free;
rgn.Free;
g.Free;
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
Repaint;
end;
end.
窗體文件:object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 137
ClientWidth = 238
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 RadioGroup1: TRadioGroup
Left = 137
Top = 8
Width = 91
Height = 121
Caption = 'RadioGroup1'
TabOrder = 0
OnClick = RadioGroup1Click
end
end