Using Delphi to achieve accurate ticket printing

Source: Internet
Author: User

I. Overview
In the banking, tax, postal and other industries in the actual work, often involves in the printing of a fixed form of money orders, savings vouchers,
Information about the printout of the confirmation location on the invoice and other documents. In such requirements, the precise positioning of documents and the printing of relevant information,
is the key to solving the problem. In general, developers are using repetitive tests on the printer to achieve the actual requirements.
So, is there a simple, effective and flexible way to achieve the above function?
Second, the basic ideas

Analysis of the characteristics of the above-mentioned documents, you can find: This type of printout of the information is generally relatively short, does not involve the text too long folding line processing,
In addition, the position of the printout is relatively fixed. Therefore, we can measure each output information position by using a ruler in millimeters.
Horizontal and vertical coordinates, as the position of the information output. However, due to the actual output of different printers, there is always a theoretical and actual position
, therefore, requires the program to have some flexibility for the end user to make the necessary position adjustments as needed. Therefore, you can set
A print configuration file that stores the horizontal and vertical offsets for the user for position correction, providing some flexibility.
Third, the accurate printing output program realization
1. Create a new unit named Mprint.pas in Delphi and write the following program, add printers to the cell reference:

Gets the height of the character
function Charheight:word;
Var
Metrics:ttextmetric;
Begin
GetTextMetrics (Printer.Canvas.Handle, Metrics);
Result: = Metrics.tmheight;
End

file://get the average width of the character
function Avgcharwidth:word;
Var
Metrics:ttextmetric;
Begin
GetTextMetrics (Printer.Canvas.Handle, Metrics);
Result: = Metrics.tmavecharwidth;
End

file://physical dimensions of the paper---units: points
function Getphicalpaper:tpoint;
Var
Pagesize:tpoint;
Begin
File://PageSize.X; Paper Physical width-unit: Point
File://PageSize.Y; Paper Physical height-unit: Point
Escape (Printer.handle, Getphyspagesize, 0,nil, @PageSize);
Result: = PageSize;
End

FILE://2. Obtaining the paper's logical width--printable area
file://get the logical size of the paper
function Paperlogicsize:tpoint;
Var
Apoint:tpoint;
Begin
Apoint.x: = Printer.pagewidth;
Apoint.y: = Printer.pageheight;
Result: = Apoint;
End

file://the horizontal aspect ratio of the paper horizontally to the vertical direction
function hvlogincratio:extended;
Var
Ap:tpoint;
Begin
Ap: = paperlogicsize;
Result: = ap.y/ap.x;
End

file://the horizontal offset of the paper-Unit: Point
function Getoffsetx:integer;
Begin
Result: = GetDeviceCaps (Printer.handle, PHYSICALOFFSETX);
End

file://the vertical offset of the paper-Unit: Point
function Getoffsety:integer;
Begin
Result: = GetDeviceCaps (Printer.handle, physicaloffsety);
End

file://mm units converted to inch units
function Mmtoinch (length:extended): Extended;
Begin
Result: = length/25.4;
End

file://inch units converted to millimeters
function Inchtomm (length:extended): Extended;
Begin
Result: = length*25.4;
End

file://the number of printer points per inch in the horizontal direction
function Hpointsperinch:integer;
Begin
Result: = GetDeviceCaps (Printer.handle, logpixelsx);
End

file://get the number of gratings per inch printer vertically
function Vpointsperinch:integer;
Begin
Result: = GetDeviceCaps (Printer.handle, Logpixelsy)
End

file://horizontal Point unit to millimeter unit
function Xpointtomm (Pos:integer): Extended;
Begin
Result: = Pos*25.4/hpointsperinch;
End

file://vertical point units converted to millimeters units
function Ypointtomm (Pos:integer): Extended;
Begin
Result: = Pos*25.4/vpointsperinch;
End

file://Setting the paper Height-unit: 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
file://Custom paper Minimum height 127mm
If value < 127 then Value: = 127;
file://Custom Paper Maximum height 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://Setting the paper width: unit--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
file://Custom Paper Minimum width 76mm
If value < then Value: = 76;
file://Custom Paper Max width 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://convert millimeters units to 0.1mm units
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://at (Xmm, Ymm) by specifying profile information and font output string
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;
Begin
file://open configuration file, read horizontal and vertical offsets
Try
Fn: = tstringlist.create;
FileName: = Extractfilepath (application.exename) + configfilename;
If FileExists (FileName) Then
Begin
Fn.loadfromfile (FileName);
file://Transverse offset
OffSetX: = Strtoint (fn.values[' X ');
file://longitudinal offset
OffSetY: = Strtoint (fn.values[' Y ');
End
Else
Begin
file://If there is no configuration file, the build
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://because it is an absolute coordinate, therefore, it does not have to be converted to Y-axis coordinates
PX: = px + 2 * avgcharwidth;
Printer.Canvas.Font.Name: = ' song body ';
Printer.Canvas.Font.Size: = FontSize;
File://Printer.Canvas.Font.Color: = Clgreen;
Printer.Canvas.TextOut (Px, Py, TXT);
End

2. Examples of Use

Add a reference to the Mprint unit in the main form, in the onclick of a command button
The following code is written in the event (for printing zip code 843300 in the corresponding box on the postal order):

Printer.begindoc;
Printtext (+, ' 8 ', ' Config.txt ');
Printtext (+, ' 4 ', ' config.txt ');
Printtext (+, ' 3 ', ' config.txt ');
Printtext (+, ' 3 ', ' config.txt ');
Printtext (+ 0 ', ' config.txt ');
Printtext (0 ', ' config.txt ');
Printer.enddoc;

Observe the result, measure the offset with a ruler, and modify the value of x, y in the Config.txt file.

Other, set printer and paper type withheld.

Iv. concluding remarks

Use Delphi for accurate ticket printing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.