mrod0
asked on
Print a stringgrid
Hello, I wounder how I do to dump a stringgrid to the printer the easiest way. Can someone please tell me how to do? I use Delphi 5. Answer quick, this question is worth lots to me!
Can you put it on a temporary form and print it out? I am assuming you just want to print out a stringgrid.
You can also use a QRImage on a QuickReport. Use a TQuickRep, a TQRBand and a TQRImage. The TQRImage must be in the TQRBand.
procedure TForm1.Button1Click(Sender : TObject);
begin
QRImage1.Canvas.CopyRect(S tringGrid1 .ClientRec t, StringGrid1.Canvas, StringGrid1.ClientRect);
QuickRep1.Preview;
// QuickRep1.Print;
end;
procedure TForm1.Button1Click(Sender
begin
QRImage1.Canvas.CopyRect(S
QuickRep1.Preview;
// QuickRep1.Print;
end;
quick and simple
with Printer do
begin
BeginDoc;
StringGrid1.PaintTo(Handle , 10, 10)
EndDoc;
end; // with
with Printer do
begin
BeginDoc;
StringGrid1.PaintTo(Handle
EndDoc;
end; // with
can't guarantee it will work, because I can't test it until I get to work tomorrow, but give it a try supposedly it should work...
get this free component
http://sunsite.informatik.rwth-aachen.de/delphi/ftp/d40free/listprin.zip
http://sunsite.informatik.rwth-aachen.de/delphi/ftp/d40free/listprin.zip
procedure PrintGrid(sGrid: TStringGrid; sTitle: String);
var
X1, X2 : Integer;
Y1, Y2 : Integer;
TmpI : Integer;
F : Integer;
TR : TRect;
begin
Printer.Title:=sTitle;
Printer.BeginDoc;
Printer.Canvas.Pen.Color:= 0;
Printer.Canvas.Font.Name:= 'Times New Roman';
Printer.Canvas.Font.Size:= 12;
Printer.Canvas.Font.Style: =[fsBold, fsUnderline];
Printer.Canvas.TextOut(0, 100, Printer.Title);
For F:=1 to sGrid.ColCount-1 do begin
X1:=0;
For TmpI:=1 to (F-1) do
X1:=X1+5*(sGrid.ColWidths[ TmpI]);
Y1:=300;
X2:=0;
For TmpI:=1 to F do
X2:=X2+5*(sGrid.ColWidths[ TmpI]);
Y2:=450;
TR:=Rect(X1, Y1, X2-30, Y2);
Printer.Canvas.Font.Style: =[fsBold];
Printer.Canvas.Font.Size:= 7;
Printer.Canvas.TextRect(TR , X1+50, 350, sGrid.Cells[F, 0]);
Printer.Canvas.Font.Style: =[];
For TmpI:=1 to sGrid.RowCount-1 do begin
Y1:=150*TmpI+300;
Y2:=150*(TmpI+1)+300;
TR:=Rect(X1, Y1, X2-30, Y2);
Printer.Canvas.TextRect(TR , X1+50, Y1+50, sGrid.Cells[F, TmpI]);
end;
end;
Printer.EndDoc;
end;
var
X1, X2 : Integer;
Y1, Y2 : Integer;
TmpI : Integer;
F : Integer;
TR : TRect;
begin
Printer.Title:=sTitle;
Printer.BeginDoc;
Printer.Canvas.Pen.Color:=
Printer.Canvas.Font.Name:=
Printer.Canvas.Font.Size:=
Printer.Canvas.Font.Style:
Printer.Canvas.TextOut(0, 100, Printer.Title);
For F:=1 to sGrid.ColCount-1 do begin
X1:=0;
For TmpI:=1 to (F-1) do
X1:=X1+5*(sGrid.ColWidths[
Y1:=300;
X2:=0;
For TmpI:=1 to F do
X2:=X2+5*(sGrid.ColWidths[
Y2:=450;
TR:=Rect(X1, Y1, X2-30, Y2);
Printer.Canvas.Font.Style:
Printer.Canvas.Font.Size:=
Printer.Canvas.TextRect(TR
Printer.Canvas.Font.Style:
For TmpI:=1 to sGrid.RowCount-1 do begin
Y1:=150*TmpI+300;
Y2:=150*(TmpI+1)+300;
TR:=Rect(X1, Y1, X2-30, Y2);
Printer.Canvas.TextRect(TR
end;
end;
Printer.EndDoc;
end;
Or you could use something like TAdvStringGrid which supports printing (you can download it from DSP (http://sunsite.icm.edu.pl/delphi).
Cheers,
Raymond.
Cheers,
Raymond.
Here is some more code that may help you out. PaintTo works but you would have to do more than what I had said. This will work bettter.
procedure Grid.DrawToCanvas(ACanvas: TCanvas; Mode: TPrintMode; FromRow, ToRow: Integer);
var
PageWidth, PageHeight, PageRow,PageCol,I, iRow, FromCol,ToCol, X,Y: Integer;
DoPaint,haslogo: Boolean;
Hheader,Hfooter:integer;
logopic,logopics:TBitmap;
function ScaleX(I:Integer): Integer;
begin
if Mode = pmPreview then
Result := I
else
Result :=round( I * (GetDeviceCaps(Printer.Han dle, LOGPIXELSX) / Screen.PixelsPerInch));
end;
function ScaleY(I:Integer): Integer;
begin
if Mode = pmPreview then
Result := I
else
Result := round(I * (GetDeviceCaps(Printer.Han dle, LOGPIXELSY) / Screen.PixelsPerInch));
end;
procedure DrawCells(iRow:Integer);
var
iCol,I: Integer;
R: TRect;
drs:string;
nr:boolean;
v:extended;
begin
//Alignment must be done another day
for iCol := FromCol to ToCol do
begin
if ColWidths[iCol]<>0 then begin
//X Offset
X := scaleX(printoptions.margin left);
for I := FromCol to iCol-1 do
Inc(X, ScaleX(ColWidths[I]+1));
//Text Rect
R := Rect(X,Y, X+ScaleX(ColWidths[iCol]), Y+ScaleY(RowHeights[iRow]) );
//Draw on the Canvas
if DoPaint then begin
if PrintOptions.BorderStyle =bssingle then begin
Acanvas.brush.Style :=bsclear;
Acanvas.Rectangle (r.left,r.top,r.right+Scal eX(2),r.bo ttom+scale Y(1));
end;
drs:=Cells[iCol, iRow];
nr:=false;
if FShowValues then
if drs<>'' then
if drs[1]='=' then begin
drs:=format(FNumberFormat, [FCellValu es[icol,ir ow]]);
if NumbersalRight then nr:=true;
end;
if ((irow=0)and(icol>0)) then
Acanvas.font.style:=Acanva s.Font.sty le+[fsbold ]
else
Acanvas.font.style:=Acanva s.Font.sty le-[fsbold ];
R.left:=R.left+scaleX(Prin tOptions.L eftpadding );
if (FWordWrap and (iCol<>0) and (iRow<>0)) then begin
if (NumbersalRight and (not nr))then
try
v:=strtofloat(drs);
nr:=true;
drs:=format(FNumberFormat, [v]);
except
// do nothing
end;
if nr then
DrawText(Acanvas.handle,pc har(drs),- 1,R,DT_WOR DBREAK or DT_RIGHT)
else
DrawText(Acanvas.handle,pc har(drs),- 1,R,DT_WOR DBREAK or DT_LEFT)
end
else begin
if (NumbersalRight and (not nr)) then
try
v:=strtofloat(drs);
nr:=true;
drs:=format(FNumberFormat, [v]);
except
// do nothing
end;
if nr then
DrawText(Acanvas.handle,pc har(drs),- 1,R,DT_SIN GLELINE or DT_RIGHT)
else
DrawText(Acanvas.handle,pc har(drs),- 1,R,DT_SIN GLELINE or DT_LEFT)
end;
end;
end;
end;
Inc(Y, ScaleY(RowHeights[iRow]));
end;
procedure DrawTitle; //draw Header and Footer
var
S,fstr: String;
flist:tstringlist;
fcnt,i:integer;
tmpfont:tfont;//I have no idea why you can't use gettextwidth when acanvas = printer.canvas, it returns wrong value
begin
if DoPaint then
begin
ACanvas.Font.Size := FprintOptions.HeaderSize ;
tmpfont:=font;
canvas.font := acanvas.font;
end;
//Title
Y := ScaleY(PrintOptions.Margin Top);
S := PrintOptions.PageTitle;
HHeader:=canvas.textheight (s);
if haslogo then if logopic.Height >HHeader then HHeader:=logopic.height;
if DoPaint then begin
if haslogo then begin
Acanvas.Draw(scaleX(printo ptions.mar ginleft),Y ,logopics) ;
end;
ACanvas.TextOut( (PageWidth div 2) - (ScaleX(Canvas.TextWidth(S ) div 2)), Y, S);
end;
Y:=Y+ScaleY(HHeader);
//Page nr
S := 'Page '+IntToStr(PageRow);
if (ToCol < ColCount-1) or (PageCol > 1) then
S := S+'-'+IntToStr(PageCol);
fstr:=Printoptions.PageFoo ter ;
HFooter:=canvas.textheight (fstr);
if fstr<>'' then
if DoPaint then begin
ACanvas.Font.Size := FprintOptions.FooterSize ;
canvas.font := acanvas.font;
HFooter:=canvas.textheight (fstr);
flist:=tstringlist.create;
flist.text:=stringreplace( fstr,'|',c r,[rfrepla ceall]);
while flist.count<3 do
flist.Append ('');
for i:=0 to 2 do begin
flist[i]:=stringreplace(fl ist[i],'da te',format datetime(P rintOption s.Dateform at,now),[] );
flist[i]:=stringreplace(fl ist[i],'ti me',format datetime(P rintOption s.Timeform at,now),[] );
flist[i]:=stringreplace(fl ist[i],'pa ge',s,[]);
end;
//paint left footer
if flist[0]<>'' then
ACanvas.TextOut( scaleX(Printoptions.margin left+Canva s.TextWidt h(flist[0] )), PageHeight-ScaleY(PrintOpt ions.margi nbottom+ca nvas.TextH eight(flis t[0])), flist[0]);
//paint center footer
if flist[1]<>'' then
ACanvas.TextOut( (PageWidth div 2)-(scaleX(Canvas.TextWidt h(flist[1] ))div 2), PageHeight-ScaleY(PrintOpt ions.margi nbottom+ca nvas.TextH eight(flis t[1])), flist[1]);
//paint right footer
if flist[2]<>'' then
ACanvas.TextOut( PageWidth-scaleX(Printopti ons.margin right+Canv as.TextWid th(flist[2 ])+10), PageHeight-ScaleY(PrintOpt ions.margi nbottom+ca nvas.TextH eight(flis t[2])), flist[2]);
flist.free;
end;
if DoPaint then
begin
ACanvas.Font.Size := Font.Size;
canvas.font := tmpfont;//Delphi 4.0 warning is wrong
end;
Y := Y+ScaleY(PrintOptions.Page TitleMargi n);
DrawCells(0);
end;
begin
//page size
Printer.Orientation :=PrintOptions.Orientation ;
PageWidth := Printer.PageWidth;
PageHeight := Printer.PageHeight;
if Mode = pmPreview then
begin
PageWidth := PageWidth div ((GetDeviceCaps(Printer.Ha ndle, LOGPIXELSX) div Screen.PixelsPerInch));
PageHeight := PageHeight div ((GetDeviceCaps(Printer.Ha ndle, LOGPIXELSY) div Screen.PixelsPerInch));
FPrintImage.width:=pagewid th;
FPrintImage.height:=pagehe ight;
ACanvas.Brush.Color := ClWhite;
ACanvas.FillRect( Rect(0,0,PageWidth,PageHei ght));
end;
haslogo:=false;
if printoptions.Logo <>'' then
if fileexists(printoptions.lo go) then begin
logopic:=tbitmap.create;
logopic.LoadFromFile (printoptions.logo);
haslogo:=true;
logopics:=tbitmap.create;
logopics.width:=scaleX(log opic.width );
logopics.height:=scaleY(lo gopic.heig ht);
logopic.PixelFormat :=pf24bit;
logopics.pixelformat:=pf24 bit;
smoothresize(logopic,logop ics);
end;
if Mode <> pmPageCount then
begin
ACanvas.Font := Font;
ACanvas.Font.Color := clBlack;
end;
PageCol := 0;
FromCol := -1;
ToCol := -0;
//scan cols
repeat
//Scan missing cols
if FromCol = ToCol then
Inc(FromCol)
else
FromCol := ToCol+1;
Inc(ToCol);
//Get Cols with width that fits page
X := PrintOptions.MarginLeft ;
for I := FromCol to ColCount-1 do
begin
Inc(X, ScaleX(ColWidths[I]+1));
if X <= (PageWidth-PrintOptions.Ma rginRight) then
ToCol := I;
end;
PageRow := 1;
Inc(PageCol);
//Mode = PageCount
Inc(fPageCount);
//preview mode
DoPaint := (((Mode = pmPreview) and (fPageCount = PrintOptions.PreviewPage)) or (Mode = pmPrint));
//Header & Footer
DrawTitle;
//Contents
iRow := FromRow;
repeat
// Inc(Y, ScaleY(RowHeights[iRow]));
if (Y+ScaleY(RowHeights[iRow] )) <= (PageHeight-ScaleY(Printop tions.marg inbottom+2 0+HFooter) ) then
begin //draw contents to canvas
if RowHeights[iRow]<>0 then
DrawCells(iRow);
Inc(iRow);
end
else//New page
begin
if (DoPaint = True) and (Mode = pmPreview) then
Exit;
if Mode = pmPrint then
Printer.NewPage;
Inc(fPageCount);//pagecoun t
DoPaint := (((Mode = pmPreview) and (fPageCount = PrintOptions.PreviewPage)) or (Mode = pmPrint));
Inc(PageRow);
DrawTitle;
end;
if (iRow = ToRow+1) and (ToCol < ColCount-1) and (Y <= PageHeight-ScaleY(20)) then
begin
if (DoPaint = True) and (Mode = pmPreview) then
Exit;
if Mode = pmPrint then
Printer.NewPage;
DrawTitle;
end;
until
iRow = ToRow+1;
until
ToCol = ColCount-1;
if haslogo then begin
logopic.free;
logopics.free;
end;
end;
procedure Grid.DrawToCanvas(ACanvas:
var
PageWidth, PageHeight, PageRow,PageCol,I, iRow, FromCol,ToCol, X,Y: Integer;
DoPaint,haslogo: Boolean;
Hheader,Hfooter:integer;
logopic,logopics:TBitmap;
function ScaleX(I:Integer): Integer;
begin
if Mode = pmPreview then
Result := I
else
Result :=round( I * (GetDeviceCaps(Printer.Han
end;
function ScaleY(I:Integer): Integer;
begin
if Mode = pmPreview then
Result := I
else
Result := round(I * (GetDeviceCaps(Printer.Han
end;
procedure DrawCells(iRow:Integer);
var
iCol,I: Integer;
R: TRect;
drs:string;
nr:boolean;
v:extended;
begin
//Alignment must be done another day
for iCol := FromCol to ToCol do
begin
if ColWidths[iCol]<>0 then begin
//X Offset
X := scaleX(printoptions.margin
for I := FromCol to iCol-1 do
Inc(X, ScaleX(ColWidths[I]+1));
//Text Rect
R := Rect(X,Y, X+ScaleX(ColWidths[iCol]),
//Draw on the Canvas
if DoPaint then begin
if PrintOptions.BorderStyle =bssingle then begin
Acanvas.brush.Style :=bsclear;
Acanvas.Rectangle (r.left,r.top,r.right+Scal
end;
drs:=Cells[iCol, iRow];
nr:=false;
if FShowValues then
if drs<>'' then
if drs[1]='=' then begin
drs:=format(FNumberFormat,
if NumbersalRight then nr:=true;
end;
if ((irow=0)and(icol>0)) then
Acanvas.font.style:=Acanva
else
Acanvas.font.style:=Acanva
R.left:=R.left+scaleX(Prin
if (FWordWrap and (iCol<>0) and (iRow<>0)) then begin
if (NumbersalRight and (not nr))then
try
v:=strtofloat(drs);
nr:=true;
drs:=format(FNumberFormat,
except
// do nothing
end;
if nr then
DrawText(Acanvas.handle,pc
else
DrawText(Acanvas.handle,pc
end
else begin
if (NumbersalRight and (not nr)) then
try
v:=strtofloat(drs);
nr:=true;
drs:=format(FNumberFormat,
except
// do nothing
end;
if nr then
DrawText(Acanvas.handle,pc
else
DrawText(Acanvas.handle,pc
end;
end;
end;
end;
Inc(Y, ScaleY(RowHeights[iRow]));
end;
procedure DrawTitle; //draw Header and Footer
var
S,fstr: String;
flist:tstringlist;
fcnt,i:integer;
tmpfont:tfont;//I have no idea why you can't use gettextwidth when acanvas = printer.canvas, it returns wrong value
begin
if DoPaint then
begin
ACanvas.Font.Size := FprintOptions.HeaderSize ;
tmpfont:=font;
canvas.font := acanvas.font;
end;
//Title
Y := ScaleY(PrintOptions.Margin
S := PrintOptions.PageTitle;
HHeader:=canvas.textheight
if haslogo then if logopic.Height >HHeader then HHeader:=logopic.height;
if DoPaint then begin
if haslogo then begin
Acanvas.Draw(scaleX(printo
end;
ACanvas.TextOut( (PageWidth div 2) - (ScaleX(Canvas.TextWidth(S
end;
Y:=Y+ScaleY(HHeader);
//Page nr
S := 'Page '+IntToStr(PageRow);
if (ToCol < ColCount-1) or (PageCol > 1) then
S := S+'-'+IntToStr(PageCol);
fstr:=Printoptions.PageFoo
HFooter:=canvas.textheight
if fstr<>'' then
if DoPaint then begin
ACanvas.Font.Size := FprintOptions.FooterSize ;
canvas.font := acanvas.font;
HFooter:=canvas.textheight
flist:=tstringlist.create;
flist.text:=stringreplace(
while flist.count<3 do
flist.Append ('');
for i:=0 to 2 do begin
flist[i]:=stringreplace(fl
flist[i]:=stringreplace(fl
flist[i]:=stringreplace(fl
end;
//paint left footer
if flist[0]<>'' then
ACanvas.TextOut( scaleX(Printoptions.margin
//paint center footer
if flist[1]<>'' then
ACanvas.TextOut( (PageWidth div 2)-(scaleX(Canvas.TextWidt
//paint right footer
if flist[2]<>'' then
ACanvas.TextOut( PageWidth-scaleX(Printopti
flist.free;
end;
if DoPaint then
begin
ACanvas.Font.Size := Font.Size;
canvas.font := tmpfont;//Delphi 4.0 warning is wrong
end;
Y := Y+ScaleY(PrintOptions.Page
DrawCells(0);
end;
begin
//page size
Printer.Orientation :=PrintOptions.Orientation
PageWidth := Printer.PageWidth;
PageHeight := Printer.PageHeight;
if Mode = pmPreview then
begin
PageWidth := PageWidth div ((GetDeviceCaps(Printer.Ha
PageHeight := PageHeight div ((GetDeviceCaps(Printer.Ha
FPrintImage.width:=pagewid
FPrintImage.height:=pagehe
ACanvas.Brush.Color := ClWhite;
ACanvas.FillRect( Rect(0,0,PageWidth,PageHei
end;
haslogo:=false;
if printoptions.Logo <>'' then
if fileexists(printoptions.lo
logopic:=tbitmap.create;
logopic.LoadFromFile (printoptions.logo);
haslogo:=true;
logopics:=tbitmap.create;
logopics.width:=scaleX(log
logopics.height:=scaleY(lo
logopic.PixelFormat :=pf24bit;
logopics.pixelformat:=pf24
smoothresize(logopic,logop
end;
if Mode <> pmPageCount then
begin
ACanvas.Font := Font;
ACanvas.Font.Color := clBlack;
end;
PageCol := 0;
FromCol := -1;
ToCol := -0;
//scan cols
repeat
//Scan missing cols
if FromCol = ToCol then
Inc(FromCol)
else
FromCol := ToCol+1;
Inc(ToCol);
//Get Cols with width that fits page
X := PrintOptions.MarginLeft ;
for I := FromCol to ColCount-1 do
begin
Inc(X, ScaleX(ColWidths[I]+1));
if X <= (PageWidth-PrintOptions.Ma
ToCol := I;
end;
PageRow := 1;
Inc(PageCol);
//Mode = PageCount
Inc(fPageCount);
//preview mode
DoPaint := (((Mode = pmPreview) and (fPageCount = PrintOptions.PreviewPage))
//Header & Footer
DrawTitle;
//Contents
iRow := FromRow;
repeat
// Inc(Y, ScaleY(RowHeights[iRow]));
if (Y+ScaleY(RowHeights[iRow]
begin //draw contents to canvas
if RowHeights[iRow]<>0 then
DrawCells(iRow);
Inc(iRow);
end
else//New page
begin
if (DoPaint = True) and (Mode = pmPreview) then
Exit;
if Mode = pmPrint then
Printer.NewPage;
Inc(fPageCount);//pagecoun
DoPaint := (((Mode = pmPreview) and (fPageCount = PrintOptions.PreviewPage))
Inc(PageRow);
DrawTitle;
end;
if (iRow = ToRow+1) and (ToCol < ColCount-1) and (Y <= PageHeight-ScaleY(20)) then
begin
if (DoPaint = True) and (Mode = pmPreview) then
Exit;
if Mode = pmPrint then
Printer.NewPage;
DrawTitle;
end;
until
iRow = ToRow+1;
until
ToCol = ColCount-1;
if haslogo then begin
logopic.free;
logopics.free;
end;
end;
> Answer quick, this question is worth lots to me!
Why don't respond then?
Johnch: Withdraw your answer. Others have also proposed solutions so it's inappropriate of you to lock this question.
Why don't respond then?
Johnch: Withdraw your answer. Others have also proposed solutions so it's inappropriate of you to lock this question.
mrod0 - Please return to your question and review the posts made by the Experts.
darinw
Customer Service
darinw
Customer Service
Johnch changed the proposed answer to a comment
ASKER
Johnch, answer and I'll give you the points, sorry I havn't done this but I havn't had the possibility until today.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Answer accepted