用代码写打印DBGRID,能不能实现在打印前实现预乱?(0分)

  • 主题发起人 主题发起人 small pig
  • 开始时间 开始时间
S

small pig

Unregistered / Unconfirmed
GUEST, unregistred user!
uses
......printers
procedure TcxFrm.printButtonClick(Sender: TObject);
const
LeftBlank=1;
//定义页边距,单位厘米
RightBlank=1;
TopBlank=1;
BottomBlank=1;
var
pointX,PointY:integer;
PointScale,PrintStep,printW:integer;
s:string;
x,y,z:integer;
i,j:integer;
begin
if printersetupdialog1.execute=true then
begin
//获取当前打印机的分辨率
PointX:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/2.54);
pointY:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSY)/2.54);
//根据打印机和屏幕的分辨率计算出从屏幕转换到打印机的比例 
PointScale:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/Screen.PixelsPerInch+0.5);
//横向打印
//printer.Orientation:=poportrait;
//poLandscape;
//打印的字体和大小
printer.Canvas.Font.Name:='宋体';
printer.canvas.Font.Size:=11;
//根据字体的大小确定每行的高度
s:='各车间劳保到期情况';
PrintStep:=printer.canvas.TextHeight(s)+16;
printW:=printer.canvas.textwidth(s);
//打印的起点位置
x:=PointX*LeftBlank;
z:=pointX*leftblank;
y:=PointY*TopBlank;
j:=1;
//DataSource1是DBGrid1所连接的数据源
if ((DataSource1.DataSet).Active=true) and ((DataSource1.DataSet).RecordCount>0) then
begin
DataSource1.dataSet.disableControls;
printer.begin
Doc;
for i:=0 to DBGrid1.FieldCount-1do
z:=z+DBGrid1.Columns.Items.Width*PointScale;
(DataSource1.DataSet).First;
while not (DataSource1.DataSet).Eofdo
begin
//打印DBGrid中的所有列
for i:=0 to DBGrid1.FieldCount-1do
begin
//假如所要 打印的列超出了打印范围,则忽略该列
if (x+DBGrid1.Columns.Items.Width*PointScale)<=(Printer.PageWidth-PointX*RightBlank) then
begin
//画表格线 每页的第一行打印表头
Printer.Canvas.Rectangle(x,y,x+DBGrid1.Columns.Items.Width*PointScale,y+PrintStep);
if y=PointY*TopBlank then
begin
printer.canvas.Font.Size:=14;
printer.canvas.textout(trunc((z-printw) div 2),1,'综合数据库');
printer.canvas.Font.Size:=11;
Printer.Canvas.TextOut(x+8,y+8,DBGrid1.Columns.Title.Caption);
end
else
Printer.Canvas.TextOut(x+8,y+8,DBGrid1.Fields.asString);
end;
//计算下一列的横坐标
x:=x+DBGrid1.Columns.Items.Width*PointScale;
end;
if not (y=PointY*TopBlank) then
(DataSource1.DataSet).next;
x:=PointX*LeftBlank;
y:=y+PrintStep;
//换页
if ((y+PrintStep)>(Printer.PageHeight-PointY*BottomBlank))
or ((DataSource1.DataSet).eof) then
begin
printer.canvas.TextOut(trunc(z/2),y+printstep,'第'+inttostr(j)+'页');
Printer.NewPage;
y:=PointY*TopBlank;
j:=j+1;
end;
end;
printer.EndDoc;
(DataSource1.DataSet).First;
DataSource1.dataset.EnableControls;
Application.MessageBox('正在打印,请等待。','提示:',32);
end;
end;

end;

 
你好!我做有类似的控件,DEMO可以到http://www.efile.com.cn上用dfw@133780搜索就可以看到[:D]
 
接受答案了.
 
后退
顶部