De jó, hogy már nem tudtam szerkeszteni. Na mindegy, írom külön:
A kód (kell hozzá a Printers unit, illetve egy Button1 és egy PrintDialog a Formon):
KÓD
procedure TForm1.Button1Click(Sender:TObject);
// Beállítások
const
Zoom: Real = 4.0; // nagyítás
AutoZoom: Boolean = true; // a szélesség automatikus igazítása az oldalszélességhez
MarginLeft: Integer = 12; // bal margó
MarginTop: Integer = 12; // felső margó
MarginRight: Integer = 12; // jobb margó
MarginBottom: Integer = 12; // alsó margó
CellPadding: Integer = 2; // cellán belüli margók
THBackground: TColor = $00CCCCCC; // a 'Fixed' cellák háttérszíne
TDBackground: TColor = $00FFFFFF; // a normális cellák háttérszíne
PageBackground: TColor = $00FFFFFF; // az oldal háttérszíne
LineColor: TColor = $00000000; // a cellák szegélyeinek színe
THFontColor: TColor = $00FFFFFF; // betűszín (normál cellák)
TDFontColor: TColor = $00000000; // betűszín (fixed cellák)
// Beállítások vége
var i, j: Integer;
FullWidth: Integer;
Zoom_: Real;
SumWidths: Integer;
SumHeights: Integer;
CurrRowHeight:Integer;
CurrColWidth: Integer;
begin
if PrintDialog1.Execute then
begin
Printer.BeginDoc;
Zoom_:=Zoom;
SumHeights:=round(MarginTop*Zoom_);
if AutoZoom Then
begin
FullWidth:=0;
for i:=0 to StringGrid1.ColCount-1 do
begin
FullWidth:=FullWidth+StringGrid1.ColWidths[i];
end;
FullWidth:=FullWidth+MarginLeft+MarginRight;
Zoom_:=(Printer.PageWidth)/(FullWidth);
end;
Printer.Canvas.Font:=StringGrid1.Font;
Printer.Canvas.Font.Size:=round(StringGrid1.Font.Size*Zoom_/(300/96));
Printer.Canvas.Brush.Style:=bsSolid;
Printer.Canvas.Brush.Color:=PageBackground;
Printer.Canvas.FillRect(Rect(0, 0, Printer.PageWidth, Printer.PageHeight));
for i:=0 to StringGrid1.RowCount-1 do
begin
// új lap, ha nem fér ki
if i<>0 Then SumHeights:=SumHeights+round(StringGrid1.RowHeights[i-1]*Zoom_);
CurrRowheight:=round(StringGrid1.RowHeights[i]*Zoom_);
if SumHeights+CurrRowHeight>(Printer.PageHeight-(MarginBottom*Zoom_)) Then
begin
Printer.NewPage;
SumHeights:=round(MarginTop*Zoom_);
end;
SumWidths:=round(MarginLeft*Zoom_);
for j:=0 to StringGrid1.ColCount-1 do
begin
if j<>0 Then SumWidths:=SumWidths+round(StringGrid1.ColWidths[j-1]*Zoom_);
CurrColWidth:=round(StringGrid1.ColWidths[j]*Zoom_);
// Külön formázzuk az első sort és az első oszlopot
if (i<StringGrid1.FixedCols) or (j<StringGrid1.FixedRows) Then
begin
Printer.Canvas.Font.Style:=[fsBold];
Printer.Canvas.Brush.Color:=THBackground;
Printer.Canvas.Font.Color:=THFontColor;
end
else
begin
Printer.Canvas.Font.Style:=[];
Printer.Canvas.Brush.Color:=TDBackground;
Printer.Canvas.Font.Color:=TDFontColor;
end;
Printer.Canvas.Rectangle(Rect(SumWidths,
SumHeights,
SumWidths+CurrColWidth,
SumHeights+CurrRowHeight));
Printer.Canvas.Brush.Style:=bsClear;
Printer.Canvas.TextRect(Rect(SumWidths,
SumHeights,
SumWidths+CurrColWidth,
SumHeights+CurrRowHeight),
SumWidths+round(CellPadding*Zoom_),
SumHeights+round(CellPadding*Zoom_),
StringGrid1.Cells[j, i]);
end;
end;
Printer.EndDoc;
end;
end;
Tud szegélyeket rajzolni a celláknak, bal oldalt és fent található, nem megváltoztatható sorok (fixed rows/cols, vagy mi) más színnel és betűstílussal jelennek meg, mint a többi. Változtatható betűszín, háttérszín (a celláknak), lapszín, szegélyszín és margók(oldalmargók, és belső margók a cellákban). Zoomolás, akár automatikusan is (a táblázat szélességét a lap szélességéhez igazítja), automatikus oldaltörés (ha nem fér ki a táblázat), illetve a szabványos Nyomtatás ablak megjelenítése. Ja, és támogatja azt is, ha a sorok, vagy oszlopok szélességei/magasságai nem egyformák...
Ez már egész használható, és kiindulási alapnak jó. A kód egy kicsit kusza ugyan, mert nem igazán terveztem, csak írtam, ami eszembe jutott.
Aztán ha kell, akkor még bővíthetem pl. fej- illetve lábléccel(oldalszámozás), vagy különféle vonaltípusokkal, stb.
Sok sikert a használatához!