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

📄 dibandgdiu.pas

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

interface

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

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
  Dib: HBITMAP;           // holds a handle to the DIB
  DibInfo: PBitmapInfo;   // a pointer to the bitmap information data structure
  BitsPtr: PByte;         // holds a pointer to the bitmap bits
  ReferenceDC: HDC;       // a handle to the reference device context
  iCount: Integer;        // general loop counter
  OldBitmap: HBITMAP;     // holds a handle to the old DC bitmap
  ScratchCanvas: TCanvas; // holds a temporary canvas for drawing

  APolygon: array[0..2] of TPoint;                // holds a polygon

  SystemPalette: array[0..255] of TPaletteEntry;  // required for converting the
                                                  // system palette into a DIB
                                                  // compatible palette
begin
  {get the memory needed for the bitmap information data structure}
  GetMem(DibInfo, SizeOf(TBitmapInfo)+256*SizeOf(TRGBQuad));

  {initialize the bitmap information}
  DibInfo^.bmiHeader.biWidth         := 64;     // create a 64 X 64 pixel DIB,
  DibInfo^.bmiHeader.biHeight        := -64;    // oriented top-down
  DibInfo^.bmiHeader.biPlanes        := 1;
  DibInfo^.bmiHeader.biBitCount      := 8;      // 256 colors
  DibInfo^.bmiHeader.biCompression   := BI_RGB; // no compression
  DibInfo^.bmiHeader.biSizeImage     := 0;      // let Windows determine size
  DibInfo^.bmiHeader.biXPelsPerMeter := 0;
  DibInfo^.bmiHeader.biYPelsPerMeter := 0;
  DibInfo^.bmiHeader.biClrUsed       := 0;
  DibInfo^.bmiHeader.biClrImportant  := 0;
  DibInfo^.bmiHeader.biSize          := SizeOf(TBitmapInfoHeader);

  {retrieve the current system palette}
  GetSystemPaletteEntries(Form1.Canvas.Handle, 0, 256, SystemPalette);

  {the system palette is returned as an array of TPaletteEntry structures,
   which store the palette colors in the form of Red, Green, and Blue.  however,
   the TBitmapInfo structure's bmiColors member takes an array of TRGBQuad
   structures, which store the palette colors in the form of Blue, Green, and
   Red.  therefore, we must translate the TPaletteEntry structures into the
   appropriate TRGBQuad structures to get the correct color entries.}
  for iCount := 0 to 255 do
  begin
    DibInfo^.bmiColors[iCount].rgbBlue     := SystemPalette[iCount].peBlue;
    DibInfo^.bmiColors[iCount].rgbRed      := SystemPalette[iCount].peRed;
    DibInfo^.bmiColors[iCount].rgbGreen    := SystemPalette[iCount].peGreen;
    DibInfo^.bmiColors[iCount].rgbReserved := 0;
  end;

  {create a memory based device context}
  ReferenceDC := CreateCompatibleDC(0);

  {create the dib based on the memory device context and the
   initialized bitmap information}
  Dib := CreateDIBSection(ReferenceDC, DibInfo^, DIB_RGB_COLORS,
                          Pointer(BitsPtr), 0, 0);

  {select the Dib into the device context}
  OldBitmap := SelectObject(ReferenceDC, Dib);

  {create a canvas and set its handle to the created device context}
  ScratchCanvas := TCanvas.Create;
  ScratchCanvas.Handle := ReferenceDC;

  {fill the canvas with red}
  ScratchCanvas.Brush.Color := clRed;
  ScratchCanvas.FillRect(ScratchCanvas.ClipRect);

  {draw a green circle}
  ScratchCanvas.Brush.Color := clLime;
  ScratchCanvas.Ellipse(0, 0, 32, 32);

  {draw a triangle}
  ScratchCanvas.Brush.Color := clBlue;
  APolygon[0] := Point(63, 63);
  APolygon[1] := Point(32, 63);
  APolygon[2] := Point(48, 32);
  ScratchCanvas.Polygon(APolygon);

  {the above functions have drawn directly into the Dib. now we can draw the
   Dib onto the form surface using Dib functions}
  SetDIBitsToDevice(Form1.Canvas.Handle, 30, 5, 64, 64, 0, 0, 0, 64, BitsPtr,
                    DibInfo^, DIB_RGB_COLORS);

  {draw the DIB again, but this time let's stretch it to twice its size}
  StretchDIBits(Form1.Canvas.Handle, 105, 5, 128, 128, 0, 0, 64, 64, BitsPtr,
                DibInfo^, DIB_RGB_COLORS, SRCCOPY);

  {we no longer need the DIB, so delete it, the canvas, and the
   allocated memory for the information data structure}
  SelectObject(ReferenceDC, OldBitmap);
  ScratchCanvas.Free;
  DeleteObject(Dib);
  DeleteDC(ReferenceDC);
  FreeMem(DibInfo, SizeOf(TBitmapInfo)+256*SizeOf(TRGBQuad));
end;


end.

⌨️ 快捷键说明

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