stringgrid控件内容打印的问题, 急急急!!!!(100分)

  • 主题发起人 主题发起人 icelily7631
  • 开始时间 开始时间
I

icelily7631

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位有什么方法能把一个STRINGGRID中的所有内容(包含表格)打印出来或能保存为BMP
图片或带表格的RTF格式都行,急急急!!!
 
try reportmachine
 
StringGrid.PaintTo(Handle, X, Y);

StringGrid.PaintTo(Canvas, x, y);
 
将图象直接复制到位图:

Image1.Canvas.CopyRect(stringgrid1.ClientRect,stringgrid1.Canvas,stringgrid1.ClientRect);
image1.savefile('c:/stringgrid1.bmp');
 
Procedure TACDListerMain.PrintTable;
Var
margins: TRect;
spacing: Integer;
Cols: TList;
Dlg: TPrintProgressDlg;

Procedure SetColumnWidth;
Var
i, k, w: Integer;
Begin
Printer.Canvas.Font.Style := [ fsBold ];
For i := 0 To Pred( Grid.ColCount ) Do
Cols.Add( Pointer( Printer.Canvas.TextWidth( Grid.Cells[ i,0 ] )));

Printer.Canvas.Font.Style := [];
For i := 1 To Pred( Grid.RowCount ) Do
For k := 0 To Pred( Grid.ColCount ) Do Begin
w:= Printer.Canvas.TextWidth( Grid.Cells[ k, i ] );
If w > Integer( Cols[ k ] ) Then
Cols[ k ] := Pointer( w );
End; { For }

w := 2 * Printer.Canvas.Font.PixelsPerInch div 3;
margins :=
Rect( w, w, Printer.PageWidth-w, Printer.PageHeight - w );
spacing := Printer.Canvas.Font.PixelsPerInch div 10;

w := 0;
For i := 0 To Pred(cols.Count) Do
w := w + Integer( cols[ i ] ) + spacing;
w := w - spacing;
If w > (margins.right-margins.left ) Then Begin
w := w - (margins.right-margins.left );
cols[ cols.Count-2 ] :=
Pointer( Integer( cols[ cols.Count-2 ] ) - w );
End; { If }

w:= 0;
For i := 0 To Pred(cols.Count) Do
w := w + Integer( cols[ i ] ) + spacing;
margins.right := w - spacing + margins.left;
End; { SetColumnWidth }

Procedure DoPrint;
Var
i: Integer;
y: Integer;
Procedure DoLine(lineno: Integer);
Var
x, n: Integer;
r: TRect;
th: Integer;
Begin
If Length(Grid.Cells[0,lineno]) = 0 Then Exit;

x:= margins.left;
With Printer.Canvas Do Begin
th := TextHeight( '膟' );
For n := 0 To Pred( Cols.Count ) Do Begin
r := Rect( 0, 0, Integer(Cols[ n ]), th);
OffsetRect( r, x, y );
TextRect( r, x, y, Grid.Cells[ n, lineno ] );
x := r.right + spacing;
End; { For }
End; { With }
y := y + th;
End; { DoLine }
Procedure DoHeader;
Begin
y:= margins.top;
With Printer.Canvas Do Begin
Font.Style := [ fsBold ];
DoLine( 0 );
Pen.Width := Font.PixelsPerInch div 72;
Pen.Color := clBlack;
MoveTo( margins.left, y );
LineTo( margins.right, y );
Inc( y, 2 * Pen.Width );
Font.Style := [ ];
End; { With }
End; { DoHeader }
Begin
y:= 0;
For i := 1 To Pred( Grid.RowCount ) Do Begin
Dlg.Progress( i );
Application.ProcessMessages;
If FPrintAborted Then Exit;

If y = 0 Then
DoHeader;
DoLine( i );
If y >= margins.bottom Then Begin
Printer.NewPage;
y:= 0;
End; { If }
End; { For }
End; { DoPrint }

Begin
FPrintAborted := False;
Dlg := TPrintProgressDlg.Create( Application );
With Dlg Do
try
OnAbort := PrintAborted;
Display( cPrintPreparation );
SetProgressRange( 0, Grid.RowCount );
Show;
Application.ProcessMessages;
Printer.Orientation := poLandscape;

Printer.BeginDoc;
Cols:= Nil;
try
Cols:= TLIst.Create;
Printer.Canvas.Font.Assign( Grid.Font );
SetColumnWidth;
Display( cPrintProceeding );
Application.ProcessMessages;
DoPrint;
finally
Cols.Free;
If FPrintAborted Then
Printer.Abort
Else
Printer.EndDoc;
end;
finally
Close;
End; { With }
End; { TACDListerMain.PrintTable }

 
to zbwsh:
如果STRINGGRID在表单中显示的区域比较小, 不足以显示出其中所有的内容, 很大一部分
内容可能由于SCROLLBAR的使用而隐藏了, 用您所说的这种方式, 能把所有的内容都打印出
来吗(包括隐藏部分)
 
打印Stringgrid的内容(转)

