这里有一段源代码,你可以参照,是打印dbgrid的。
procedure TForm1.Button1Click(Sender: TObject);
const
leftblank=1;
rightblank=1;
topblank=1;
bottomblank=1;
var
pointx,pointy:integer;
printstep:integer;
previewscale,x,y,i:integer;
s:string;
begin
previewscale:=strtoint(edit1.Text);
pointx:=trunc(GetDeviceCaps(printer.Handle,LOGPIXELSX)/2.54);// div previewscale;
pointy:=trunc(GetDeviceCaps(printer.Handle,LOGPIXELSY)/2.54);// div previewscale;
form2:=Tform2.Create(self);
Form2.Image1.Canvas.Font.Name:='宋体';
form2.Image1.Canvas.Font.Size:=10;
s:='商品信息';
printstep:=Form2.Image1.Canvas.TextHeight(s);
x:=leftblank;
y:=topblank;
if (query1.Active=true) and (query1.RecordCount>0) then
begin
query1.first;
while not query1.eofdo
begin
for i:=0 to DBGrid1.FieldCount-1do
begin
if (x+DBGrid1.Columns.Items.Width)<=(Printer.PageWidth-pointx*rightblank) then
{pointx*rightblank得到的是什么?}
begin
form2.Image1.Canvas.Rectangle(x,y,(x+4+DBGrid1.Columns.items.Width)*pointx,pointy*(y+4+printstep));
if y=topblank then
form2.Image1.Canvas.TextOut(x+2,y+2,DBGrid1.Columns.Title.Caption)
else
form2.Image1.Canvas.TextOut(x+2,y+2,DBGrid1.Fields.AsString);
end;
x:=x+DBGrid1.Columns.Items.Width;
end;
if not (y=pointy*topblank) then
query1.Next;
x:=leftblank;
y:=y+(printstep+8);
end;
application.MessageBox('预览完成','提示',32);
end;
form2.Show;
end;