procedure MyRectangle(DC: HDC;
Rect: TRect);
procedure SgDrawCenterText(Canvas: TCanvas;
Rect: TRect;
Text: PChar);
overload;
procedure SgDrawCenterText(DC: HDC;
Rect: TRect;
Text: PChar);
overload;
procedure SGDrawCenterText(DC: HDC;
Rect: TRect;
Text: String);
overload;
procedure SgSetPrinter;
procedure SgPrintDJ(CzDj: TCzDJ;
SfPage: TSfPage);
procedure SgPrintDjSf(CzDj: TCzDj;
Canvas: TCanvas;
SfPage: TSfPage);
procedure SgPrintSf(DC: HDC;
aSf: TSF;
T, L: Real;
PPIX, PPIY: Integer);
function IsSfValidate(SF: TSF): Boolean;
function CmToPixelX(cm: Real;
PPIX: Integer):Integer;
function CmToPixelY(Cm: Real;
PPIY: Integer):Integer;
function CmRect(L, T, W, H: Real;
PPIX, PPIY: Integer): TRect;
procedure SgSetPrinter;
var
ADevice : PChar;
ADriver : PChar;
APort : PChar;
DeviceMode : THandle;
DevMode : PDeviceMode;
PPIX, PPIY: Integer;
DC: HDC;
ARect: TRect;
function Supported(Setting : integer) : boolean;
begin
Supported := (DevMode^.dmFields and Setting) = Setting
end;
procedure SetField(aField : integer);
begin
DevMode^.dmFields := DevMode^.dmFields or aField;
end;
begin
GetMem(ADevice, 128);
GetMem(ADriver, 128);
GetMem(APort, 128);
DC := Printer.Handle;
PPIX := GetDeviceCaps(DC,LogPixelsX);
PPIY := GetDeviceCaps(DC,LogPixelsY);
Printer.GetPrinter(ADevice, ADriver, APort, DeviceMode);
DevMode := GlobalLock(DeviceMode);
// if PaperSize = Custom then
// begin
// SetField(dm_PaperSize);
// DevMode^.dmPaperSize := DMPAPER_A3;
SetField(dm_paperlength);
DevMode^.dmPaperLength := Round(PageHeight * 100);
SetField(dm_paperwidth);
DevMode^.dmPaperWidth := Round(PageWidth * 100);
// end;
// if FDuplex then
// begin
// SetField(dm_duplex);
// DevMode^.dmDuplex := dmdup_horizontal;
// end;
// if Supported(dm_PaperSize) and (PaperSize <> Default) then
// begin
// SetField(dm_papersize);
// DevMode^.dmPaperSize := cQRPaperTranslate[PaperSize];
// end;
SetField(dm_copies);
DevMode^.dmCopies := 1;
//FCopies;
SetField(dm_defaultsource);
DevMode^.dmDefaultSource := (DevMode^.dmDefaultSource and 256);// or cQRBinTranslate[OutputBin];
SetField(dm_orientation);
// if Orientation=poPortrait then
DevMode^.dmOrientation := dmorient_portrait;
// else
// DevMode^.dmOrientation := dmorient_landscape;
Printer.SetPrinter(ADevice, ADriver, APort, DeviceMode);
GlobalUnlock(DeviceMode);
with Printer.Canvasdo
begin
Font.Name := '宋体';
Font.Size := 10;
end;
end;
procedure SgPrintDJ(CzDj: TCzDJ;
SfPage: TSfPage);
begin
Printer.begin
Doc;
Printer.Title := format('收费 单据 %d 号',[CzDJ.Djh]);
SgPrintDjsf(CzDj,Printer.Canvas, SfPage);
Printer.EndDoc;
end;
function CmRect(L, T, W, H: Real;
PPIX, PPIY: Integer): TRect;
var
mL, mT, mW, mH: Integer;
begin
mL := CmToPixelX(L,PPIX);
mT := CmToPixelY(T,PPIY);
mW := mL + CmToPixelX(W,PPIX);
mH := mT + CmToPixelY(H,PPIY);
Result := Rect(mL, mT, mW, mH);
end;
function CmToPixelX(cm: Real;
PPIX: Integer):Integer;
begin
Result := Trunc((cm * PPIX) / 2.54);
end;
function CmToPixelY(Cm: Real;
PPIY: Integer):Integer;
begin
Result := Trunc((cm * PPIY) / 2.54);
end;
procedure SgPrintSf(DC: HDC;
aSf: TSF;
T, L: Real;
PPIX, PPIY: Integer);
var
ARect: TRect;
myText, NText: String;
I, Len, mL: Integer;
begin
T := T + 1;
ARect := CmRect(L, T, 5.7, 0.8, PPIX, PPIY);
myText := aSF.xmmc;
SgDrawCenterText(DC,ARect,myText);
L := L + 5.7;
ARect := CmRect(L, T, 2, 0.8, PPIX, PPIY);
myText := aSF.jsdw;
SgDrawCenterText(DC,ARect,PChar(myText));
L := L + 2;
ARect := CmRect(L, T, 2, 0.8, PPIX, PPIY);
myText := format('%.2f',[aSF.sfbz]);
SgDrawCenterText(DC,ARect,PChar(myText));
L := L + 2;
ARect := CmRect(L, T, 2, 0.8, PPIX, PPIY);
myText := format('%.2f',[aSf.sl]);
SgDrawCenterText(DC,ARect,PChar(myText));
L := L + 2;
ARect := CmRect(L, T, 2.8, 0.8, PPIX, PPIY);
myText := format('%.0f',[aSF.je*100]);
Len := Length(myText);
mL := CmToPixelX(L + 2.8, PPIX);
for I := Lendo
wnto 1do
begin
NText := format('%s',[myText]);
ARect.Right := mL;
mL := mL - CmToPixelX(0.4, PPIY);
ARect.Left := mL;
SgDrawCenterText(Dc,ARect,PChar(NText));
end;
end;
procedure SgPrintDjSf(CzDj: TCzDj;
Canvas: TCanvas;
SfPage: TSfPage);
var
DC: HDC;
myText: PChar;
sText: String;
PPIX, PPIY, I, mL, mW: Integer;
T, L, W, H: Real;
ARect: TRect;
y, m, d: Word;
begin
DC := Canvas.Handle;
PPIX := GetDeviceCaps(DC,LogPixelsX);
PPIY := GetDeviceCaps(DC,LogPixelsY);
//
ARect := CmRect(0.1, 0.1, 18.1, 9.2, PPIX, PPIY);
Canvas.Pen.Color := clBlack;
// MyRectangle(Dc,ARect);
//以上打印出一个矩形,用来确定收据的位置
//
//单据号
T := 1.4-SfPage.TopMargin;
L := 12.0;
W := 2.5;
H := 0.4;
myText := PChar(format('编号: %d',[CzDj.djh]));
ARect := CmRect(L,T,W,H,PPIX,PPIY);
SgDrawCenterText(DC,ARect,MyText);
//时间
DecodeDate(CzDj.sj,y,m,d);
T := 1.9-SfPage.TopMargin;
L := 12.8;
W := 0.8;
H := 0.4;
myText := PChar(format('%d',[y]));
ARect := CmRect(L,T,W,H,PPIX,PPIY);
SgDrawCenterText(DC,ARect,MyText);
L := L + 0.8 + 0.4;
myText := PChar(format('%d',[m]));
ARect := CmRect(L,T,W,H,PPIX,PPIY);
SgDrawCenterText(DC,ARect,MyText);
L := L + 0.8 + 0.4;
myText := PChar(format('%d',[d]));
ARect := CmRect(L,T,W,H,PPIX,PPIY);
SgDrawCenterText(DC,ARect,MyText);
//单位
T := 1.9-SfPage.TopMargin;
L := 4.0;
W := 3.4;
H := 0.4;
myText := PChar(String(CzDj.jkr));
ARect := CmRect(L,T,W,H,PPIX,PPIY);
SgDrawCenterText(DC,AREct,MyText);
//地址
T := 1.9-SfPage.TopMargin;
L := 8.3;
W := 4.0;
H := 0.4;
myText := PChar(String(CzDj.dz));
ARect := CmRect(L,T,W,H,PPIX,PPIY);
SgDrawCenterText(DC,AREct,MyText);
//项目
T := 2.4-SfPage.TopMargin;
L := 1.7;
for I := 1 to 3do
begin
if IsSfValidate(CzDj.SfDj) then
begin
SgPrintSf(DC,CzDj.SfDj,T,L,PPIX,PPIY);
T := 2.4 + 0.8-SfPage.TopMargin;
end;
end;
//大写金额
sText := format('%7.0f',[CzDj.je * 100]);
for I := 1 to 7do
begin
if sText = ' ' then
begin
sText := '0';
end;
end;
mW := Canvas.TextWidth('万');
ARect.Left := CmToPixelX(5.3 - 0.8, PPIX);
ARect.Top := CmToPixelY(6.2-SfPage.TopMargin, PPIY);
ARect.Bottom := ARect.Top + Canvas.TextHeight('万');
for I := 1 to 7do
begin
ARect.Right := ARect.Left + CmToPixelX(0.8,PPIX);
SGDrawCenterText(DC,ARect,Copy(Su,(Ord(sText)-Ord('0'))*2+1,2));
ARect.Left := ARect.Left + mW + CmToPixelX(0.8,PPIX);
end;
//小写金额
myText := PChar(format('%.2f',[CzDj.je]));
ARect.Left := CmToPixelX(13.0, PPIX);
ARect.Top := CmToPixelY(6.2-SfPage.TopMargin, PPIY);
ARect.Bottom := ARect.Top + Canvas.TextHeight('万');
ARect.Right := ARect.Left + Canvas.TextWidth(format('%.2f',[CzDj.je]));
SgDrawCenterText(DC,ARect,myText);
L := 13.0;
T := 6.2-SfPage.TopMargin;
//备注
T := 7-SfPage.TopMargin;
L := 4;
W := 12.0;
H := 0.4;
myText := PChar(String(CzDj.pz));
ARect := CmRect(L,T,W,H,PPIX,PPIY);
SgDrawCenterText(DC,AREct,MyText);
//开票
T := 8-SfPage.TopMargin;
L := 9;
W := 2.5;
H := 0.4;
myText := PChar(String(CzDj.kpr));
ARect := CmRect(L,T,W,H,PPIX,PPIY);
SgDrawCenterText(DC,AREct,MyText);
//收款
T := 8-SfPage.TopMargin;
L := 13.0;
W := 2.5;
H := 0.4;
myText := PChar(String(CzDj.skr));
ARect := CmRect(L,T,W,H,PPIX,PPIY);
SgDrawCenterText(DC,AREct,MyText);
end;
function IsSfValidate(SF: TSF): Boolean;
begin
Result := (SF.xmmc <> '') and
(SF.jsdw <> '') and
(SF.sfbz > 0) and
(SF.sl > 0) and
(SF.je > 0);
end;
procedure MyRectangle(DC: HDC;
Rect: TRect);
begin
Rectangle(DC,Rect.Left,Rect.Top,Rect.Right,Rect.Bottom);
end;
procedure SgDrawCenterText(DC: HDC;
Rect: TRect;
Text: PChar);
var
aRect, vRect: TRect;
h, w: Integer;
myRgn: HRgn;
begin
aRect := Rect;
vRect := Rect;
DrawText(DC,Text,StrLen(Text),vRect,DT_CALCRECT);
h := vRect.Bottom - vRect.Top;
w := vRect.Right - vRect.Left;
aRect.Top := aRect.Top + ((Rect.Bottom - Rect.Top) - h) div 2;
aRect.Left := aRect.Left + ((Rect.Right - Rect.Left) - w) div 2;
aRect.Right := aRect.Left + w;
aRect.Bottom := aRect.Top + h;
DrawText(DC,Text,StrLen(Text),aRect,DT_CENTER + DT_VCENTER);
end;
procedure SgDrawCenterText(Canvas: TCanvas;
Rect: TRect;
Text: PChar);
begin
SgDrawCenterText(Canvas.Handle,Rect,Text);
end;
procedure SgDrawCenterText(DC: HDC;
Rect: TRect;
Text: String);
begin
SgDrawCenterText(DC,Rect,PChar(Text));
end;
大同小异,将就^_^