📄 excludecliprectu.pas
字号:
unit ExcludeClipRectU;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TForm1 = class(TForm)
Image2: TImage;
Timer1: TTimer;
procedure FormPaint(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
{the record structure defining a moving dot}
TDot = record
Pos: TPoint;
Vel: TPoint;
end;
var
Form1: TForm1;
Dots: array[0..9] of TDot; // the array of moving dots
Offscreen: TBitmap; // the offscreen double buffer
implementation
{$R *.DFM}
procedure TForm1.FormPaint(Sender: TObject);
begin
{draw the foreground image. this will be drawn only once}
Canvas.Draw(Image2.Left, Image2.Top, Image2.Picture.Bitmap);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
iCount: Integer; // a general loop control variable
begin
{create and initialize the offscreen bitmap}
OffScreen := TBitmap.Create;
OffScreen.Width := Form1.ClientWidth;
OffScreen.Height := Form1.ClientHeight;
{create and initialize the array of moving dots}
for iCount := 0 to 9 do
begin
Dots[iCount].Pos.X := Random(ClientWidth);
Dots[iCount].Pos.Y := Random(ClientHeight);
if Random(2)=0 then Dots[iCount].Vel.X := -1 else Dots[iCount].Vel.X := 1;
if Random(2)=0 then Dots[iCount].Vel.Y := -1 else Dots[iCount].Vel.Y := 1;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
{the offscreen bitmap is no longer needed, so free it}
Offscreen.Free;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
iCount: Integer; // a general loop counter
begin
{erase the last frame of animation in the offscreen bitmap}
Offscreen.Canvas.Brush.Color := clBlack;
Offscreen.Canvas.FillRect(Offscreen.Canvas.ClipRect);
{loop through all 10 moving dots}
for iCount := 0 to 9 do
begin
{change the dot's position according to velocity}
Dots[iCount].Pos.X := Dots[iCount].Pos.X+Dots[iCount].Vel.X;
Dots[iCount].Pos.Y := Dots[iCount].Pos.Y+Dots[iCount].Vel.Y;
{reverse the dot's velocity if it has reached the edge of the screen}
if (Dots[iCount].Pos.X<0) or (Dots[iCount].Pos.X>ClientWidth) then
Dots[iCount].Vel.X := 0-Dots[iCount].Vel.X;
if (Dots[iCount].Pos.Y<0) or (Dots[iCount].Pos.Y>ClientHeight) then
Dots[iCount].Vel.Y := 0-Dots[iCount].Vel.Y;
{draw a red dot on the offscreen bitmap (2X2 pixels)}
Offscreen.Canvas.Pixels[Dots[iCount].Pos.X,Dots[iCount].Pos.Y] := clRed;
Offscreen.Canvas.Pixels[Dots[iCount].Pos.X+1,Dots[iCount].Pos.Y] := clRed;
Offscreen.Canvas.Pixels[Dots[iCount].Pos.X,Dots[iCount].Pos.Y+1] := clRed;
Offscreen.Canvas.Pixels[Dots[iCount].Pos.X+1,Dots[iCount].Pos.Y+1] := clRed;
end;
{the bitmap stored in Image1 has already been drawn to the form. this happens
only once, when the Paint event fires, which happens only when the form is
displayed the first time or after it has been uncovered by a top level
window. since we don't want to destroy this 'foreground' image, we exclude
its rectangular area from the clipping region. this will effectively cut a
hole in the clipping region, and any drawing attempted in this area will be
denied}
ExcludeClipRect(Canvas.Handle, Image2.Left, Image2.Top,
Image2.Left+Image2.Width, Image2.Top+Image2.Height);
{draw the offscreen bitmap to the screen. the 'hole' in the clipping region
prevents the bitmap from being drawn over the foreground bitmap}
Canvas.Draw(0, 0, Offscreen);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -