L
liberpike
Unregistered / Unconfirmed
GUEST, unregistred user!
还是票据的打印的问题,在大富翁商业搜索了很多帖子。还发现DJPrint这个控件(源码见下面),但是我用这个控件打印出来的位置还是不对!总是有偏差,哪位大富翁知道问题所在?请赐教!
unit DJPrint;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
printers;
type
TAlignment = (alLeft,alRight,alCenter);
TDJPrint = class
private
{ Private declarations }
FPaperHeight:integer;
FPaperWidth :integer;
protected
{ Protected declarations }
public
{ Public declarations }
WordSpace :integer;//字距
LineSPace :integer;//行距
MaxLength :integer;//行宽
WordWrap :boolean;//是否折行
Alignment :TAlignment;//对齐方式
constructor create(Aowner:TComponent);override;
destructor destroy;override;
Function GetPaperWidth:integer;//纸的实际宽度,单位:0.1毫米
Function GetPaperHeight:integer;//纸的实际高度,单位:0.1毫米
Function GetWidth:integer;// 纸的实际宽度,单位oint
Function GetHeight:integer;
//纸的实际长度,单位oint
Function GetOffSetX:integer;
Function GetOffSetY:integer;
Function PToPX(value:integer):integer;//将X实际尺寸转化为点距(单位:0.1毫米)
Function PToPY(value:integer):integer;//将Y实际尺寸转化为点距(单位:0.1毫米)
FUnction GetPixelsX:integer;
//取得水平方向分辨率
Function GetPixelsY:integer;
procedure PrintText(x,y:integer;Text:string);
procedure ExtPrint(x,y:integer;Text:string);//WordSpace:字距
procedure MoveTo(x,y:integer);
procedure LineTo(x,y:integer);
procedure Rectangle(X1, Y1, X2, Y2: Integer);
Procedure SetPageWidth(value:integer);
//单位:毫米
procedure SetPaperHeight(Value:integer);
//单位:毫米
Function strLength(s:string):integer;//串的打印长度,单位:0.1毫米
Function strheight(s:string):integer;//串的打印长度,单位:0.1毫米
published
{ Published declrations }
property PaperWidth : integer Read FPaperWidth write SetPageWidth;
property PaperHeight: integer Read FPaperHeight write SetPaperHeight;
end;
procedure Register;
implementation
constructor TDJPrint.create();
begin
inherited create(Aowner);
FOwner:=AOwner;
WordSpace:=0;LineSpace:=0;
MaxLength:=0;WordWrap:=False;
alignment:=alLeft;
end;
destructor TDJPrint.destroy;
begin
inherited destroy;
end;
Function TDJPrint.GetPaperWidth:integer;
begin
result:=trunc(getWidth/getPixelsX*254);
end;
Function TDJPrint.GetPaperHeight:integer;
begin
result:=Trunc(GetHEIGHT/getPixelsY*254);
end;
Function TDJPrint.GetWidth:integer;
begin
result:=GetDeviceCaps(printer.Handle,PHYSICALWIDTH);
end;
Function TDJPrint.GetHeight:integer;
begin
result:=GetDeviceCaps(printer.Handle,PHYSICALHEIGHT);
end;
Function TDJPrint.GetOffSetX:integer;
begin
result:=GetDeviceCaps(Printer.handle,PHYSICALOFFSETX);
end;
Function TDJPrint.GetOffSetY:integer;
begin
result:=GetDeviceCaps(Printer.handle,PHYSICALOFFSETY);
end;
Function TDJPrint.GetPixelsX:integer;
begin
Result:=GetDeviceCaps(Printer.handle, LOGPIXELSX);
end;
Function TDJPrint.GetPixelsY:integer;
begin
Result:=GetDeviceCaps(Printer.handle, LOGPIXELSY);
end;
Function TDJPrint.PToPX(value:integer):integer;//将X实际尺寸转化为点距(单位:0.1毫米)
begin
if Value=0 then
Result:=0
else
result:=Trunc(Value/254*GetPixelsX-getOffsetX);
end;
Function TDJPrint.PToPY(value:integer):integer;//将Y实际尺寸转化为点距(单位:0.1毫米)
begin
if Value=0 then
Result:=0
else
result:=Trunc(Value/254*GetPixelsY-getOffsetY)
end;
procedure TDJPrint.MoveTo(x,y:integer);
begin
x:=pTopX(X);y:=pTOpY(Y);
printer.canvas.MoveTo(x,y);
end;
procedure TDJPrint.LineTo(x,y:integer);
begin
x:=pTopX(X);y:=pTOpY(Y);
printer.canvas.LineTo(x,y);
end;
procedure TDJPrint.Rectangle(X1, Y1, X2, Y2: Integer);
begin
x1:=pTopX(X1);y1:=pTOpY(Y1);
x2:=pTopX(X2);y2:=pTOpY(Y2);
printer.canvas.Rectangle(X1, Y1, X2, Y2);
end;
Function TDJPrint.strLength(s:string):integer;//串的打印长度,单位:0.1毫米
begin
result:=trunc(printer.Canvas.TextWidth(s)/GetPixelsX*254);
end;
Function TDJPrint.strHeight(s:string):integer;//串的打印长度,单位:0.1毫米
begin
result:=trunc(printer.Canvas.TextHeight(s)/GetPixelsY*254);
end;
procedure TDJPrint.PrintText(x,y:integer;Text:string);//x,y单位:0.1毫米
var i,itemp,newx,NewY:integer;
begin
NewX:=pTopX(X);Newy:=pTopY(Y);
if WordSpace=0 then
begin
if maxLength>0 then
case alignment of
alright : NewX:=NewX+trunc(MaxLength/254*getPixelsX)-printer.Canvas.TextWidth(Text);
alcenter: NewX:=NewX+trunc((MaxLength/254*getPixelsX-printer.Canvas.TextWidth(Text))/2);
end;
printer.canvas.textout(NewX,NewY,text)
end
else
begin
itemp:=0;
if maxLength>0 then
case alignment of
alright : NewX:=NewX+trunc(MaxLength/254*getPixelsX-printer.Canvas.TextWidth(Text)
-(length(text)-1)*WordSpace/254*getPixelsX);
alcenter: NewX:=NewX+trunc((MaxLength/254*getPixelsX-printer.Canvas.TextWidth(Text)-(length(text)-1)*WordSpace/254*getPixelsX)/2);
end;
for i:=1 to length(Text)do
begin
if i>1 then
begin
itemp:=itemp+WordSpace+strLength(text[i-1]);
end;
printer.canvas.TextOut(NewX+trunc(itemp/254*getPixelsX),NewY,text);
end;
end;
end;
procedure TDJPrint.ExtPrint(x,y:integer;Text:string);//x,y单位:0.1毫米
var
i,ij,LineNum,iTemp,HanziNum:integer;
szTemp:string;
begin
itemp:=0;
if not wordWrap then
PrintText(x,y,Text)
else
begin
if maxlength=0 then
begin
printText(x,Y,text);exit;
end;
i:=0;LineNum:=0;ij:=1;
HanziNum:=0;
repeat
i:=i+1;
szTemp:=copy(text,ij,i-ij+1);
itemp:=itemp+strLength(text);
if text>#127 then
HanziNum:=HanziNum+1
else
HanziNum:=0;
if ((iTemp+(i-ij)*WordSpace)>maxLength) or (i=length(Text)) then
begin
if i=length(text) then
begin
szTemp:=copy(text,ij,i-ij+1);i:=i+1;
end
else
if odd(HanziNum) then
begin
szTemp:=copy(text,ij,i-ij-1);i:=i-2;
end
else
begin
szTemp:=Copy(Text,ij,i-ij);i:=i-1;
end;
ij:=i;i:=i-1;LineNum:=LineNum+1;
if LineNum>1 then
y:=Y+LineSpace+strHeight(szTemp);
printText(x,y,szTemp);
itemp:=0;HanziNum:=0;
end;
until i>=length(Text);
end;
end;
Procedure TDJPrint.SetPageWidth(Value:integer);
var
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port : array[0..255] of char;
hDMode : THandle;
PDMode : PDEVMODE;
begin
if Value<>FPaperWidth then
begin
FPaperWidth:=Value;
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device, Driver, Port, hDMode);
if hDMode <> 0 then
begin
pDMode := GlobalLock(hDMode);
if pDMode <> nil then
begin
pDMode^.dmFields := pDMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERWIDTH;
pDMode^.dmPaperSize := DMPAPER_USER;
pDMode^.dmPaperWidth := Value {SomeValueInTenthsOfAMillimeter};
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
GlobalUnlock(hDMode);
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;
end;
procedure TDJPrint.SetPaperHeight(Value:integer);
var
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port : array[0..255] of char;
hDMode : THandle;
PDMode : PDEVMODE;
begin
if Value<>FPaperHeight then
begin
FPaperHeight:=Value;
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device, Driver, Port, hDMode);
if hDMode <> 0 then
begin
pDMode := GlobalLock(hDMode);
if pDMode <> nil then
begin
pDMode^.dmFields := pDMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERLENGTH;
pDMode^.dmPaperSize := DMPAPER_USER;
pDMode^.dmPaperLength := Value {SomeValueInTenthsOfAMillimeter};
{Set the bin to use}
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
GlobalUnlock(hDMode);
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;
end;
end.
unit DJPrint;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
printers;
type
TAlignment = (alLeft,alRight,alCenter);
TDJPrint = class
private
{ Private declarations }
FPaperHeight:integer;
FPaperWidth :integer;
protected
{ Protected declarations }
public
{ Public declarations }
WordSpace :integer;//字距
LineSPace :integer;//行距
MaxLength :integer;//行宽
WordWrap :boolean;//是否折行
Alignment :TAlignment;//对齐方式
constructor create(Aowner:TComponent);override;
destructor destroy;override;
Function GetPaperWidth:integer;//纸的实际宽度,单位:0.1毫米
Function GetPaperHeight:integer;//纸的实际高度,单位:0.1毫米
Function GetWidth:integer;// 纸的实际宽度,单位oint
Function GetHeight:integer;
//纸的实际长度,单位oint
Function GetOffSetX:integer;
Function GetOffSetY:integer;
Function PToPX(value:integer):integer;//将X实际尺寸转化为点距(单位:0.1毫米)
Function PToPY(value:integer):integer;//将Y实际尺寸转化为点距(单位:0.1毫米)
FUnction GetPixelsX:integer;
//取得水平方向分辨率
Function GetPixelsY:integer;
procedure PrintText(x,y:integer;Text:string);
procedure ExtPrint(x,y:integer;Text:string);//WordSpace:字距
procedure MoveTo(x,y:integer);
procedure LineTo(x,y:integer);
procedure Rectangle(X1, Y1, X2, Y2: Integer);
Procedure SetPageWidth(value:integer);
//单位:毫米
procedure SetPaperHeight(Value:integer);
//单位:毫米
Function strLength(s:string):integer;//串的打印长度,单位:0.1毫米
Function strheight(s:string):integer;//串的打印长度,单位:0.1毫米
published
{ Published declrations }
property PaperWidth : integer Read FPaperWidth write SetPageWidth;
property PaperHeight: integer Read FPaperHeight write SetPaperHeight;
end;
procedure Register;
implementation
constructor TDJPrint.create();
begin
inherited create(Aowner);
FOwner:=AOwner;
WordSpace:=0;LineSpace:=0;
MaxLength:=0;WordWrap:=False;
alignment:=alLeft;
end;
destructor TDJPrint.destroy;
begin
inherited destroy;
end;
Function TDJPrint.GetPaperWidth:integer;
begin
result:=trunc(getWidth/getPixelsX*254);
end;
Function TDJPrint.GetPaperHeight:integer;
begin
result:=Trunc(GetHEIGHT/getPixelsY*254);
end;
Function TDJPrint.GetWidth:integer;
begin
result:=GetDeviceCaps(printer.Handle,PHYSICALWIDTH);
end;
Function TDJPrint.GetHeight:integer;
begin
result:=GetDeviceCaps(printer.Handle,PHYSICALHEIGHT);
end;
Function TDJPrint.GetOffSetX:integer;
begin
result:=GetDeviceCaps(Printer.handle,PHYSICALOFFSETX);
end;
Function TDJPrint.GetOffSetY:integer;
begin
result:=GetDeviceCaps(Printer.handle,PHYSICALOFFSETY);
end;
Function TDJPrint.GetPixelsX:integer;
begin
Result:=GetDeviceCaps(Printer.handle, LOGPIXELSX);
end;
Function TDJPrint.GetPixelsY:integer;
begin
Result:=GetDeviceCaps(Printer.handle, LOGPIXELSY);
end;
Function TDJPrint.PToPX(value:integer):integer;//将X实际尺寸转化为点距(单位:0.1毫米)
begin
if Value=0 then
Result:=0
else
result:=Trunc(Value/254*GetPixelsX-getOffsetX);
end;
Function TDJPrint.PToPY(value:integer):integer;//将Y实际尺寸转化为点距(单位:0.1毫米)
begin
if Value=0 then
Result:=0
else
result:=Trunc(Value/254*GetPixelsY-getOffsetY)
end;
procedure TDJPrint.MoveTo(x,y:integer);
begin
x:=pTopX(X);y:=pTOpY(Y);
printer.canvas.MoveTo(x,y);
end;
procedure TDJPrint.LineTo(x,y:integer);
begin
x:=pTopX(X);y:=pTOpY(Y);
printer.canvas.LineTo(x,y);
end;
procedure TDJPrint.Rectangle(X1, Y1, X2, Y2: Integer);
begin
x1:=pTopX(X1);y1:=pTOpY(Y1);
x2:=pTopX(X2);y2:=pTOpY(Y2);
printer.canvas.Rectangle(X1, Y1, X2, Y2);
end;
Function TDJPrint.strLength(s:string):integer;//串的打印长度,单位:0.1毫米
begin
result:=trunc(printer.Canvas.TextWidth(s)/GetPixelsX*254);
end;
Function TDJPrint.strHeight(s:string):integer;//串的打印长度,单位:0.1毫米
begin
result:=trunc(printer.Canvas.TextHeight(s)/GetPixelsY*254);
end;
procedure TDJPrint.PrintText(x,y:integer;Text:string);//x,y单位:0.1毫米
var i,itemp,newx,NewY:integer;
begin
NewX:=pTopX(X);Newy:=pTopY(Y);
if WordSpace=0 then
begin
if maxLength>0 then
case alignment of
alright : NewX:=NewX+trunc(MaxLength/254*getPixelsX)-printer.Canvas.TextWidth(Text);
alcenter: NewX:=NewX+trunc((MaxLength/254*getPixelsX-printer.Canvas.TextWidth(Text))/2);
end;
printer.canvas.textout(NewX,NewY,text)
end
else
begin
itemp:=0;
if maxLength>0 then
case alignment of
alright : NewX:=NewX+trunc(MaxLength/254*getPixelsX-printer.Canvas.TextWidth(Text)
-(length(text)-1)*WordSpace/254*getPixelsX);
alcenter: NewX:=NewX+trunc((MaxLength/254*getPixelsX-printer.Canvas.TextWidth(Text)-(length(text)-1)*WordSpace/254*getPixelsX)/2);
end;
for i:=1 to length(Text)do
begin
if i>1 then
begin
itemp:=itemp+WordSpace+strLength(text[i-1]);
end;
printer.canvas.TextOut(NewX+trunc(itemp/254*getPixelsX),NewY,text);
end;
end;
end;
procedure TDJPrint.ExtPrint(x,y:integer;Text:string);//x,y单位:0.1毫米
var
i,ij,LineNum,iTemp,HanziNum:integer;
szTemp:string;
begin
itemp:=0;
if not wordWrap then
PrintText(x,y,Text)
else
begin
if maxlength=0 then
begin
printText(x,Y,text);exit;
end;
i:=0;LineNum:=0;ij:=1;
HanziNum:=0;
repeat
i:=i+1;
szTemp:=copy(text,ij,i-ij+1);
itemp:=itemp+strLength(text);
if text>#127 then
HanziNum:=HanziNum+1
else
HanziNum:=0;
if ((iTemp+(i-ij)*WordSpace)>maxLength) or (i=length(Text)) then
begin
if i=length(text) then
begin
szTemp:=copy(text,ij,i-ij+1);i:=i+1;
end
else
if odd(HanziNum) then
begin
szTemp:=copy(text,ij,i-ij-1);i:=i-2;
end
else
begin
szTemp:=Copy(Text,ij,i-ij);i:=i-1;
end;
ij:=i;i:=i-1;LineNum:=LineNum+1;
if LineNum>1 then
y:=Y+LineSpace+strHeight(szTemp);
printText(x,y,szTemp);
itemp:=0;HanziNum:=0;
end;
until i>=length(Text);
end;
end;
Procedure TDJPrint.SetPageWidth(Value:integer);
var
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port : array[0..255] of char;
hDMode : THandle;
PDMode : PDEVMODE;
begin
if Value<>FPaperWidth then
begin
FPaperWidth:=Value;
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device, Driver, Port, hDMode);
if hDMode <> 0 then
begin
pDMode := GlobalLock(hDMode);
if pDMode <> nil then
begin
pDMode^.dmFields := pDMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERWIDTH;
pDMode^.dmPaperSize := DMPAPER_USER;
pDMode^.dmPaperWidth := Value {SomeValueInTenthsOfAMillimeter};
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
GlobalUnlock(hDMode);
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;
end;
procedure TDJPrint.SetPaperHeight(Value:integer);
var
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port : array[0..255] of char;
hDMode : THandle;
PDMode : PDEVMODE;
begin
if Value<>FPaperHeight then
begin
FPaperHeight:=Value;
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device, Driver, Port, hDMode);
if hDMode <> 0 then
begin
pDMode := GlobalLock(hDMode);
if pDMode <> nil then
begin
pDMode^.dmFields := pDMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERLENGTH;
pDMode^.dmPaperSize := DMPAPER_USER;
pDMode^.dmPaperLength := Value {SomeValueInTenthsOfAMillimeter};
{Set the bin to use}
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
GlobalUnlock(hDMode);
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;
end;
end.