给个我刚写的代码,打印机上调试通过,可以精确定位到毫米。
用offsetX,offsetY调整打印偏移量,单位为毫米。
procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Lines.Clear;
if PrinterSetupDialog1.Execute then
begin
prtX:=GetDeviceCaps(printer.Handle,LOGPIXELSX);
memo1.Lines.Add('打印机水平分辨率LOGPIXELSX='+inttostr(prtX));
prtY:=GetDeviceCaps(printer.Handle,LOGPIXELSY);
memo1.Lines.Add('打印机垂直分辨率LOGPIXELSY='+inttostr(prtY));
paperW:=GetDeviceCaps(printer.Handle,PHYSICALWIDTH);
memo1.Lines.Add('打印纸的实际宽度PHYSICALWIDTH='+inttostr(paperW)+' ('+inttostr(round(paperW/prtX))+')inch');
paperH:=GetDeviceCaps(printer.Handle,PHYSICALHEIGHT);
memo1.Lines.Add('打印纸的实际高度PHYSICALHEIGHT='+inttostr(paperH)+' ('+inttostr(round(paperH/prtY))+')inch');
prtLeft:=GetDeviceCaps(printer.Handle,PHYSICALOFFSETX);
memo1.Lines.Add('实际可打印区域的物理左边距PHYSICALOFFSETX='+inttostr(prtLeft));
prtTop:=GetDeviceCaps(printer.Handle,PHYSICALOFFSETY);
memo1.Lines.Add('实际可打印区域的物理上边距PHYSICALOFFSETY='+inttostr(prtTop));
memo1.Lines.Add('Printer.PageWidth='+inttostr(Printer.PageWidth));
memo1.Lines.Add('Printer.PageHeight='+inttostr(Printer.PageHeight));
end;
inifile:=Tinifile.Create(ExtractFilePath(Application.ExeName)+'config.ini');
OX:=inifile.ReadInteger('print','offsetX',0);
OY:=inifile.ReadInteger('print','offsetY',0);
memo1.Lines.Add('offsetX='+inttostr(OX));
memo1.Lines.Add('offsetY='+inttostr(OY));
end;