打印数据02

(***************************************************) (* 单元名: UN_TDprint *) (* 功 能: 通用的打印单元 *) (* TianYueZeng *) (******************************************************************) (* 应用:在要套打的单元里uses这个单元:UN_TDprint, *) (* Printer.BeginDoc; *) (* PrintText(16, 14, '开心', 'config.txt',12); *) (* PrintText(26, 14, '就好', 'config.txt',12); *) (* Printer.EndDoc; *) (******************************************************************) unit UN_TDprint; interface uses Printers,Windows, Messages, SysUtils, Variants, Classes, Graphics, Dialogs, Controls,forms; //取得字符的高度 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; //设置纸张高度-单位:mm procedure SetPaperHeight(Value:integer); //设置纸张宽度:单位--mm Procedure SetPaperWidth(Value:integer); //在 (Xmm, Ymm)处按指定配置文件信息和字体输出字符串 procedure PrintText(X, Y: Extended; Txt: string;FontSize: Integer=12); Procedure DrawLine(Move_X, Move_Y,Line_X,Line_Y : integer;Temp_PenStyle : TPenStyle = psSolid); 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 //PageSize.X/; 纸张物理宽度-单位:点 //PageSize.Y/; 纸张物理高度-单位:点 Escape(Printer.Handle, GETPHYSPAGESIZE, 0,nil,@PageSize); Result := PageSize; end; //取得纸张的逻辑宽度--可打印区域,取得纸张的逻辑尺寸 function PaperLogicSize: TPoint; var APoint: TPoint; begin APoint.X := Printer.PageWidth; APoint.Y := Printer.PageHeight; Result := APoint; end; //纸张水平对垂直方向的纵横比例 function HVLogincRatio: Extended; var AP: TPoint; begin Ap := PaperLogicSize; Result := Ap.y/Ap.X; end; //取得纸张的横向偏移量-单位:点 function GetOffSetX: Integer; begin Result := GetDeviceCaps(Printer.Handle, PhysicalOffSetX); end; //取得纸张的纵向偏移量-单位:点 function GetOffSetY: Integer; begin Result := GetDeviceCaps(Printer.Handle, PhysicalOffSetY); end; //毫米单位转换为英寸单位 function MmToInch(Length: Extended): Extended; begin Result := Length/25.4; end; //英寸单位转换为毫米单位 function InchToMm(Length: Extended): Extended; begin Result := Length*25.4; end; //取得水平方向每英寸打印机的点数 function HPointsPerInch: Integer; begin Result := GetDeviceCaps(Printer.Handle, LOGPIXELSX); end; //取得纵向方向每英寸打印机的光栅数 function VPointsPerInch: Integer; begin Result := GetDeviceCaps(Printer.Handle, LOGPIXELSY) end; //横向点单位转换为毫米单位 function XPointToMm(Pos: Integer): Extended; begin Result := Pos*25.4/HPointsPerInch; end; //纵向点单位转换为毫米单位 function YPointToMm(Pos: Integer): Extended; begin Result := Pos*25.4/VPointsPerInch; end; //设置纸张高度-单位: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 //自定义纸张最小高度127mm if Value < 127 then Value := 127; //自定义纸张最大高度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^.dmPaperLength := Value * 10000; pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; pDMode^.dmDefaultSource := DMBIN_MANUAL; GlobalUnlock(hDMode); end; end; Printer.PrinterIndex := Printer.PrinterIndex; end; //设置纸张宽度:单位--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 //自定义纸张最小宽度76mm if Value < 76 then Value := 76; //自定义纸张最大宽度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; //pDMode^.dmPaperWidth := Value * 10; //将毫米单位转换为0.1mm单位 //谭笑改动 pDMode^.dmPaperWidth := Value * 10000; //将毫米单位转换为0.0001mm单位 pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; pDMode^.dmDefaultSource := DMBIN_MANUAL; GlobalUnlock(hDMode); end; end; Printer.PrinterIndex := Printer.PrinterIndex; end; //在 (Xmm, Ymm)处按指定配置文件信息和字体输出字符串 procedure PrintText(X, Y: Extended; Txt: string; FontSize: Integer=12); var Px, Py: Integer; begin Px := Round(X);// * HPointsPerInch); // /25.4 Py := Round(Y * VPointsPerInch/25.4 ); // Py := Py - GetOffSetY; //因为是绝对坐标, 因此, 不用换算成相对于Y轴坐标 Px := Px + 2 * AvgCharWidth; Printer.Canvas.Font.Name := '宋体'; Printer.Canvas.Font.Size := FontSize; Printer.Canvas.TextOut(Px, Py, Txt); end; procedure DrawLine(Move_X, Move_Y, Line_X, Line_Y : integer; Temp_PenStyle : TPenStyle = psSolid); var Printer_MoveTo, Printer_LineTo : TPoint; {*坐标指针*} Fn: TStrings; {*生成配置文件的名字*} begin Printer_MoveTo.X :=Round(Move_X ); Printer_MoveTo.Y := Round(Move_Y * VPointsPerInch/25.4); Printer_MoveTo.X := Printer_MoveTo.X + 2 * AvgCharWidth; Printer_MoveTo.Y := Printer_MoveTo.Y - GetOffSetY; Printer_LineTo.X := Round(Line_X); Printer_LineTo.Y := Round(Line_Y * HPointsPerInch/25.4); Printer_LineTo.X := Printer_LineTo.X + 2 * AvgCharWidth; Printer_LineTo.Y := Printer_LineTo.Y - GetOffSetY; Printer.Canvas.Pen.Style := Temp_PenStyle; Printer.Canvas.MoveTo(Printer_MoveTo.X,Printer_MoveTo.Y); Printer.Canvas.LineTo(Printer_LineTo.X,Printer_LineTo.Y); end; end.

你可能感兴趣的:(Delphi学习,integer,function,forms,user,windows,string)