建立 GDI+ 的区域有五种办法:

1、根据一个矩形建立(矩形区域);
2、根据路径建立;
3、根据 GDI 区域的句柄建立;
4、根据从区域中获取的数据建立;
5、无参数建立.

本例演示了前三种建立方法.

本例效果图:



代码文件:
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 FormPaint(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses GDIPOBJ, GDIPAPI;

procedure TForm1.FormCreate(Sender: TObject);
begin
  RadioGroup1.Items.CommaText := '矩形通道,椭圆形通道,多边形通道';
  RadioGroup1.ItemIndex := 0;
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  rt: TGPRect;
  pts: array[0..2] of TPoint;
  RgnHandle: HRGN;
  g: TGPGraphics;
  path: TGPGraphicsPath;
  b: TGPBrush;
  rgn: TGPRegion;
begin
  rt := MakeRect(20, 20, 100, ClientHeight-40);
  pts[0] := Point(rt.X + rt.Width div 2, rt.Y);
  pts[1] := Point(rt.X, rt.Y + rt.Height);
  pts[2] := Point(rt.X + rt.Width, rt.Y + rt.Height);

  case RadioGroup1.ItemIndex of
    0: rgn := TGPRegion.Create(rt);       {根据矩形建立(矩形区域)}
    1: begin                              {根据路径建立}
         path := TGPGraphicsPath.Create;
         path.AddEllipse(rt);
         rgn := TGPRegion.Create(path);
         path.Free;
       end;
    2: begin                              {根据 GDI 区域的句柄建立}
         RgnHandle := CreatePolygonRgn(pts, Length(pts), WINDING);
         rgn := TGPRegion.Create(RgnHandle);
         DeleteObject(RgnHandle);
       end;
  end;

  g := TGPGraphics.Create(Canvas.Handle);
  b := TGPHatchBrush.Create(HatchStyleDiagonalCross, aclSilver, aclGray);
  g.FillRegion(b, rgn);

  b.Free;
  rgn.Free;
  g.Free;
end;

procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
  Repaint;
end;

end.

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 137
  ClientWidth = 238
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnCreate = FormCreate
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
  object RadioGroup1: TRadioGroup
    Left = 137
    Top = 8
    Width = 91
    Height = 121
    Caption = 'RadioGroup1'
    TabOrder = 0
    OnClick = RadioGroup1Click
  end
end

posted on 2008-06-29 21:48  万一  阅读(2741)  评论(0编辑  收藏  举报