给你一个函数。
//打印函数
function TfrmInfo.PrintDblist(Db:Tdbgrid;Qprint:TAdoquery):boolean;
var
PointX,PointY:integer;
PointScale,PrintStep:integer;
s:string;
x,y:integer;
i:integer;
LeftBlank,RightBlank, TopBlank,BottomBlank:integer;
begin
//获取当前打印机的分辨率
{ fontname:='宋体';
fontsize:=10;
}
LeftBlank:=1;
//定义页边距,单位厘米
RightBlank:=1;
TopBlank:=1;
BottomBlank:=1;
result:=False;
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:=Fontname;
//printer.canvas.Font.Size:=Fontsize;
//根据字体的大小确定每行的高度
S:='计算机';
PrintStep:=printer.canvas.TextHeight(s)+16;
//打印的起点位置
x:=PointX*LeftBlank;
y:=PointY*TopBlank;
//Qprint是DBGrid1所连接的数据源
if (Qprint.Active=true) and (Qprint.RecordCount>0) then
begin
printer.begin
Doc;
Qprint.First;
while not Qprint.Eofdo
begin
//打印DBGrid中的所有列
for i:=0 to DB.FieldCount-1do
begin
if (x+DB.Columns.Items.Width*PointScale)<=(Printer.PageWidth-PointX*RightBlank) then
begin
//画表格线
//每页的第一行打印表头
Printer.Canvas.Rectangle(x,y,x+DB.Columns.Items.Width*PointScale,y+PrintStep);
if y=PointY*TopBlank then
Printer.Canvas.TextOut(x+8,y+8,DB.Columns.Title.Caption)
else
Printer.Canvas.TextOut(x+8,y+8,DB.Fields.asString);
//计算下一需要打印列的横坐标
end;
x:=x+DB.Columns.Items.Width*PointScale;
end;
if not (y=PointY*TopBlank) then
Qprint.next;
x:=PointX*LeftBlank;
y:=y+PrintStep;
//换页
if (y+PrintStep)>(Printer.PageHeight-PointY*BottomBlank) then
begin
Printer.NewPage;
y:=PointY*TopBlank;
end;
end;
//begin
2
printer.EndDoc;
Qprint.First;
// Application.MessageBox(,32);
end;
//begin
1
end;