GetRegionData(
RGN: HRGN; {區域句柄}
p2: DWord; {區域數據緩沖區大小}
p3: PRgnData {區域數據結構 TRgnData 的指針, 見下表}
): DWord; {成功返回 1}
//TRgnData 是 _RGNDATA 結構的重命名:
_RGNDATA = record
rdh: TRgnDataHeader; {數據頭; 又是個結構, 見下表}
Buffer: array[0..0] of CHAR; {}
Reserved: array[0..2] of CHAR; {}
end;
//TRgnDataHeader 是 _RGNDATAHEADER 結構的重命名:
_RGNDATAHEADER = packed record
dwSize: DWord; {}
iType: DWord; {}
nCount: DWord; {}
nRgnSize: DWord; {}
rcBound: TRect; {}
end;
每個區域都是有若干矩形構成的, 下面的例子枚舉了一個區域中的所有矩形. 運行效果圖:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Rgn: HRGN; {區域句柄}
RgnDataSize: Integer; {區域數據緩沖區的大小}
RgnData: PRgnData; {區域數據指針}
BrushHandle: HBRUSH; {筆刷句柄}
Count: Integer; {區域中的矩形總數}
Rect: PRect; {矩形指針}
i: Integer;
begin
{建立一個圓角矩形區域}
Rgn := CreateRoundRectRgn(10,10,180,190,120,130);
{建立筆刷, 並填充區域}
BrushHandle := CreateSolidBrush(clSkyBlue);
FillRgn(Canvas.Handle, Rgn, BrushHandle);
{獲取區域數據緩沖區的大小, 並申請需要的內存}
RgnDataSize := GetRegionData(Rgn, 0, nil);
GetMem(RgnData, RgnDataSize);
{再次使用 GetRegionData, 獲取區域數據}
GetRegionData(Rgn, RgnDataSize, RgnData);
{獲取矩形總數}
Count := RgnData^.rdh.nCount;
Text := '區域中的矩形總數: ' + IntToStr(Count);
{獲取第一個矩形的指針}
Rect := @RgnData^.Buffer;
{枚舉區域中的矩形, 並使用隨機顏色畫出來}
Randomize;
for i := 0 to Count - 1 do
begin
Canvas.Brush.Color := Random($FFFFFF);
Canvas.FillRect(Rect^);
Inc(Rect); {指向下一個矩形}
end;
{釋放資源}
DeleteObject(BrushHandle);
DeleteObject(Rgn);
FreeMem(RgnData);
end;
end.