本來這個例子是要獲取區域的詳細信息的, 但非常遺憾, 這個版本的 GDI+ 的頭文件少定義了一個 TRegionData 類!
不過也不要緊, 通過 TRegionData 主要也是獲取區域中的矩形數據, GetRegionScans 函數可以完成這個任務!
本例效果圖:
代碼文件:unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
procedure FormPaint(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses GDIPOBJ, GDIPAPI;
procedure TForm1.FormPaint(Sender: TObject);
var
g: TGPGraphics;
p: TGPPen;
b: TGPSolidBrush;
path: TGPGraphicsPath;
rgn1,rgn2: TGPRegion;
RgnData: PByte;
rt: TGPRect;
begin
g := TGPGraphics.Create(Canvas.Handle);
p := TGPPen.Create(aclRed);
b := TGPSolidBrush.Create(MakeColor(50, 0, 0, 255));
{建立並填充第一個區域}
path := TGPGraphicsPath.Create;
path.AddEllipse(MakeRect(20, 20, ClientWidth-40, ClientHeight-40));
rgn1 := TGPRegion.Create(path);
g.FillRegion(b, rgn1);
{根據第一個區域的數據創建第二個區域}
GetMem(RgnData, rgn1.GetDataSize); {分配空間}
rgn1.GetData(RgnData, rgn1.GetDataSize); {獲取數據}
rgn2 := TGPRegion.Create(RgnData, rgn1.GetDataSize); {建立第二個區域}
rgn2.GetBounds(rt, g); {獲取第二個區域的矩形范圍}
g.DrawRectangle(p, rt); {畫這個矩形}
FreeMem(RgnData); {釋放內存}
{如果不是為了測試 GetData,下面三行即可代替上面六行}
// rgn2 := rgn1.Clone;
// rgn2.GetBounds(rt, g);
// g.DrawRectangle(p, rt);
rgn1.Free;
rgn2.Free;
path.Free;
b.Free;
p.Free;
g.Free;
end;
end.
窗體文件:object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 171
ClientWidth = 243
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnPaint = FormPaint
PixelsPerInch = 96
TextHeight = 13
end