这个是分页打印的,代码写得乱点,预览的代码与这一段差不多,就是画到一个TPaintbox的canvas上了。
procedure TTjcxForm.N1Click(Sender: TObject);
var
PointX,PointY:integer;
PointScale,PrintStep:integer;
s,str:string;
x,y:integer;
i,j,n,GridNum,RightValue,m:integer;
AGrid:TDBGrid;
Newpage,NewGrid:boolean;
hj1:Real;
begin
AGrid:=DBGrid1;
//获取当前打印机的分辨率
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:=poLandscape;
//打印的字体和大小
printer.Canvas.Font.Name:='宋体';
printer.canvas.Font.Size:=10;
//根据字体的大小确定每行的高度
str:='药品调价单';
PrintStep:=printer.canvas.TextHeight(str)+16;
// pagenum:=1;
GridNum:=7;
n:=0;
hj1:=0;
// hj2:=0;
// hj3:=0;
//DataSource1是DBGrid1所连接的数据源
RightValue:=Round(LeftBlank*Pointx);
for i:=0 to DBGrid1.Columns.Count-1do
begin
RightValue:=RightValue+DBGrid1.Columns.Items.Width*PointScale;
end;
//打印的起点位置
x:=Round(LeftBlank*Pointx);
y:=PointY*TopBlank;
if ((AGrid.DataSource.DataSet).Active=true) and ((AGrid.DataSource.DataSet).RecordCount>0) then
begin
NewGrid:=True;
NewPage:=True;
(AGrid.DataSource.DataSet).First;
try
printer.begin
Doc;
// Printer.Canvas.TextOut(x+(RightValue div 2-(Printer.Canvas.TextWidth(str))),y,str);
// printer.Canvas.TextOut((x+RightValue) div 2-((printer.Canvas.TextWidth(str)-8)*PointScale),y,str);
while not (AGrid.DataSource.DataSet).Eofdo
begin
//打印DBGrid中的所有列
n:=DBGrid1.DataSource.DataSet.recno;
if NewPage then
begin
hj1:=0;
x:=Round(LeftBlank*Pointx);
y:=PointY*TopBlank;
Printer.Canvas.TextOut(x+(RightValue div 2-(Printer.Canvas.TextWidth(str))),y,str);
x:=Round(LeftBlank*Pointx);
Y:=Y+PrintStep;
printer.Canvas.TextOut(x+10*PointScale,y,'打印日期:'+DateToStr(DM.QTjcxrq.Value));
Y:=y+PrintStep-10;
end else
if NewGrid then
begin
hj1:=0;
// hj2:=0;
// hj3:=0;
x:=Round(LeftBlank*Pointx);
y:=y+TopBlank+PrintStep*2;
Printer.Canvas.TextOut(x+(RightValue div 2-(Printer.Canvas.TextWidth(str))),y,str);
// printer.Canvas.TextOut((x+DBGrid1.Width*PointX) div 2-((printer.Canvas.TextWidth(s)))+8,y,s);
x:=Round(LeftBlank*Pointx);
Y:=y+PrintStep;
printer.Canvas.TextOut(x+10*PointScale,y,'打印日期:'+DateToStr(DM.QTjcxrq.Value));
Y:=y+PrintStep-10;
end;
x:=Round(LeftBlank*Pointx);
if NewGrid then
begin
for i:=0 to DBGrid1.FieldCount-1do
begin
//假如所要打印的列超出了打印范围,则忽略该列
if (x+AGrid.Columns.Items.Width*PointScale)<=(Printer.PageWidth-PointX*RightBlank) then
begin
//画表格线
//每页的第一行打印表头
Printer.Canvas.Rectangle(x,y,x+AGrid.Columns.Items.Width*PointScale,y+PrintStep);
Printer.Canvas.TextOut(x+8*PointScale,y+8,AGrid.Columns.Title.Caption)
end;
//计算下一列的横坐标
x:=x+DBGrid1.Columns.Items.Width*PointScale;
end;
y:=y+PrintStep;
end;
x:=Round(LeftBlank*Pointx);
for i:=0 to DBGrid1.FieldCount-1do
begin
//假如所要打印的列超出了打印范围,则忽略该列
if (x+AGrid.Columns.Items.Width*PointScale)<=(Printer.PageWidth-PointX*RightBlank) then
begin
//画表格线
//每页的第一行打印表头
Printer.Canvas.Rectangle(x,y,x+AGrid.Columns.Items.Width*PointScale,y+PrintStep);
if DBGrid1.Fields.DataType=ftDateTime then
printer.Canvas.TextOut(x+8*PointScale,y+8,Copy(AGrid.Fields.asString,1,10))
else
Printer.Canvas.TextOut(x+8*PointScale,y+8,AGrid.Fields.asString);
end;
//计算下一列的横坐标
x:=x+DBGrid1.Columns.Items.Width*PointScale;
end;
x:=Round(LeftBlank*Pointx);
if ((n mod GridNum) = 1)and(n>1) then
begin
if (Y>=printer.PageHeight-TopBlank*2-PrintStep*(GridNum+3)) then
begin
NewGrid:=True;
NewPage:=True;
end else
begin
NewPage:=False;
NewGrid:=True;
end
end else
begin
NewPage:=False;
NewGrid:=False;
end;
hj1:=hj1+DBGrid1.Fields[8].Value;
// hj2:=hj2+DBGrid1.Fields[7].Value;
// hj3:=hj3+DBGrid1.Fields[8].Value;
if NewGrid then
begin
y:=y+PrintStep;
for j:=0 to DBGrid1.FieldCount-1do
begin
printer.Canvas.Rectangle(x,y,x+DBGrid1.Columns.Items[j].Width*PointScale,y+PrintStep);
x:=x+DBGrid1.Columns.Items[j].Width*PointScale;
end;
x:=Round(LeftBlank*Pointx);
Printer.Canvas.TextOut(x+8*PointScale,y+8,'合 计');
x:=x+DBGrid1.Columns.Items[0].Width*PointScale;
x:=x+DBGrid1.Columns.Items[1].Width*PointScale;
x:=x+DBGrid1.Columns.Items[2].Width*PointScale;
x:=x+DBGrid1.Columns.Items[3].Width*PointScale;
x:=x+DBGrid1.Columns.Items[4].Width*PointScale;
x:=x+DBGrid1.Columns.Items[5].Width*PointScale;
x:=x+DBGrid1.Columns.Items[6].Width*PointScale;
x:=x+DBGrid1.Columns.Items[7].Width*PointScale;
s:=FloatToStr(hj1);
if Pos('.',s)<=0 then
s:=s+'.00';
Printer.Canvas.TextOut(x+8*PointScale,y+8,s);
// Printer.Canvas.TextOut(x+8*PointScale,y+8,FloatToStr(hj2));
// Printer.Canvas.TextOut(x+8*PointScale,y+8,FloatToStr(hj3));
x:=Round(LeftBlank*Pointx);
y:=y+PrintStep+8;
printer.Canvas.TextOut(x+10*PointScale,y,'经理: 实物负责人: 制表:');
// printer.Canvas.TextOut(x+RightValue-80*PointScale,y,'第'+IntToStr(PageNum)+'页');
// pageNum:=pageNum+1;
y:=y+printStep;
//换页
end else
begin
y:=y+PrintStep;
end;
(DBGrid1.DataSource.DataSet).next;
end;
if (not NewPage)and(not NewGrid) then
begin
m:=n mod (GridNum-1);
if m>0 then
begin
for i:=1 to GridNum-m-1do
begin
x:=Round(LeftBlank*Pointx);
for j:=0 to DBGrid1.FieldCount-1do
begin
printer.Canvas.Rectangle(x,y,x+DBGrid1.Columns.Items[j].Width*PointScale,y+PrintStep);
x:=x+DBGrid1.Columns.Items[j].Width*PointScale;
end;
y:=y+PrintStep;
x:=Round(LeftBlank*Pointx);
end;
end else
begin
x:=Round(LeftBlank*Pointx);
for j:=0 to DBGrid1.FieldCount-1do
begin
printer.Canvas.Rectangle(x,y,x+DBGrid1.Columns.Items[j].Width*PointScale,y+PrintStep);
x:=x+DBGrid1.Columns.Items[j].Width*PointScale;
end;
y:=y+PrintStep;
x:=Round(LeftBlank*Pointx);
end;
y:=y-PrintStep;
printer.Canvas.TextOut(x+8*PointScale,y+8,'合 计');
x:=x+DBGrid1.Columns.Items[0].Width*PointScale;
x:=x+DBGrid1.Columns.Items[1].Width*PointScale;
x:=x+DBGrid1.Columns.Items[2].Width*PointScale;
x:=x+DBGrid1.Columns.Items[3].Width*PointScale;
x:=x+DBGrid1.Columns.Items[4].Width*PointScale;
// printer.Canvas.TextOut(x+8*PointScale,y+8,FloatToStr(hj1));
x:=x+DBGrid1.Columns.Items[5].Width*PointScale;
x:=x+DBGrid1.Columns.Items[6].Width*PointScale;
x:=x+DBGrid1.Columns.Items[7].Width*PointScale;
s:=FloatToStr(hj1);
if Pos('.',s)<=0 then
s:=s+'.00';
Printer.Canvas.TextOut(x+8*PointScale,y+8,s);
// printer.Canvas.TextOut(x+8*PointScale,y+8,FloatToStr(hj2));
// printer.Canvas.TextOut(x+8*PointScale,y+8,FloatToStr(hj3));
y:=y+PrintStep;
(DBGrid1.DataSource.DataSet).First;
y:=y+6;
x:=Round(LeftBlank*Pointx);
printer.Canvas.TextOut(x+10*PointScale,y,'经理: 实物负责人: 制表:');
end;
finally
printer.EndDoc;
end;
(AGrid.DataSource.DataSet).First;
Application.MessageBox('打印完成','打印',32);
end;
end;