Example 25 : Printing multiple grids on a pageIn some cases, it is flexible to be able to print the contents of multiple grids on a single page. Before v1.90, the grid Print routines encapsulated the complete handling of the print sequence, making it hard to adapt it for printing multiple items on a page. With 2 new methods PrintDraw and PrintDrawRect this is now much easier. The sample project here shows how 4 grids can be printed each on 1/4 of the page. This is the definition of the PrintDraw and PrintDrawRect methods:
PrintDraw(canvas:TCanvas;drawrect:TRect); Print the grid contents in the rectangle drawrect on the canvas.
PrintDrawRect(canvas:TCanvas;drawrect:TRect;gr:TGridRect); Print contents of the cells within the grid rectangle gr, in the rectangle drawrect on the canvas.
The PrintDraw method has been used in the procedure below to print the contents of the 4 grids on a single page. Therefore, the cliprectangle of the printer canvas is obtained and divided in 4 parts and each grid is printed in one quarter of the page. The drawrect indicates the space the grid can use for its printing but not the position ! The position of the grid on the page itself is set with the LeftSize and HeaderSize properties in the PrintSettings. Note that this position is expressed in mm_lometrics units, thus 1/10mm. Therefore, the position is converted from device units to logical units after setting the mapmode to mm_lometrics with the Windows DpToLp method. Further, by setting the PrintSettings.FitToPage to fpShrink, we make sure that the grid is shrinked to the rectangle it is allowed to print in.
procedure TForm1.Button1Click(Sender: TObject); var r:TRect; quarterpage:TRect; begin printersetupdialog1.Execute; with Printer do begin BeginDoc; r := Canvas.ClipRect; quarterpage:=rect(0,0,(r.right-r.left) shr 1,(r.bottom-r.top) shr 1); setmapmode(canvas.handle,mm_lometric); dptolp(canvas.handle,r,2); advstringgrid1.PrintSettings.FitToPage:=fpShrink; advstringgrid1.PrintDraw(canvas,quarterpage); advstringgrid2.PrintSettings.HeaderSize:=(-r.bottom) shr 1; advstringgrid2.PrintSettings.FitToPage:=fpShrink; advstringgrid2.PrintDraw(canvas,quarterpage); advstringgrid3.PrintSettings.LeftSize:=r.right shr 1; advstringgrid3.PrintSettings.FitToPage:=fpShrink; advstringgrid3.PrintDraw(canvas,quarterpage); advstringgrid4.PrintSettings.LeftSize:=r.right shr 1; advstringgrid4.PrintSettings.HeaderSize:=(-r.bottom) shr 1; advstringgrid4.PrintSettings.FitToPage:=fpShrink; advstringgrid4.PrintDraw(canvas,quarterpage); EndDoc; end; end;
Delphi project & source files for downloading included in the main demos distribution for Delphi.