不过也不要紧, 通过 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