TAdvStringGrid
Example 9 : Using bitmaps in the header when printing

PrintPageWidth:integer; Width in pixels of the paper PrintColWidth[aCol]:integer; Width in pixels of column aCol PrintColOffset[aCol]:integer Start position in pixels of column aCol PrintColStart:integer; Position from left in pixels of the first column left border PrintColEnd:integer; Position from left in pixels of the last column right border
With these things in mind, the following code is used to print the bitmap and text in the header and draw a line above the printed columns as well:
procedure TForm1.AdvStringGrid1PrintPage(Sender: TObject; Canvas: TCanvas; pagenr, pagexsize, pageysize: Integer); var bmp: TBitmap; r: TRect; ratio: double; begin bmp := TBitmap.create; bmp.LoadFromFile('athena.bmp'); ratio := bmp.width/bmp.height; r.Left := AdvStringGrid1.printcoloffset[1]; r.top := -0; r.right := r.left+round(advstringgrid1.printsettings.headersize*ratio); r.bottom := r.top-advstringgrid1.printsettings.headersize; Canvas.StretchDraw(r,bmp); bmp.free; r.left := r.right; r.top := 0; Canvas.Textout(r.left,r.top,'Printed with TAdvStringGrid'); r.top := r.top-canvas.textheight('gh'); Canvas.Textout(r.left,r.top,'showing how to add a bitmap in the header'); r.left := advstringgrid1.printcoloffset[1]; r.right := advstringgrid1.printcoloffset[8]; r.top := -advstringgrid1.printsettings.HeaderSize+2; Canvas.MoveTo(r.left,r.top); Canvas.LineTo(r.right,r.top); end;
First of all, the bitmap is created and loaded from a file. You can use a resource bitmap here as well (Use the LoadFromResourceName or LoadFromResourceID method instead of LoadFromFile). To print the bitmap within the header, the bitmap will be resized (keeping the aspect ratio!) to fit its height into the headersize. This height is set in the printsettings.headersize property. Notice that the printing is in MM_LOMETRIC mode, meaning a unit is 0.1mm and the vertical coordinates start at 0 and go down negative! The bitmap is drawn to the full headersize with the StretchDraw method and the width is resized with property aspect ratio. The left position of the bitmap is aligned to the first normal grid cell by using the PrintColOffset[1] property. As a last additional feature, a line is drawn here above the grid cells that contain data. Therefore, the start positions of these columns are used : printcoloffset[1] and printcoloffset[8]. Given this information, it should be easy to fully customize your headers and footers for printing with text as well as graphics.
Delphi project & source files for downloading included in the main demos distribution for Delphi.
×