16,748
社区成员
发帖
与我相关
我的任务
分享
unit Print;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Printers, Grids, DBGrids, DB, ADODB,DateUtils, ExtCtrls;
procedure SetPaperHeight(Value:integer); //设置纸张高度-单位:mm
procedure SetPaperWidth(Value:integer); //设置纸张宽度:单位--mm
function _init(PageSize:TPoint):Boolean; //
function _outTxt(x,y:Real;Txt:String;FontSize:Real;FontName:String):Boolean;
procedure PrintPage();
procedure PrintPageend();
procedure PrintNewPage();
var
PhysicalWidth,PhysicalHeight,PageWidth,PageHeight,PageLeft,PageRight,PageTop,PageBottom:Integer;
mm_H,mm_V:real; //毫米
implementation
procedure SetPaperHeight(Value:integer); //设置纸张高度-单位:mm
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 < 127 then Value := 127; //自定义纸张最小高度127mm
if Value > 432 then Value := 432; //自定义纸张最大高度432mm
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;
procedure SetPaperWidth(Value:integer); //设置纸张宽度:单位--mm
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 < 76 then Value := 76; //自定义纸张最小宽度76mm
if Value > 216 then Value := 216; //自定义纸张最大宽度216mm
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^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
GlobalUnlock(hDMode);
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;
function _init(PageSize:TPoint):Boolean; //
begin
PhysicalWidth:=PageSize.x; //物理页宽
PhysicalHeight:=PageSize.Y; //物理页高
PageWidth:=printer.PageWidth; //逻辑页宽
PageHeight:=printer.PageHeight; //逻辑页高
PageLeft:=GetDeviceCaps(Printer.Handle,PHYSICALOFFSETX); //左边距
PageTop:=GetDeviceCaps(Printer.Handle,PHYSICALOFFSETY); //上边距
PageRight:=PhysicalWidth-PageLeft-PageWidth; //右边距
PageBottom:=PhysicalHeight-PageTop-PageHeight; //下边距
end;
function _outTxt(x,y:Real;Txt:String;FontSize:Real;FontName:String):Boolean;
var
LogRec: TLOGFONT;
OldFont, NewFont: HFONT;
i: LongInt;
begin
with printer do
begin
GetObject(Canvas.Font.Handle, SizeOf(LogRec), @LogRec);
LogRec.lfOutPrecision := OUT_TT_ONLY_PRECIS;
LogRec.lfFaceName :='宋体';
LogRec.lfHeight:=round(FontSize*mm_V);
LogRec.lfWeight:=0;
NewFont := CreateFontIndirect(LogRec);
OldFont := SelectObject(Canvas.Handle,NewFont);
end;
x:=Round(x*mm_H)-PageLeft;
y:=Round(y*mm_V)-PageTop;
Printer.Canvas.TextOut(round(x),round(y),txt);
end;
procedure PrintPage();
var
PageSize:Tpoint;
PointX,PointY:Integer; //英寸
begin
Printer.Canvas.Brush.Style := bsclear;
Escape(Printer.Handle, GETPHYSPAGESIZE, 0,nil,@PageSize); //取得物理页尺寸
PointX:=GetDeviceCaps(Printer.Handle,LOGPIXELSX);
PointY:=GetDeviceCaps(Printer.Handle,LOGPIXELSY);
mm_H:=PointX/25.4;
mm_V:=PointY/25.4;
Printer.BeginDoc;
end;
procedure PrintPageend;
begin
Printer.EndDoc;
end;
procedure PrintNewPage();
begin
printer.NewPage;
end;
end.