I. Introduction
In recent years, with the popularity and promotion of the Internet, traditional stand-alone mode and C / S model in the LAN have become more and more unable to meet the requirements of information sharing. Therefore, a new development method based on the browser-based B / S is enrolled. The new development plan is automatically updated automatically based on its client's maintenance, free of configuration, and the program can automatically update the upgrade according to the information of the server; the server-side multi-layer mode should improve the efficiency and security of the processing. It is optimistic about it. Become a new direction of development development. On Windows's platform, people use ASP to develop service display interfaces, and use components to package business rules, and use various tools to use a variety of magazines to develop components. It is also uncommon. But introducing the printing group is not very much. The author hopes to throw the jade from a little experience in the development of printing components.
Second, the idea
The web print component requires that some of the bills in this system need to print in a certain unit development information management system. In this way, how to set these bills into problems we have to solve.
There are two ways to print some of the tickets or other text or image information on the client, one is to generate the interface of the client using Delphi's Active Form, and is downloaded by the browser and runs on the client. (This approach is more common in the development of multi-bache database. There is a method in China's new version of financial software. Its disadvantage is that the requirements for developers are too high), the other is to develop a component in the customer. End Installation, then generate the VBScript script by the server, run, create the corresponding component object; use the component object to make a print operation. (This method is as long as the client's COM component development is completed, as long as it is familiar with the VBScript or JavaScript scripting language to be convenient). This article we mainly discuss the second way.
Third, realize
First, run Delphi 5.0; select New in the File menu, select the ActiveX page in the pop-up dialog box, select ActiveX Library to create an ActiveX library. Then, add an Automation Object to the newly created library, and the operation steps are. Fill in the component name PrtTest3 in the Automation Object Wizard dialog; click OK.
Then, in the pop-up PrtTest3.tlb window, select IPRTTest3. Click Right click to add an isinit's Property, the type is long. Continue to increase the following five Method:
PROCEDURE PRTCUSTOMPAGE (PageHeight: Integer; SaFECALL;
PROCEDURE PRTSTART; SAFECALL;
PROCEDURE PRTNEWPAGE; SAFECALL;
PROCEDURE PRTEND; SAFECALL;
Procedure procrawline (x1: integer; x2: integer; y2: integer; linewidth: integer;
Procedure PrtSetfont (const fontname: WideString; Fontsize: integer; SaFECALL;
PROCEDURE PRTTEXTRECT (Valign: Integer; Rect: Integer; RectTop: Integer; Rectright: Integer; RectBottom: integer; const printstring: wideString;
They mean: PRTCUSTompage is used to set custom paper. Introduced parameters customize the width and height of the paper. PrtStart is used to initialize the printer. If initialization is successful, the attribute isinit is set true, and it is false.
PrtneWPage is used to make the printer to change the page.
PRTEND ends the print task.
Prtdrawline is used to draw a line on the page. (X1, y1) is the starting point. (X2, y2) is the termination point. LINEWIDTH is the width of the print line.
PrtSetFont is used to set the printed font name and size.
PrtTEXTRECT is used to specify the specified string of the output in the box.
Fourth, call
Method for calling this component in the script:
(1) Create a print object using the CreateObject function.
(2) Call the PRTCUSTompage to specify the size of the custom paper (based on 0.1 mm). If, not a customized paper, do not need to call this process (ie, print the default paper size).
(3) Call PrtStart to initialize the printer. If the printer is initialized, it will be true, indicating the initialization success; otherwise, indicating that the printer is busy or other applications are using the printer, initialization cannot succeed.
(4) Judging the Isinit flag. If true, continue the print segment.
(5) Execute the print segment. You can use the printer to display the text and output in the specified location.
(Note: All positions in the component are 0.1 mm. For example, .prtobject.prtdrawlien 0,0,1000, 1000 represents a straight line from the upper left corner (0, 0) mm to the lower right corner (100, 100) mm )
(6), the print task is ended by prtend.
Sub testprtObject
DIM PRTOBJECT
Set prtObject = creteObject ("prTtest3.prttest3)
PrtObject.prtcustompage 1000, 1000
PrtObject.prtstart
IF prtObject.isinit then
PrtObject.prtdrawline 0,0,1000,1000,1
PrtObject.prtdrawline 500, 700, 1000, 1000, 1
PrtObject.prtsetFont "Song", 16
PrtObject.prtTextRect 2, 2, 0, 0, 1000, 500, "Web Application Print Test"
PrtObject.prtnd
END IF
Set prtObject = Nothing
End Sub
TestPRTOBJECT ()
// ->
Script>
V. Code analysis
In this component, several questions we have to solve:
(1), in Delphi's application design, custom print paper settings, with a QuickReport print design in Delphi, which facilitates print design in a certain extent, but this design is customizable Paper settings and print support are not very good. Therefore, in this component we use manual code to set the custom paper size.
Function TPRTTest3.INITPRINTPAPER: BOOLEAN; VAR
Device: array [0..cchdevicename-1] of char;
Driver: array [0 .. (max_path-1)] of char;
Port: array [0..32] of char;
HDMODE: THANDLE;
PDMODE: PDEVMODE;
Begin
RESULT: = TRUE;
IF Prtiscustompaper THEN
Begin
{Set printer segment}
Printer.getPrinter (Device, Driver, Port, HDMode);
IF HDMODE <> 0 THEN BEGIN
Try
PDMODE: = Globalock (HDMODE);
IF pdmode <> nil kilin
/ / Set the direction of printing is longitudinal or horizontal
IF Paperorientation <> 0 THEN PDMODE ^ .dmorientation: = DMORIENT_LANDSCAPE
Else PDMode ^ .dmorientation: = DMORIENT_PORTRAIT;
/ / Set the copying number of 1 copy.
PDMODE ^ .dmcopies: = 1;
// The size of the paper in millimeters.
PDMODE ^ .dmpaPerLength: = PaperHeight * 10;
PDMODE ^ .dmpaperWidth: = PaperWidth * 10;
// Set the paper type to customize the user.
PDMODE ^ .dmpapersize: = DMPAPER_USER;
END;
GlobalUnlock (HDMODE);
Printer.SetPrinter (Device, Driver, Port, HDMode);
Except
Result: = FALSE;
END;
ELSE BEGIN
Result: = FALSE;
END;
END;
END;
(2) Determination of the print position: Since the component is printed in a graphic mode, this makes it possible to accurately point to the output location of the print file to facilitate a set of tickets. But then produce a problem, we specify the input location in the printing pixel location, or in the print size. Obviously, using pixels to output a print position, it is more convenient for the program. However, since the general user does not understand the concept of pixels, it will bring difficulties in use; and the resolution of each printer is different, so there is a problem that the effect of printing on different printers will appear. In order to specify the location in the print size and there will be no top problems in the program. The specific processing method is to input the size position by the user. Then, when printing, first take the current printer's resolution (Note: The resolution is in units of pixels per inch), and then calculates the pixel position of the actual input, so that the component can make the component more practical, Moreover, maintaining the coherence of the printing. The specific function is as follows.
Function TPRTTest3.mmtoprintpixel (InputPoint: tpoint): TPOINT;
Begin
Result.x: = mmtoprinTpixelx (InputPoint.x);
Result.y: = mmtoprintpixely (inputpoint.y);
END;
Function TPRTTest3.mmtoprintpixelx (Inputx: Integer): Integer;
Begin
IF queryprintlogpixel life
Begin
PDDix: = getDeviceCaps (Printer.Handle, Logpixelsx); PDDIY: = GetDeviceCaps (Printer.Handle, Logpixelsy);
QueryPrintLogpixel: = false;
END;
Result: = trunc (Inputx / 253.8 * PDDix 0.5);
END;
Function TPRTTest3.mmtoprintpixely (Inputy: Integer): Integer;
Begin
IF queryprintlogpixel life
Begin
PDDix: = getDeviceCaps (Printer.Handle, Logpixelsx);
PDDIY: = getDeviceCaps (Printer.Handle, Logpixelsy);
QueryPrintLogpixel: = false;
END;
Result: = trunc (INPUTY / 253.8 * PDDIY 0.5);
END;
GetDeviceCaps: A function of acquiring the specified device information for the Windows system. The prototype of the function is as follows
INT getDeviceCaps
HDC HDC, // Set handle
INT NINDEX / / The device parameters of the query.
);
Among them, HDC's handle of the printer wants to query, Logpixelsx, Logpixelsy indicates that the content you want to query is the number of pixel points per inch of the printer horizontal and vertical direction.
Six, registration of components
If this component is written in Delphi, you can select the Register ActiveX Server under the RUN menu to register for this component.
For registration without developing machines, you can use: Click "Start" → "Run"; enter the following command in the Run dialog:
Regsvr32
Regsvr32 / u
Seven, other applications
Component's extension: The above list is only the framework of this component, and the function is limited. We can also add functions such as painting, ellipse, rectangles, and even pictures as needed. If you are interested, please add it yourself.
Because the components are running in a COM, we also apply this component in Window Script Hosts (WSH), combined with the ability to access the external database in WSH (through the ADO object), you may make powerful power under Windows WSH script.
Please go to the programmer magazine channel download.