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

📄 getdibitsu.pas

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

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  TheBitmap: HBITMAP;                  // a handle to a regular bitmap
  RegularBitmapInfo: Windows.TBitmap;  // a Windows bitmap information structure

  BitmapInfo: PBitmapInfo;             // a pointer to a DIB info structure
  BitmapBits: Pointer;                 // a pointer to DIB bit values
begin
  {get a handle to a system bitmap}
  TheBitmap:=LoadBitmap(0, MakeIntResource(OBM_CHECKBOXES));

  {fill in a Windows TBITMAP information structure}
  GetObject(TheBitmap, SizeOf(Windows.TBitmap), @RegularBitmapInfo);

  {get the memory for the DIB bitmap header}
  GetMem(BitmapInfo, SizeOf(TBitmapInfo)+256*SizeOf(TRGBQuad));

  {initialize the bitmap information}
  BitmapInfo^.bmiHeader.biWidth         := RegularBitmapInfo.bmWidth;
  BitmapInfo^.bmiHeader.biHeight        := RegularBitmapInfo.bmHeight;
  BitmapInfo^.bmiHeader.biPlanes        := 1;
  BitmapInfo^.bmiHeader.biBitCount      := 8;      // 256 colors
  BitmapInfo^.bmiHeader.biCompression   := BI_RGB; // no compression
  BitmapInfo^.bmiHeader.biSizeImage     := 0;      // let Windows determine size
  BitmapInfo^.bmiHeader.biXPelsPerMeter := 0;
  BitmapInfo^.bmiHeader.biYPelsPerMeter := 0;
  BitmapInfo^.bmiHeader.biClrUsed       := 0;
  BitmapInfo^.bmiHeader.biClrImportant  := 0;
  BitmapInfo^.bmiHeader.biSize          := SizeOf(TBitmapInfoHeader);

  {allocate enough memory to hold the bitmap bit values}
  GetMem(BitmapBits,RegularBitmapInfo.bmWidth*RegularBitmapInfo.bmHeight);

  {retrieve the bit values from the regular bitmap in a DIB format}
  GetDIBits(Form1.Canvas.Handle, TheBitmap, 0, RegularBitmapInfo.bmHeight,
            BitmapBits, BitmapInfo^, 0);

  {display this new DIB bitmap}
  SetDIBitsToDevice(Form1.Canvas.Handle, (Form1.Width div 2)-
                   (BitmapInfo^.bmiHeader.biWidth div 2), 25,
                    BitmapInfo^.bmiHeader.biWidth,
                    BitmapInfo^.bmiHeader.biHeight, 0, 0, 0,
                    BitmapInfo^.bmiHeader.biHeight, BitmapBits, BitmapInfo^,
                    DIB_RGB_COLORS);

  {delete the regular bitmap}
  DeleteObject(TheBitmap);

  {cleanup allocated memory}
  FreeMem(BitmapInfo, SizeOf(TBitmapInfo)+256*SizeOf(TRGBQuad));
  FreeMem(BitmapBits,RegularBitmapInfo.bmWidth*RegularBitmapInfo.bmHeight);
end;

end.

⌨️ 快捷键说明

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