PRODUCTS

FEATURED PRODUCT

Grid, menus, calendars, advanced edits, navigational controls and much more... to create feature-rich IntraWeb applications faster

License only 125 EUR See More

SEARCH

LOGIN

Customer login to access products, support information & special benefits.

NEWS ALERTS

Add your e-mail address here to receive the monthly TMS Software alerts.

 

LINKS

EXAMPLE 9

TAdvStringGrid

example 9 : Using bitmaps in the header when printing

TAdvStringGrid

Adding a company logo or any other graphic to every page header (this applies to footers as well) is a fairly easy task. This example shows how to use a bitmap in the header to print reports. The most important issue is to control the size of the bitmap on the output device, since the resolution is device dependent. This will be discussed further. To print any additional information to the data in the grid, you can use the PrintPage event. This event is called for every page printed. Additional properties are available to help placement of images and text with respect to the columns that will be printed automatically by the TAdvStringGrid. These properties are:

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.

The project and source files have been written with Delphi 6,7. To use these files in other versions of Delphi, ignore any remarks when opening the form files and save the files. After this, compilation can be done. The error messages are due to properties included in the Delphi 6 form file, but not available in lower versions of Delphi.  

Copyright © 1995 - 2008 TMS Software