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