// 区域建立: TGPRegion.Create(); { 参数可以是矩形、路径、HRGN、区域数据, 或无参} TGPRegion.FromHRGN(); { 从 HRGN 初始化 } // 区域运算(矩形、路径、或另一个区域): IGPRegion.Intersect(); { 交集 } IGPRegion.Union(); { 并集 } IGPRegion.Exclude(); { 减去 } IGPRegion.ExclusiveOr(); { 减去交集 } IGPRegion.Complement(); { 不相交 } // 其他: IGPRegion.IsVisible(); { 判断指定的点或矩形是否在区域内 } IGPRegion.GetData; { 获取区域数据(IGPBuffer), 其中包含数据指针和数据大小 } IGPRegion.GetRegionScans(); { 从区域中获取矩形数组, 浮点型 } IGPRegion.GetRegionScansI(); { 从区域中获取矩形数组, 整型 } IGPRegion.Translate(); { 偏移 } IGPRegion.Transform(); { Matrix 变换 } IGPRegion.MakeInfinite; { 初始化为无限大 } IGPRegion.MakeEmpty; { 初始化为空 } IGPRegion.IsEmpty(); { 判断区域是否为空 } IGPRegion.IsInfinite(); { 判断区域是否是无限大 } IGPRegion.GetBounds(); { 获取边界矩形 } IGPRegion.GetHRGN(); { 获取 HRGN(Windows 区域的句柄) } IGPRegion.Clone; { 复制 } IGPRegion.Equals(); { 判断是否相同 }
区域的两个重要用途:
1、IGPRegion.IsVisible();
2、IGPGraphics.Clip 或 IGPGraphics.SetClip();
IsVisible 测试:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormPaint(Sender: TObject); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); end; var Form1: TForm1; implementation {$R *.dfm} uses GdiPlus, GdiPlusHelpers; var Region: IGPRegion; procedure TForm1.FormCreate(Sender: TObject); var Rect: TGPRect; Path: IGPGraphicsPath; begin Rect.Initialize(ClientRect); Rect.Inflate(-20, -20); Region := TGPRegion.Create(Rect); Rect.Inflate(-2, -2); Path := TGPGraphicsPath.Create(); Path.AddEllipse(Rect); Region.Exclude(Path); end; procedure TForm1.FormPaint(Sender: TObject); var Brush: IGPHatchBrush; begin Brush := TGPHatchBrush.Create(HatchStyleMin, TGPColor.Silver, TGPColor.Red); Canvas.ToGPGraphics.FillRegion(Brush, Region); end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin Text := 'Form1'; if Region.IsVisible(X, Y) then Text := '在区域中'; end; end.
剪切区域测试:
uses GdiPlus; procedure TForm1.FormPaint(Sender: TObject); const Pts: array [0..3] of TGPPoint = ( (X: 10; Y: 10), (X: 150; Y: 10), (X: 100; Y: 75), (X: 100; Y: 150)); var Graphics: IGPGraphics; Path: IGPGraphicsPath; Region: IGPRegion; Pen: IGPPen; Font: IGPFont; Brush: IGPBrush; begin Graphics := TGPGraphics.Create(Handle); Path := TGPGraphicsPath.Create; Path.AddPolygon(Pts); Region := TGPRegion.Create(Path); Pen := TGPPen.Create($FF000000); Graphics.DrawPath(Pen, Path); Graphics.Clip := Region; // Graphics.SetClip(Region); //或者这样 Font := TGPFont.Create('Arial', 36, [FontStyleBold], UnitPixel); Brush := TGPSolidBrush.Create($FFFF0000); Graphics.DrawString('A Clipping Region', Font, TGPPointF.Create(15, 25), Brush); Graphics.DrawString('A Clipping Region', Font, TGPPointF.Create(15, 68), Brush); end;