var
i,j,m,k,n,w,y,p:integer;
PointX,PointY,LCount,RCount,CX,CY,Widthw,Heightw:integer;
year,month,day,hour,min,sec,hsec:word;
date,time:string;
begin
n:=0;
decodedate(now,year,month,day);
decodetime(now,hour,min,sec,hsec);
date:=inttostr(year)+'年'+inttostr(month)+'月'+inttostr(day)+'日';
time:=inttostr(hour)+'时'+inttostr(min)+'分'+inttostr(sec)+'秒 ';
n:=application.MessageBox('确实要打印吗?',' 打印提示',3);
if n<>6 then exit;
PointX:=GetDeviceCaps(printer.Handle,LOGPIXELSX)div 3;
PointY:=GetDeviceCaps(printer.Handle,LOGPIXELSY)div 3;
m:=GetDeviceCaps(printer.handle,LOGPIXELSX)div 3;
n:=0;
printer.BeginDoc;
CX:=trunc(PointX*1.5);
CY:=PointY*3;
widthw:=(printer.PageWidth-cx)div stringgrid1.ColCount ;
printer.Canvas.Pen.Width:=15;
printer.Canvas.Font.Size:=20;
printer.Canvas.Font.Name:='楷体_GB2312';
printer.Canvas.TextOut(printer.PageWidth div 3,m,'清单');
printer.Canvas.Pen.Width:=2;
printer.Canvas.Font.Size:=10;
printer.Canvas.MoveTo(pointx ,pointy+m*2);
printer.Canvas.LineTo((widthw+cx div 15)*(stringgrid1.ColCount) ,pointy+m*2);
printer.Canvas.TextOut(widthw,pointy+m+cx div 4,date);
printer.Canvas.TextOut((stringgrid1.ColCount-2)*widthw,pointy+m +cx div 4,'打印者:'+czy);
for i:=0 to stringgrid1.RowCount-2 do
begin
for j:=0 to stringgrid1.ColCount-1 do
begin
if (PointX=GetDeviceCaps(printer.Handle,LOGPIXELSX)div 3)and (PointY=GetDeviceCaps(printer.Handle,LOGPIXELSY)div 3)and(n=1) then
begin
printer.Canvas.Pen.Width:=15;
printer.Canvas.Font.Size:=20;
printer.Canvas.Font.Name:='楷体_GB2312';
printer.Canvas.TextOut(printer.PageWidth div 3,m,'清单');
printer.Canvas.Pen.Width:=2;
printer.Canvas.Font.Size:=10;
printer.Canvas.MoveTo(pointx ,pointy+m*2);
printer.Canvas.LineTo((widthw+cx div 15)*(stringgrid1.ColCount) ,pointy+m*2);
printer.Canvas.TextOut(widthw,pointy+m+cx div 4,date);
printer.Canvas.TextOut((stringgrid1.ColCount-2)*widthw,pointy+m +cx div 4,'打印者:'+czy);
for w:=0 to stringgrid1.ColCount do
begin
printer.Canvas.TextOut(pointx+widthw*w,pointy+m+m+m div 3 ,' '+stringgrid1.Cells[w,0]);
printer.Canvas.MoveTo(pointx+widthw*w ,pointy+2*m );
printer.Canvas.LineTo(pointx+widthw*w,pointy+m*3);
end;
pointy:=pointy+m ;
printer.Canvas.MoveTo(pointx ,pointy+m*2);
printer.Canvas.LineTo((widthw+cx div 15)*(stringgrid1.ColCount),pointy+m*2);
end;
printer.Canvas.TextOut(pointx+widthw*j,pointy+m+m+m div 3 ,' '+stringgrid1.Cells[j,i]);
printer.Canvas.MoveTo(pointx+widthw*j ,pointy+2*m );
printer.Canvas.LineTo(pointx+widthw*j,pointy+m*3);
end;
printer.Canvas.MoveTo(pointx+widthw*j ,pointy+2*m );
printer.Canvas.LineTo(pointx+widthw*j,pointy+m*3);
pointy:=pointy+m ;
if pointy+m&amp;gt;printer.PageHeight-2*cx then
begin
printer.Canvas.MoveTo(pointx ,pointy+m*2);
printer.Canvas.LineTo((widthw+cx div 15)*(stringgrid1.ColCount),pointy+m*2);
PointX:=GetDeviceCaps(printer.Handle,LOGPIXELSX)div 3;
PointY:=GetDeviceCaps(printer.Handle,LOGPIXELSY)div 3;
n:=1;
printer.Canvas.MoveTo(pointx ,pointy+m*2);
printer.Canvas.LineTo((widthw+cx div 15)*(stringgrid1.ColCount),pointy+m*2);
printer.Canvas.TextOut(printer.PageWidth div 2-cx,printer.PageHeight-cx div 4,'第 '+inttostr(printer.PageNumber )+' 页');
printer.NewPage;
end;
PointX:=GetDeviceCaps(printer.Handle,LOGPIXELSX)div 3;
printer.Canvas.MoveTo(pointx ,pointy+m*2);
printer.Canvas.LineTo((widthw+cx div 15)*(stringgrid1.ColCount),pointy+m*2);
end;
printer.Canvas.TextOut(printer.PageWidth div 2-cx,printer.PageHeight-cx div 4,'第 '+inttostr(printer.PageNumber )+' 页');
printer.EndDoc;
end;
 
to icelily7631,
不能。您可以自己画在一个canvas上,如:原野飞侠的方法。
 
多人接受答案了。
 
后退
顶部