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

📄 createbitmapu.pas

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

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

{Note: This example will only work correctly under a 256 color video driver}

procedure TForm1.Button1Click(Sender: TObject);
var
   TheBitmap: HBitmap;                // a handle for the new bitmap
   TheBits: array[0..4095] of Byte;   // an array of original bitmap bits
   GotBits: array[0..4095] of Byte;   // an array to retrieve the bitmap bits
   LoopX,                             // general loop counter variables
   LoopY: Integer;
   OffScreen: HDC;                    // an offscreen device context
   TheSize: TSize;                    // holds the bitmap size dimensions
begin
   {set every bit in the new bitmap to the color stored
    in the system palette slot 3}
   FillMemory(@TheBits, 4096, 3);

   {set a 10 X 10 pixel square in the middle of the
    image to the color in system palette slot 1}
   for LoopX:=27 to 37 do
   begin
      TheBits[LoopX*64+27]:=1;
      TheBits[LoopX*64+28]:=1;
      TheBits[LoopX*64+29]:=1;
      TheBits[LoopX*64+30]:=1;
      TheBits[LoopX*64+31]:=1;
      TheBits[LoopX*64+32]:=1;
      TheBits[LoopX*64+33]:=1;
      TheBits[LoopX*64+34]:=1;
      TheBits[LoopX*64+35]:=1;
      TheBits[LoopX*64+36]:=1;
   end;

   {create a 64 X 64 pixel bitmap, using the information
    in the array TheBits}
   TheBitmap:=CreateBitmap(64,64,1,8,@thebits);

   {set the prefered bitmap dimensions. this is not used
    by Windows, it simply sets some user defined information}
   SetBitmapDimensionEx(TheBitmap,100,100,nil);

   {create an offscreen device context that is
    compatible with the screen}
   OffScreen:=CreateCompatibleDC(0);

   {select the new bitmap into the offscreen device context}
   SelectObject(OffScreen, TheBitmap);

   {copy the bitmap from the offscreen device context
    onto the canvas of the form. this will display the bitmap}
   BitBlt(Form1.Canvas.Handle,162,16,64,64,OffScreen,0,0,SRCCOPY);

   {retrieve the bits that make up the bitmap image}
   GetBitmapBits(TheBitmap, 4096,@GotBits);

   {display the bits in the string grid}
   for LoopX:=0 to 63 do
     for LoopY:=0 to 63 do
       StringGrid1.Cells[LoopX,LoopY]:=IntToStr(GotBits[LoopX*64+LoopY]);

   {retrieve the user defined, prefered bitmap dimensions}
   GetBitmapDimensionEx(TheBitmap,TheSize);

   {Display these dimensions}
   Label1.Caption:='Preferred bitmap dimensions - Width: '+IntToStr(TheSize.CX)+
                   ' Height: '+IntToStr(TheSize.CY);

   {delete the offscreen device context}
   DeleteDC(OffScreen);

   {delete the new bitmap}
   DeleteObject(TheBitmap);
end;

end.

⌨️ 快捷键说明

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