哈,套打我整过。
下面的程序满足你的要求,并且测试通过。试试。
//各位,谁能写出自定义纸张220MMX140MM
//将数据打印到长度为130MM位置的字符(test)
//pinter.canvas(0,130mm,'test');
//使用EPSON LQ1600类型的打印机,我就加分。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Printers;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
RateX, RateY: Integer;
//初始化打印机的一些参数
function InitPrinter: Boolean;
begin
Result := Printer.Printers.Count > 0;
RateX := MulDiv(GetDeviceCaps(Printer.Handle, LOGPIXELSX), 1000, 254);
RateY := MulDiv(GetDeviceCaps(Printer.Handle, LOGPIXELSY), 1000, 254);
end;
//设置纸张大小,x为纸张,y宽为纸张高,均以0.1mm为单位
procedure SetPaperSize(X, Y: Integer);
var
Device, Driver, Port: PChar;
HDMode: THandle;
PDMode: PDevMode;
begin
Device := StrAlloc(cchDeviceName);
Driver := StrAlloc(MAX_PATH);
Port := StrAlloc(32);
try
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device, Driver, Port, HDMode);
if HDMode <> 0 then
begin
PDMode := GlobalLock(HDMode);
if PDMode <> nil then
begin
with PDMode^do
begin
dmPaperSize := 256;
dmPaperWidth := X;
dmPaperLength := Y;
dmFields := dmFields or DM_PAPERSIZE or DM_PAPERWIDTH or DM_PAPERLENGTH;
GlobalUnlock(HDMode);
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;
end;
finally
StrDispose(Device);
StrDispose(Driver);
StrDispose(Port);
end;
end;
//转换x方向值,由0.1mm单位转换成Pixel单位
function TransformX(X: Integer): Integer;
begin
Result := MulDiv(X, RateX, 1000);
end;
//转换y方向值勤,由0.1mm单位转换成Pixel单位
function TransformY(Y: Integer): Integer;
begin
Result := MulDiv(Y, RateY, 1000);
end;
//转换点的座标系,由0.1mm单位转换成Pixel单位
procedure TransformPoint(Point: TPoint);
begin
Point.x := TransformX(Point.x);
Point.y := TransformY(Point.y);
end;
//转换区域的座标系,由0.1mm单位转换成Pixel单位
procedure TransformRect(Rect: TRect);
begin
Rect.Left := TransformX(Rect.Left);
Rect.Right := TransformX(Rect.Right);
Rect.Top := TransformY(Rect.Top);
Rect.Bottom := TransformY(Rect.Bottom);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
x, y: Integer;
begin
if not InitPrinter then
Application.MessageBox('', '', mb_IconError + mb_OK)
else
begin
//定义纸张为220MMX140MM
SetPaperSize(2200, 1400);
//打印文本test
with Printerdo
begin
begin
Doc;
x := 1000;
//0.1mm
y := 1300;
//0.1mm
Canvas.Font.Assign(Form1.Font);
Canvas.TextOut(TransformX(x), TransformY
, 'test');
EndDoc;
end;
end;
end;
end.