⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createpolygonrgnu.pas

📁 DelphiWin32核心API参考光盘内容.是学习书籍中的源码,便于学习.
💻 PAS
字号:
unit CreatePolygonRgnU;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  PolygonRgn, ScaledRgn: HRGN;  // holds the original and scaled regions

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  Vertices: array[0..9] of TPoint;  // holds the vertices of the polygon region
  RegionData: Pointer;              // a pointer to region data
  RgnDataSize: DWORD;               // the size of the region data
  Transform: TXForm;                // the scaling transformation matrix
begin
  {specify a polygon in the shape of a star}
  Vertices[0] := Point(120, 5);
  Vertices[1] := Point(140, 70);
  Vertices[2] := Point(210, 70);
  Vertices[3] := Point(150, 100);
  Vertices[4] := Point(180, 175);
  Vertices[5] := Point(120, 120);
  Vertices[6] := Point(60, 175);
  Vertices[7] := Point(90, 100);
  Vertices[8] := Point(30, 70);
  Vertices[9] := Point(100, 70);

  {create a star shaped polygonal region}
  PolygonRgn := CreatePolygonRgn(Vertices, 10, WINDING);

  {retrieve the size of the region's data}
  RgnDataSize := GetRegionData(PolygonRgn, 0, NIL);

  {allocate enough memory to hold the region data}
  GetMem(RegionData, RgnDataSize);

  {retrieve the region data for the star shaped region}
  GetRegionData(PolygonRgn, RgnDataSize, RegionData);

  {initialize a transformation matrix to indicate a slight increase in size
   and a translation in position}
  with Transform do
  begin
    eM11 := 1.35;
    eM12 := 0;
    eM21 := 0;
    eM22 := 1.35;
    eDx  := -42;
    eDy  := -35;
  end;

  {create a new, scaled region based on the original star shaped region}
  ScaledRgn := ExtCreateRegion(@Transform, RgnDataSize, TRgnData(RegionData^));

  {free the region data as it is no longer needed}
  FreeMem(RegionData, RgnDataSize);
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  TempRgn: HRGN;    // holds a retrieved region handle
begin
  {select the scaled star shaped region as a clipping region}
  SelectClipRgn(Canvas.Handle, ScaledRgn);

  {draw the cityscape image onto the form. it will be clipped to the boundaries
   of the star shaped region}
  Canvas.Draw(0, 0, Image1.Picture.Bitmap);

  {even though we explicitly know what the clipping region is, we can retrieve
   it from the device context, using the retrieved region in any region
   functions. the GetClipRgn function requires the specified region handle
   to identify an existing region, so set it to the original star shaped
   region.  this will retrieve the current clipping region, which is the
   scaled region}
  TempRgn := PolygonRgn;
  GetClipRgn(Canvas.Handle, TempRgn);

  {draw the edges of the region to make it stand out}
  Canvas.Brush.Color := clRed;
  FrameRgn(Canvas.Handle, TempRgn, Canvas.Brush.Handle, 2, 2);
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  {select the scaled star shaped region as a clipping region}
  SelectClipRgn(Canvas.Handle, ScaledRgn);

  {indicate if the clicked area of the canvas is visible within
   the current clipping region (the scaled star shaped region)}
  if PtVisible(Canvas.Handle, X, Y) then
    Caption := 'CreatePolygonRgn Example - Visible'
  else
    Caption := 'CreatePolygonRgn Example - Invisible';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  {free all resources associated with both regions}
  DeleteObject(PolygonRgn);
  DeleteObject(ScaledRgn);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -