usetpixel.pas

来自「Delphi Win32核心API参考光盘源码 本书包含了常用的Windows」· PAS 代码 · 共 71 行

PAS
71
字号
unit USetPixel;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  {erase the current image}
  Canvas.Brush.Color := Color;
  Canvas.FillRect(ClientRect);

  {begin the effect}
  Timer1.Enabled := TRUE;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  X, Y: Integer;       // tracks pixel coordinates
  iCount: Integer;     // a general loop counter
begin
  {begin the cheap fade effect}
  for iCount := 0 to 20000 do
  begin
    {retrieve a random coordinate}
    X := Random(Image1.Width-1);
    Y := Random(Image1.Height-1);

    {in a 4x4 pixel square at this coordinate, retrieve the pixels in the
     source image, and set them in the form's canvas}
    SetPixel(Canvas.Handle, X+Image1.Left, Y+Image1.Top,
             GetPixel(Image1.Picture.Bitmap.Canvas.Handle, X, Y));
    SetPixel(Canvas.Handle, X+1+Image1.Left, Y+Image1.Top,
             GetPixel(Image1.Picture.Bitmap.Canvas.Handle, X+1, Y));
    SetPixel(Canvas.Handle, X+Image1.Left, Y+1+Image1.Top,
             GetPixel(Image1.Picture.Bitmap.Canvas.Handle, X, Y+1));
    SetPixel(Canvas.Handle, X+1+Image1.Left, Y+1+Image1.Top,
             GetPixel(Image1.Picture.Bitmap.Canvas.Handle, X+1, Y+1));
  end;

  {draw the finished image so that there are no holes left}
  Canvas.Draw(Image1.Left, Image1.Top, Image1.Picture.Bitmap);

  {disable the timer}
  Timer1.Enabled := FALSE;
end;

end.

⌨️ 快捷键说明

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