// 區域建立:
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;