我源代码如下: unit mprint;interfaceuses Classes,DBTables,DB,DBCtrls,Graphics,Windows,Messages, SysUtils, Variants, Controls, Forms,Dialogs, Menus ,StdCtrls,Printers;//取得字符的高度 function CharHeight: Word; function AvgCharWidth: Word; function GetPhicalPaper: TPoint; function PaperLogicSize: TPoint; function HVLogincRatio: Extended; function GetOffSetX: Integer; function GetOffSetY: Integer; function MmToInch(Length: Extended): Extended; function InchToMm(Length: Extended): Extended; function HPointsPerInch: Integer; function VPointsPerInch: Integer; function XPointToMm(Pos: Integer): Extended; function YPointToMm(Pos: Integer): Extended; procedure SetPaperHeight(Value:integer); Procedure SetPaperWidth(Value:integer); procedure PrintText(X, Y: Extended; Txt: string; ConfigFileName: string; FontSize: Integer=12);implementation//取得字符的高度 function CharHeight: Word; var Metrics: TTextMetric; begin GetTextMetrics(Printer.Canvas.Handle, Metrics); Result := Metrics.tmHeight; end;//取得字符的平均宽度 function AvgCharWidth: Word; var Metrics: TTextMetric; begin GetTextMetrics(Printer.Canvas.Handle, Metrics); Result := Metrics.tmAveCharWidth; end;//取得纸张的物理尺寸---单位:点 function GetPhicalPaper: TPoint; var PageSize : TPoint; begin //file://PageSize.X; //纸张物理宽度-单位:点 //file://PageSize.Y; //纸张物理高度-单位:点 Escape(Printer.Handle, GETPHYSPAGESIZE, 0,nil,@PageSize); Result := PageSize; end;//file://2.取得纸张的逻辑宽度--可打印区域 //file://取得纸张的逻辑尺寸 function PaperLogicSize: TPoint; var APoint: TPoint; begin APoint.X := Printer.PageWidth; APoint.Y := Printer.PageHeight; Result := APoint; end;//file://纸张水平对垂直方向的纵横比例 function HVLogincRatio: Extended; var AP: TPoint; begin Ap := PaperLogicSize; Result := Ap.y/Ap.X; end;//file://取得纸张的横向偏移量-单位:点 function GetOffSetX: Integer; begin Result := GetDeviceCaps(Printer.Handle, PhysicalOffSetX); end;//file://取得纸张的纵向偏移量-单位:点 function GetOffSetY: Integer; begin Result := GetDeviceCaps(Printer.Handle, PhysicalOffSetY); end;//file://毫米单位转换为英寸单位 function MmToInch(Length: Extended): Extended; begin Result := Length/25.4; end;//file://英寸单位转换为毫米单位 function InchToMm(Length: Extended): Extended; begin Result := Length*25.4; end;//file://取得水平方向每英寸打印机的点数 function HPointsPerInch: Integer; begin Result := GetDeviceCaps(Printer.Handle, LOGPIXELSX); end;//file://取得纵向方向每英寸打印机的光栅数 function VPointsPerInch: Integer; begin Result := GetDeviceCaps(Printer.Handle, LOGPIXELSY) end;//file://横向点单位转换为毫米单位 function XPointToMm(Pos: Integer): Extended; begin Result := Pos*25.4/HPointsPerInch; end;//file://纵向点单位转换为毫米单位 function YPointToMm(Pos: Integer): Extended; begin Result := Pos*25.4/VPointsPerInch; end;//file://设置纸张高度-单位:mm procedure 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 //file://自定义纸张最小高度127mm if Value <110 then Value := 110; //file://自定义纸张最大高度432mm if Value > 115 then Value := 115; 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 * 10; pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; pDMode^.dmDefaultSource := DMBIN_MANUAL; GlobalUnlock(hDMode); end; end; Printer.PrinterIndex := Printer.PrinterIndex; end;//file://设置纸张宽度:单位--mm Procedure SetPaperWidth(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 //file://自定义纸张最小宽度76mm if Value < 85 then Value := 85; //file://自定义纸张最大宽度216mm if Value > 90 then Value := 90; 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; //file://将毫米单位转换为0.1mm单位 pDMode^.dmPaperWidth := Value * 10; pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; pDMode^.dmDefaultSource := DMBIN_MANUAL; GlobalUnlock(hDMode); end; end; Printer.PrinterIndex := Printer.PrinterIndex; end;//file://在 (Xmm, Ymm)处按指定配置文件信息和字体输出字符串 procedure PrintText(X, Y: Extended; Txt: string; ConfigFileName: string; FontSize: Integer=12); var OrX, OrY: Extended; Px, Py: Integer; AP: TPoint; Fn: TStrings; FileName: string; OffSetX, OffSetY: Integer; begin //file://打开配置文件,读出横向和纵向偏移量 try Fn := TStringList.Create; FileName := ExtractFilePath(Application.ExeName) + ConfigFileName; if FileExists(FileName) then begin Fn.LoadFromFile(FileName); //file://横向偏移量 OffSetX := StrToInt(Fn.Values['X']); //file://纵向偏移量 OffSetY := StrToInt(Fn.Values['Y']); end else begin //file://如果没有配置文件,则生成 Fn.Values['X']:='0'; Fn.Values['Y']:='0'; Fn.SaveToFile(FileName); end; finally Fn.Free; end; X := X + OffSetX; Y := Y + OffSetY; Px := Round(Round(X * HPointsPerInch * 10000/25.4) / 10000); Py := Round(Round(Y * VPointsPerInch * 10000/25.4) / 10000); Py := Py - GetOffSetY; //file://因为是绝对坐标, 因此, 不用换算成相对于Y轴坐标 Px := Px + 2 * AvgCharWidth; Printer.Canvas.Font.Name := '宋体'; Printer.Canvas.Font.Size := FontSize; //file://Printer.Canvas.Font.Color := clGreen; Printer.Canvas.TextOut(Px, Py, Txt);end; end.
unit mprint;interfaceuses
Classes,DBTables,DB,DBCtrls,Graphics,Windows,Messages, SysUtils,
Variants, Controls, Forms,Dialogs, Menus ,StdCtrls,Printers;//取得字符的高度
function CharHeight: Word;
function AvgCharWidth: Word;
function GetPhicalPaper: TPoint;
function PaperLogicSize: TPoint;
function HVLogincRatio: Extended;
function GetOffSetX: Integer;
function GetOffSetY: Integer;
function MmToInch(Length: Extended): Extended;
function InchToMm(Length: Extended): Extended;
function HPointsPerInch: Integer;
function VPointsPerInch: Integer;
function XPointToMm(Pos: Integer): Extended;
function YPointToMm(Pos: Integer): Extended;
procedure SetPaperHeight(Value:integer);
Procedure SetPaperWidth(Value:integer);
procedure PrintText(X, Y: Extended; Txt: string; ConfigFileName: string; FontSize: Integer=12);implementation//取得字符的高度
function CharHeight: Word;
var
Metrics: TTextMetric;
begin
GetTextMetrics(Printer.Canvas.Handle, Metrics);
Result := Metrics.tmHeight;
end;//取得字符的平均宽度
function AvgCharWidth: Word;
var
Metrics: TTextMetric;
begin
GetTextMetrics(Printer.Canvas.Handle, Metrics);
Result := Metrics.tmAveCharWidth;
end;//取得纸张的物理尺寸---单位:点
function GetPhicalPaper: TPoint;
var
PageSize : TPoint;
begin
//file://PageSize.X; //纸张物理宽度-单位:点
//file://PageSize.Y; //纸张物理高度-单位:点
Escape(Printer.Handle, GETPHYSPAGESIZE, 0,nil,@PageSize);
Result := PageSize;
end;//file://2.取得纸张的逻辑宽度--可打印区域
//file://取得纸张的逻辑尺寸
function PaperLogicSize: TPoint;
var
APoint: TPoint;
begin
APoint.X := Printer.PageWidth;
APoint.Y := Printer.PageHeight;
Result := APoint;
end;//file://纸张水平对垂直方向的纵横比例
function HVLogincRatio: Extended;
var
AP: TPoint;
begin
Ap := PaperLogicSize;
Result := Ap.y/Ap.X;
end;//file://取得纸张的横向偏移量-单位:点
function GetOffSetX: Integer;
begin
Result := GetDeviceCaps(Printer.Handle, PhysicalOffSetX);
end;//file://取得纸张的纵向偏移量-单位:点
function GetOffSetY: Integer;
begin
Result := GetDeviceCaps(Printer.Handle, PhysicalOffSetY);
end;//file://毫米单位转换为英寸单位
function MmToInch(Length: Extended): Extended;
begin
Result := Length/25.4;
end;//file://英寸单位转换为毫米单位
function InchToMm(Length: Extended): Extended;
begin
Result := Length*25.4;
end;//file://取得水平方向每英寸打印机的点数
function HPointsPerInch: Integer;
begin
Result := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
end;//file://取得纵向方向每英寸打印机的光栅数
function VPointsPerInch: Integer;
begin
Result := GetDeviceCaps(Printer.Handle, LOGPIXELSY)
end;//file://横向点单位转换为毫米单位
function XPointToMm(Pos: Integer): Extended;
begin
Result := Pos*25.4/HPointsPerInch;
end;//file://纵向点单位转换为毫米单位
function YPointToMm(Pos: Integer): Extended;
begin
Result := Pos*25.4/VPointsPerInch;
end;//file://设置纸张高度-单位:mm
procedure 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
//file://自定义纸张最小高度127mm
if Value <110 then Value := 110;
//file://自定义纸张最大高度432mm
if Value > 115 then Value := 115;
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 * 10;
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
GlobalUnlock(hDMode);
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;//file://设置纸张宽度:单位--mm
Procedure SetPaperWidth(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
//file://自定义纸张最小宽度76mm
if Value < 85 then Value := 85;
//file://自定义纸张最大宽度216mm
if Value > 90 then Value := 90;
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;
//file://将毫米单位转换为0.1mm单位
pDMode^.dmPaperWidth := Value * 10;
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
GlobalUnlock(hDMode);
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;//file://在 (Xmm, Ymm)处按指定配置文件信息和字体输出字符串
procedure PrintText(X, Y: Extended; Txt: string; ConfigFileName: string; FontSize: Integer=12);
var
OrX, OrY: Extended;
Px, Py: Integer;
AP: TPoint;
Fn: TStrings;
FileName: string;
OffSetX, OffSetY: Integer;
begin
//file://打开配置文件,读出横向和纵向偏移量
try
Fn := TStringList.Create;
FileName := ExtractFilePath(Application.ExeName) + ConfigFileName;
if FileExists(FileName) then
begin
Fn.LoadFromFile(FileName);
//file://横向偏移量
OffSetX := StrToInt(Fn.Values['X']);
//file://纵向偏移量
OffSetY := StrToInt(Fn.Values['Y']);
end
else
begin
//file://如果没有配置文件,则生成
Fn.Values['X']:='0';
Fn.Values['Y']:='0';
Fn.SaveToFile(FileName);
end;
finally
Fn.Free;
end;
X := X + OffSetX;
Y := Y + OffSetY;
Px := Round(Round(X * HPointsPerInch * 10000/25.4) / 10000);
Py := Round(Round(Y * VPointsPerInch * 10000/25.4) / 10000);
Py := Py - GetOffSetY;
//file://因为是绝对坐标, 因此, 不用换算成相对于Y轴坐标
Px := Px + 2 * AvgCharWidth;
Printer.Canvas.Font.Name := '宋体';
Printer.Canvas.Font.Size := FontSize;
//file://Printer.Canvas.Font.Color := clGreen;
Printer.Canvas.TextOut(Px, Py, Txt);end;
end.
从控制面板中打开"传真和打印机"的文件夹,不要点选任何打印机.
1.在这个界面上的菜单栏上,点选"文件",
新建一个纸型.大小是你要打印的纸型大小.2.在你需要进行打印的机器上,设置该打印机的打印首选项.
高级选项中的纸张输出设置为你新建立的纸型即可.3.如果你与其他机器使用的是共享打印机,
只需要在打印机所挂接的机器上设置即可.4.如果使用多台打印机的话,这样的设置就很麻烦,其实windows把我们刚才的设定已经保存在注册表里了,在这里.
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Forms]
把这一支导出来,在另外的机器上导入就行了.
不过还是需要在打印首选项里设置成默认的纸型.这个地方我没有找到注册表里存在哪里,如果你发现的话一定要告诉我啊,:)
5.通过以上的设置,就可以按照你的纸型打印了.
需要注意的是,
部分打印机可能因为驱动程序的问题,还是会有些许偏差
设定的纸型不能超过打印机的最小和最大的打印范围
hp系列高速激光打印机需要设置打印方向,其他打印机可能也会有这种需要
Epson680k 请安装670k的驱动,不然控制不住在下一节中,我将使用VB来做个演示怎么在设定好纸型后,
在程序中自动选择纸型,从而满足打印机需要打印多种不同规格票据的需求.
另:如果是在ASP/ASP.NET中的WEB应用,就到此为止了 :)