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

📄 combinergnu.pas

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

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 FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  BinocularRgn: HRGN;   // a handle to the combined region

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  Circle1, Circle2: HRGN;  // holds two circular regions
begin
  {the handle to the combined region must identify a pre-existing region, so
   create a bogus region}
  BinocularRgn := CreateEllipticRgnIndirect(BoundsRect);

  {create two circular regions, the first taking up 3/4 of the left side
   of the area covered by Image1, and the second taking up 3/4 of the right
   side of the area covered by Image1}
  Circle1 := CreateEllipticRgn(Image1.Left, Image1.Top,
                               Image1.Left+MulDiv(Image1.Width,3,4),
                               Image1.Top+Image1.Height);
  Circle2 := CreateEllipticRgn(Image1.Left +(Image1.Width div 4),
                               Image1.Top, Image1.Left+Image1.Width,
                               Image1.Top+Image1.Height);

  {combine the two regions, creating a region reminiscent of a view through
   a pair of binoculars}
  CombineRgn(BinocularRgn, Circle1, Circle2, RGN_OR);

  {delete the two circular regions as they are no longer needed}
  DeleteObject(Circle1);
  DeleteObject(Circle2);
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  ClipRect: TRect;    // holds the current clipping region coordinates
begin
  {select the combined region into the device context as a clipping region}
  SelectClipRgn(Canvas.Handle, BinocularRgn);

  {draw the contents of the image (which is invisible) onto the surface of the
   form.  it will be clipped to the current clipping region, resulting in what
   looks like the view of a ship through a pair of binoculars}
  Canvas.Draw(Image1.Left, Image1.Top, Image1.Picture.Bitmap);

  {draw the perimeter of the region in red to make it stand out}
  Canvas.Brush.Color := clRed;
  FrameRgn(Canvas.Handle, BinocularRgn, Canvas.Brush.Handle, 2, 2);

  {retrieve the smallest rectangle that will fit around the currently visible
   portion of the device context}
  GetClipBox(Canvas.Handle, ClipRect);

  {delete the clipping region so that drawing can be performed on the entire
   device context surface}
  SelectClipRgn(Canvas.Handle, 0);

  {draw the extents of the previously selected clipping region}
  Canvas.Brush.Style := bsClear;
  Canvas.Pen.Color := clBlack;
  Rectangle(Canvas.Handle, ClipRect.Left, ClipRect.Top, ClipRect.Right,
            ClipRect.Bottom);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  {delete the combined region}
  DeleteObject(BinocularRgn);
end;

end.

⌨️ 快捷键说明

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