如何在WINDOWS2000下快速打印发票!

解决方案 »

  1.   

    //*********************************发票精确打印函数定义*************************
    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
            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;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
    //        if Value<127 then Value:=127;
    //                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^.dmPaperSize :=0;
                            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);
    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;
    //                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^.dmPaperSize :=0;
                            pDMode^.dmPaperWidth := Value * 10;
                            pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
                            pDMode^.dmDefaultSource := DMBIN_MANUAL;
                            GlobalUnlock(hDMode);
                    end;
            end;
            Printer.PrinterIndex := Printer.PrinterIndex;
    end;procedure PrintText(X, Y: Extended; Txt,FontName:String;FontSize:Integer);
    var
            Px,Py:Integer;
            OffSetX, OffSetY: Integer;
    begin
            OffSetX:=0;
            OffSetY:=0;
            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;
            Px := Px + 2 * AvgCharWidth;
            Printer.Canvas.Font.Name := FontName;
            Printer.Canvas.Font.Size := FontSize;
            Printer.Canvas.TextOut(Px, Py, Txt);
    end;
    //*******************************************************************************
    //使用时先调用
    //                SetPaperHeight(65);
    //                SetPaperWidth(175);
    //设置发票的高度和宽度(单位:毫米)
    //            Printer.BeginDoc;
    //                 PrintText(X坐标,Y坐标,‘要打印的字符!’,'字体',字体大小);
    //            Printer.EndDoc;