如何实现将DBGrid内容进行预览呢?很实用的问题对大家均有好处(0分)

  • 主题发起人 主题发起人 huang_li
  • 开始时间 开始时间
H

huang_li

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾:
你们好。
以下是《delphi园地》摘录下来的代码,可以将表格中的内容按照表格高度,宽度等条件直接打印出来,好是好,但是就是不能预览呵?可否实现呢?类似金蝶中既可预览,又可打印的功能。以下代码可以改写后实现将它显示在form中。
谢谢。
{*=============================
*
*名 称:PrintDbGrid(DataSet:TDataSet;DbGrid:TDbGrid;Title:String);
*
*功 能: 将DBGrid内容直接送到打印机中(要求在uses中加入Printers)
*
*参数说明:此模块来源于《delphi园地》
*
*修改日期:问题是:怎样设置打印选项或者是预览呢?
*
*============================ }
procedure TForm1.PrintDbGrid(DataSet:TDataSet;DbGrid:TDbGrid;Title:String);
var
pointX,PointY:integer;
ScreenX:integer;
i,lx,ly:integer;
px1,py1,px2,py2:integer;
RowPerPage,RowPrinted:integer;
ScaleX:Real;
THeight:integer;
TitleWidth:integer;
SumWidth:integer;
PageCount:integer;
SpaceX,SpaceY:integer;
RowCount:integer;
begin
PointX:=Round(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/2.54);
PointY:=Round(GetDeviceCaps(Printer.Handle,LOGPIXELSY)/2.54);
ScreenX:=Round(Screen.PixelsPerInch/2.54);
ScaleX:=PointX/ScreenX;
RowPrinted:=0;
SumWidth:=0;
Printer.BeginDoc;
With Printer.Canvas do
begin
DataSet.DisableControls;
DataSet.First ;
THeight:=Round(TextHeight('我')*1.5);//设定每行高度为字符高的1.5倍
SpaceY:= Round(TextHeight('我')/4);
SpaceX:=Round(TextWidth('我')/4);
RowPerpage:=Round((Printer.PageHeight-5*PointY)/THeight); //上下边缘各2厘米
ly:=2*PointY;
PageCount:=0;
while not DataSet.Eof do
begin
if (RowPrinted=RowPerPage) or (RowPrinted=0) then
begin
if RowPrinted<>0 then
Printer.NewPage;
RowPrinted:=0;
PageCount:=PageCount+1;
Font.Name:='宋体';
Font.size:=16;
Font.Style:=Font.Style+[fsBold];
lx:=Round((Printer.PageWidth-TextWidth(Title))/2);
ly:=2*PointY;
TextOut(lx,ly,Title);
Font.Size:=11;
Font.Style:=Font.Style-[fsBold];
lx:=Printer.PageWidth-5*PointX;
ly:=Round(2*PointY+0.2*PointY);
if RowPerPage*PageCount>DataSet.RecordCount then
RowCount:=DataSet.RecordCount
else
RowCount:=RowPerPage*PageCount;
TextOut(lx,ly,'第'+IntToStr(RowPerPage*(PageCount-1)+1)+'-'+IntToStr(RowCount)+'条,共'+IntToStr(DataSet.RecordCount)+'条');
lx:=2*PointX;
ly:=ly+THeight*2;
py1:=ly-SpaceY;
if RowCount=DataSet.RecordCount then
py2:=py1+THeight*(RowCount-RowPerPage*(PageCount-1)+1)
else
py2:=py1+THeight*(RowPerPage+1);
SumWidth:=lx;
for i:=0 to DBGrid.Columns.Count-1 do
begin
px1:=SumWidth-SpaceX;
px2:=SumWidth;
MoveTo(px1,py1);
LineTo(px2,py2);
TitleWidth:=TextWidth(DBGrid.Columns.Title.Caption);
lx:=Round(SumWidth+(DBGrid.Columns.Width*scaleX-TitleWidth)/2);
TextOut(lx,ly,DBGrid.Columns.Title.Caption);
SumWidth:=Round(SumWidth+DBGrid.Columns.Width*scaleX)+SpaceX*2;
end;
px1:=SumWidth; //画最后一条竖线
px2:=SumWidth;
MoveTo(px1,py1);
LineTo(px2,py2);
px1:=2*PointX; //画第一条横线
px2:=SumWidth;
py1:=ly-SpaceY;
py2:=ly-SpaceY;
MoveTo(px1,py1);
LineTo(px2,py2);
py1:=py1+THeight;
py2:=py2+THeight;
MoveTo(px1,py1);
LineTo(px2,py2);
end;
lx:=2*PointX;
ly:=ly+THeight;
px1:=lx;
px2:=SumWidth;
py1:=ly-SpaceY+THeight;
py2:=ly-SpaceY+THeight;
MoveTo(px1,py1);
LineTo(px2,py2);
for i:=0 to DBGrid.Columns.Count-1 do
begin
TextOut(lx,ly,DataSet.FieldByname(DBGrid.Columns.Fieldname).AsString);
lx:=Round(lx+DBGrid.Columns.Width*ScaleX+SpaceX*2);
end;
RowPrinted:=RowPrinted+1;
DataSet.Next;
end;
DataSet.First;
DataSet.EnableControls;
end;
Printer.EndDoc;
end;
 
用DBGRIDEH就可以直接預覽打印了
 
嗯,支持使用第三方控件。。。。。。。还有cxgrid我的至爱
 
接受答案了.
 
后退
顶部