📄 offsetcliprgnu.pas
字号:
unit OffsetClipRgnU;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MovingRgn: HRGN; // holds a region
XPos, YPos, XVel, YVel: Integer; // holds the region's velocity and position
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
{create a small circular region to be used as the clipping region}
MovingRgn := CreateEllipticRgn(0, 0, 75, 75);
{initialize the region's position and velocity}
XPos := 1;
YPos := 1;
XVel := 1;
YVel := 1;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
TempBitmap: TBitmap; // holds an offscreen bitmap
begin
{create the offscreen bitmap and initialize its size to that of the
invisible TImage. this offscreen bitmap is used to eliminate flicker}
TempBitmap := TBitmap.Create;
TempBitmap.Width := Image1.Width;
TempBitmap.Height := Image1.Height;
{increase the region's position by its velocity}
Inc(XPos, XVel);
Inc(YPos, YVel);
{if the region has reached the edge of the screen, reverse its velocity}
if (XPos<0) or (XPos>ClientRect.Right-75) then
XVel := 0-XVel;
if (YPos<0) or (YPos>ClientRect.Bottom-75) then
YVel := 0-YVel;
{select the circular region into the device context of the offscreen bitmap,
indicating that it should be logically ANDed with the bitmap's current
clipping region}
ExtSelectClipRgn(TempBitmap.Canvas.Handle, MovingRgn, RGN_AND);
{move the clipping region to the position being tracked}
OffsetClipRgn(TempBitmap.Canvas.Handle, XPos, YPos);
{draw the picture stored in Image1 into the bitmap. the clipping region will
only allow the bitmap to be drawn within the small circular area of the
region}
TempBitmap.Canvas.Draw(0, 0, Image1.Picture.Bitmap);
{draw the offscreen bitmap to the form. this will result in an animation of
a small, bouncing circle}
Canvas.Draw(Image1.Left, Image1.Top, TempBitmap);
{free the offscreen bitmap}
TempBitmap.Free;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
{we no longer need the region, so delete it}
DeleteObject(MovingRgn);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -