Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14490224
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 20:48:42

下载本文示例代码
  一、概述  在银行,税务,邮政等行业的实际工作中,经常涉及到在印刷好具有固定格式的汇款单,储蓄凭证,税票等单据上的确定位置打印输出相关的信息。在此类需求中,精确地定位单据并打印相关信息,是解决问题]的关键。一般情况下,开发者都是通过在打印机上通过重复的测试来达到实际需求。那么,有没有简单有效而又灵活的方法实现上述功能呢?  二、基本思路  分析上述单据的特征,可以发现:此类打印输出的信息一般比较简短,不涉及到文字过长的折行处理,另外,其打印输出的位置相对固定。因此,我们可以通过用尺子以毫米为单位,测量好每个输出信息位置的横向和纵向坐标,作为信息输出的位置。但由于不同打印机在实际输出效果上,总是存在理论和实际位置的偏差,因此,要求程序具有一定的灵活性,供最终用户根据需要,进行必要的位置调整。因此,可设置一打印配置文件,用于存储横坐标和纵坐标的偏移量,用于用户进行位置校正,从而提供了一定的灵活性。  三、精确打印输出的程序实现  1. 在Delphi中新建一个名为mprint.pas的单元文件并编写如下程序,单元引用中加入Printers略: //取得字符的高度function CharHeight: Word;var Metrics: TTextMetric;begin GetTextMetrics(Printer.Canvas.Handle, Metrics); Result := Metrics.tmHeight;end;file://取得字符的平均宽度function AvgCharWidth: Word;var Metrics: TTextMetric;begin GetTextMetrics(Printer.Canvas.Handle, Metrics); Result := Metrics.tmAveCharWidth;end;file://取得纸张的物理尺寸---单位:点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://设置纸张高度-单位:mmprocedure 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;beginfile://自定义纸张最小高度127mmif Value < 127 then Value := 127; file://自定义纸张最大高度432mm if Value > 432 then Value := 432;  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://设置纸张宽度:单位--mmProcedure 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;beginfile://自定义纸张最小宽度76mmif Value < 76 then Value := 76; file://自定义纸张最大宽度216mm if Value > 216 then Value := 216;  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;beginfile://打开配置文件,读出横向和纵向偏移量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']); endelsebegin 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;  2. 使用举例  在主窗体中加入对mprint单元的引用,在一命令钮的OnClick事件中书写如下代码(用于在邮政汇款单上的相应方框内打印邮政编码843300): Printer.BeginDoc;PrintText(16, 14, '8', 'config.txt');PrintText(26, 14, '4', 'config.txt');PrintText(36, 14, '3', 'config.txt');PrintText(46, 14, '3', 'config.txt');PrintText(56, 14, '0', 'config.txt');PrintText(66, 14, '0', 'config.txt');Printer.EndDoc;  观察结果,用尺子测量偏移量,在config.txt文件中修改X,Y的值即可。  其它,设置打印机和纸张类型从略。  四、结束语  笔者通过该方法,实现了邮政汇款单,储蓄凭证,客户信封等单据的精确打印,取得了较为满意的效果。该程序在Windows98,Delphi5下调试通过。   一、概述  在银行,税务,邮政等行业的实际工作中,经常涉及到在印刷好具有固定格式的汇款单,储蓄凭证,税票等单据上的确定位置打印输出相关的信息。在此类需求中,精确地定位单据并打印相关信息,是解决问题]的关键。一般情况下,开发者都是通过在打印机上通过重复的测试来达到实际需求。那么,有没有简单有效而又灵活的方法实现上述功能呢?  二、基本思路  分析上述单据的特征,可以发现:此类打印输出的信息一般比较简短,不涉及到文字过长的折行处理,另外,其打印输出的位置相对固定。因此,我们可以通过用尺子以毫米为单位,测量好每个输出信息位置的横向和纵向坐标,作为信息输出的位置。但由于不同打印机在实际输出效果上,总是存在理论和实际位置的偏差,因此,要求程序具有一定的灵活性,供最终用户根据需要,进行必要的位置调整。因此,可设置一打印配置文件,用于存储横坐标和纵坐标的偏移量,用于用户进行位置校正,从而提供了一定的灵活性。  三、精确打印输出的程序实现  1. 在Delphi中新建一个名为mprint.pas的单元文件并编写如下程序,单元引用中加入Printers略: //取得字符的高度function CharHeight: Word;var Metrics: TTextMetric;begin GetTextMetrics(Printer.Canvas.Handle, Metrics); Result := Metrics.tmHeight;end;file://取得字符的平均宽度function AvgCharWidth: Word;var Metrics: TTextMetric;begin GetTextMetrics(Printer.Canvas.Handle, Metrics); Result := Metrics.tmAveCharWidth;end;file://取得纸张的物理尺寸---单位:点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://设置纸张高度-单位:mmprocedure 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;beginfile://自定义纸张最小高度127mmif Value < 127 then Value := 127; file://自定义纸张最大高度432mm if Value > 432 then Value := 432;  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://设置纸张宽度:单位--mmProcedure 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;beginfile://自定义纸张最小宽度76mmif Value < 76 then Value := 76; file://自定义纸张最大宽度216mm if Value > 216 then Value := 216;  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;beginfile://打开配置文件,读出横向和纵向偏移量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']); endelsebegin 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;  2. 使用举例  在主窗体中加入对mprint单元的引用,在一命令钮的OnClick事件中书写如下代码(用于在邮政汇款单上的相应方框内打印邮政编码843300): Printer.BeginDoc;PrintText(16, 14, '8', 'config.txt');PrintText(26, 14, '4', 'config.txt');PrintText(36, 14, '3', 'config.txt');PrintText(46, 14, '3', 'config.txt');PrintText(56, 14, '0', 'config.txt');PrintText(66, 14, '0', 'config.txt');Printer.EndDoc;  观察结果,用尺子测量偏移量,在config.txt文件中修改X,Y的值即可。  其它,设置打印机和纸张类型从略。  四、结束语  笔者通过该方法,实现了邮政汇款单,储蓄凭证,客户信封等单据的精确打印,取得了较为满意的效果。该程序在Windows98,Delphi5下调试通过。 下载本文示例代码


Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印Delphi中票据凭证的精确打印
阅读(185) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~