Author: Lou Adler
How to Print a TStringGrid
Answer:
Here's the code. Just whack this onto a form's code and away you go.....
1
2 procedure PrintGrid(sGrid: TStringGrid; sTitle: string);
3 var
4 X1, X2: Integer;
5 Y1, Y2: Integer;
6 TmpI: Integer;
7 F: Integer;
8 TR: TRect;
9 begin
10 Printer.Title := sTitle;
11 Printer.BeginDoc;
12 Printer.Canvas.Pen.Color := 0;
13 Printer.Canvas.Font.Name := 'Times New Roman';
14 Printer.Canvas.Font.Size := 12;
15 Printer.Ccnvas.Font.Style := [fsBold, fsUnderline];
16 Printer.Canvas.TextOut(0, 100, Printer.Title);
17 for F := 1 to sGrid.ColCount - 1 do
18 begin
19 X1 := 0;
20 for TmpI := 1 to (F - 1) do
21 X1 := X1 + 5 * (sGrid.ColWidths[TmpI]);
22 Y1 := 300;
23 X2 := 0;
24 for TmpI := 1 to F do
25 X2 := X2 + 5 * (sGrid.ColWidths[TmpI]);
26 Y2 := 450;
27 TR := Rect(X1, Y1, X2 - 30, Y2);
28 Printer.Canvas.Font.Style := [fsBold];
29 Printer.Canvas.Font.Size := 7;
30 Printer.Canvas.TextRect(TR, X1 + 50, 350, sGrid.Cells[F, 0]);
31 Printer.Canvas.Font.Style := [];
32 for TmpI := 1 to sGrid.RowCount - 1 do
33 begin
34 Y1 := 150 * TmpI + 300;
35 Y2 := 150 * (TmpI + 1) + 300;
36 TR := Rect(X1, Y1, X2 - 30, Y2);
37 Printer.Canvas.TextRect(TR, X1 + 50, Y1 + 50, sGrid.Cells[F, TmpI]);
38 end;
39 end;
40 Printer.EndDoc;
41 end;
|