CombineRgn(
p1: HRGN; {合成後的區域}
p2, p3: HRGN; {兩個原始區域}
p4: Integer {合並選項; 見下表}
): Integer; {有四種可能的返回值}
//合並選項:
RGN_AND = 1;
RGN_OR = 2;
RGN_XOR = 3;
RGN_DIFF = 4;
RGN_COPY = 5; {復制第一個區域}
//返回值:
ERROR = 0; {錯誤}
NULLREGION = 1; {空區域}
SIMPLEREGION = 2; {單矩形區域}
COMPLEXREGION = 3; {多矩形區域}
本例效果圖:
代碼文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
RadioGroup1: TRadioGroup;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ForMactivate(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
Rgn1,Rgn2,Rgn: HRGN;
//初始化
procedure TForm1.FormCreate(Sender: TObject);
begin
{建立兩個原始區域}
Rgn1 := CreateEllipticRgn(20, 20, 120, 160);
Rgn2 := CreateEllipticRgn(70, 20, 170, 160);
{Rgn 是用來接受運算結果的, 但函數 CombineRgn 要求它必須是已存在的區域, 這裡虛擬一個}
Rgn := CreateRectRgnIndirect(BoundsRect);
RadioGroup1.Items.CommaText := '兩個原始區域,RGN_AND,RGN_OR,RGN_XOR,RGN_DIFF,RGN_COPY';
RadioGroup1.ItemIndex := 0;
end;
//見到窗體時先繪制一次
procedure TForm1.ForMactivate(Sender: TObject);
begin
RadioGroup1Click(nil);
end;
//主要代碼
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
Repaint;
if RadioGroup1.ItemIndex = 0 then
begin
Canvas.Brush.Color := clSilver;
Canvas.Brush.Style := bsDiagCross;
FillRgn(Canvas.Handle, Rgn1, Canvas.Brush.Handle);
FillRgn(Canvas.Handle, Rgn2, Canvas.Brush.Handle);
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Color := clRed;
FrameRgn(Canvas.Handle, Rgn1, Canvas.Brush.Handle, 2, 2);
FrameRgn(Canvas.Handle, Rgn2, Canvas.Brush.Handle, 2, 2);
end else begin
CombineRgn(Rgn, Rgn1, Rgn2, RadioGroup1.ItemIndex);
Canvas.Brush.Color := clSilver;
Canvas.Brush.Style := bsDiagCross;
FillRgn(Canvas.Handle, Rgn, Canvas.Brush.Handle);
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Color := clRed;
FrameRgn(Canvas.Handle, Rgn, Canvas.Brush.Handle, 2, 2);
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DeleteObject(Rgn1);
DeleteObject(Rgn2);
DeleteObject(Rgn);
end;
end.
窗體文件:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 182
ClIEntWidth = 300
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnActivate = ForMactivate
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object RadioGroup1: TRadioGroup
Left = 189
Top = 8
Width = 106
Height = 166
Caption = 'RadioGroup1'
TabOrder = 0
OnClick = RadioGroup1Click
end
end