setbitmapbitsu.pas

来自「delphi win32api编程」· PAS 代码 · 共 90 行

PAS
90
字号
unit SetBitmapBitsU;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

{This example will run properly only with a 256 color video driver.}
var
   Started: Boolean = FALSE;             // controls the overall loop

procedure TForm1.Button1Click(Sender: TObject);
var
   BitmapBits: array[0..9999] of byte;   // holds the new bitmap bit information
   BitmapImage: TBitmap;                 // the bitmap image
   Loop: Integer;                        // a general loop counter
begin
   {toggle the loop control variable}
   Started:=not Started;

   {change the button caption to reflect the new state}
   if Started then
     Button1.Caption := 'Stop'
   else
     Button1.Caption := 'Start';

   {create a 100X100 pixel bitmap}
   BitmapImage := TBitmap.Create;
   BitmapImage.Height := 100;
   BitmapImage.Width := 100;

   {force this to be a device dependent bitmap}
   BitmapImage.HandleType := bmDDB;

   {this loop continues until the button is pressed again}
   while Started do
   begin
     {fill the bitmap bit information with white}
     FillChar(BitmapBits, SizeOf(BitmapBits), 255);

     {set 10000 random pixels to black}
     for Loop := 0 to 1000 do
     begin
        BitmapBits[Random(100)*100+Random(100)]:=0;
        BitmapBits[Random(100)*100+Random(100)]:=0;
        BitmapBits[Random(100)*100+Random(100)]:=0;
        BitmapBits[Random(100)*100+Random(100)]:=0;
        BitmapBits[Random(100)*100+Random(100)]:=0;
        BitmapBits[Random(100)*100+Random(100)]:=0;
        BitmapBits[Random(100)*100+Random(100)]:=0;
        BitmapBits[Random(100)*100+Random(100)]:=0;
        BitmapBits[Random(100)*100+Random(100)]:=0;
        BitmapBits[Random(100)*100+Random(100)]:=0;
     end;

     {blast the new bits into the bitmap}
     SetBitmapBits(BitmapImage.Handle, 10000, @BitmapBits);

     {copy the bitmap to the canvas of the form}
     BitBlt(Form1.Canvas.Handle, 84, 8, 100, 100, BitmapImage.Canvas.Handle, 0,
            0, SRCCOPY);

     {this is required for proper Windows operation}
     Application.ProcessMessages;
   end;

   {free our bitmap}
   BitmapImage.Free
end;

end.

⌨️ 快捷键说明

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