Write VB print control program

xiaoxiao2021-03-06  45

1. Introduction

Visual Basic (VB) provides users with a visual programming environment, which has been widely used due to its simple and easy to learn. VB provides two ways to implement printing. Generally, printing is implemented in an occasion of print quality requirements, or during earlier development of programming items, printing can be implemented directly using the PrintForm method of the VB form. Using this method, printing has the advantages of simple programming, easy to use and powerful, it only needs to pass a line of code, almost print all content. The implementation method is: First, the content to be printed is displayed on the screen, and then the developer will activate the PrintForm to the form object, the form is automatically sent to the Printer object, and its syntax format is as follows: [Form. ] Printform. If the form is included in the form, the autoreraw attribute that should be first formated before printing is true. Although this method is simple, it has defects such as memory consumption, printed, and slow speed, especially for images with scroll bar, which can only print the currently visible area. In practical applications, there is often a high-printed quality requirement, such as printing music five-tales, and has a strict requirement for printing beauty, clarity, and note heads. If this application is available in VB Another print method based on the Printer object, you can get high-resolution printing to get high print quality. In practical applications, the above two methods can also be used in accordance with the actual application, ie, the previous work uses the printform simple print method to set the form of the form, and later use the Printer-based printing method to achieve the final Print work.

2. High resolution printing program development experience

(1) PRINTER object

Printing of VB can use the Printer object. The Printer object is a package that is independent of the printer device, which can represent different printers, initially, the Printer object is the default printer for the system, or the following statement: set printer = printers (2) (2 represents the PRINTERS concentration The second printer) specifies the printer.

The Printer object has, for example: ColorMode, COPIES, DUPLEX, PrintQuality, etc. The properties of the printer feature provide new ways, enddoc, killdoc, etc., and Most of the graphical properties and methods provided by the form and picture box controls. : Currentx, Currenty, TextWidth, TextHeight, Print, Pset, Line, PaintPicture, and Circle, it also has all properties of Font. Implementing high resolution printing is done by controlling the above properties and methods of the Printer object.

(2) Direct use of Windows Public Standards dialog box Commondialog control [Print]

VB provides users with the Windows Public Standards dialog CommonDialog control: [Open], [file saved as], [color], [font], [print]. The Commondialog control provides an interface between the Visual Basic and Microsoft Windows Dynamic Library CommDlg.dll routines. Using the Public Standard dialog box [Print] Develop VB print procedures, it will greatly shorten the development cycle of the program. To use a public dialog in your application, you must first add a public dialog control in the toolbox. This action is activated [Parts] dialog box, select Microsoft Common Dialog Control6.0, click [OK]. Then add the public control to the form and set the corresponding attribute, which has Color, Font, Print, Help, etc.

VB provides a MnuFilePrint_Click () process for users to add development print program code.

(3) Printer object Controlling the basic process of printing Using Printer objects Developed Printing printed by the Printer object primarily relying on its --newpage (printing new page, Currentx, Currenty is set to the upper left corner of the new page, can complete the multi-page function.), ENDDOC (Add Print Tasks to the printer queue), killdoc (canceled printing task) controls the printing process. In general, the print program will perform newpage multiple times when the print program completes multiple pages, and executes EndDoc to join the print task to the printer queue. If you use Enddoc, use Enddoc, VB, will not print extra blank pages. If you want to display your blank page, you can use Printer.print "" to print empty characters on a new page.

In order to achieve printing of text and graphics through the Printer object, the best print quality is obtained, but also a more in-depth understanding of the VB control printer, such as the conversion of the coordinate system of the printer, the transition of the screen coordinate system, the printer font size Determination, etc. The main framework of the print program is given below:

Private sub mnufileprint_click ()

ON Error ResMe next

If ActiveForm Is Nothing Ten Exit Sub

With dlgcommondialog 'printer utility dialog

.Dialogtitle = "Print"

.Cancelerror = true

.Flags = 1

Printer.fontsize = dlgcommondialog.fontsize

'Pass the font size of the printer public dialog to the printer

.Showprinter 'displays [Print] Public dialog on the screen

IF Err <> mscomdlg.cdlcancel then

Printer.FontTransparent = false 'Initialization The font is opaque

Setprinterscale MyForm 'matches the printer's zoom attributes with the properties of the form

PRINTANYWHERE PRINTER 'can place user-written print object parameterization routine

'Show display of characters and graphics

Printer.newpage 'printer coordinate initialization

Printanywhere printer 'prints another page

Printer.newpage 'printer coordinate initialization

Printer.Enddoc 'Add this task to the printer task queue

'Don't print blank

Printer.killdoc 'Cancel the current print task

END IF

End with

End Sub

⑷ Parameterized drawing program

When developing an application with VB, if you use the parameterized drawing routine to develop the program development, when developing a print program, you don't need to develop code separately, thereby avoiding a lot of repetitive labor, effectively shortening the program. Development cycle. The parameterized drawing routine is: When the development routine provides an object type parameter for each routine, the calling program passes the form, the Printer object, respectively, the form, the Printer object, respectively, can complete the screen display, respectively. Output with the printer. See the following example:

Sub Printanywhere (DEST AS Object)

Dest.print "Hello!"

Dest Is Printer Then

Printer.Enddoc

END IF

End Sub

To complete the output on the screen, call Printanywhere MyForm, and call Printanywhere Printer to complete the output on the printer.

⑸ Property matching with form zoom

Because the properties of the printer actual drawing area size height and width are determined by the paper currently being used, and the printed area has a certain distance from the edge of the paper. Therefore, in order to obtain the correct print output result, the Printer object cannot be simply transmitted to the drawing routine, but also solve the problem that the printer's zoom attribute matches the display form property. That is: to ensure that the print content in the form is displayed in the correct size and hosted in the middle of the printable area. In fact, it is realized that the printer's coordinate system is converted to the screen coordinate system. The specific process is: Using the printer's Scalex and Scaley methods, get the printer size in TWIP, and then convert these dimensions to the coordinate system in the form using the form of Scalex and Scaley methods, thereby implementing the coordinate system in the form. Provides the purpose of printing area of ​​printers. The properties match can then be used as new ScaleWidth and ScaleHeight in the printer.

However, when we print, we often encounter such an occasion - need to expand or reduce the size of the form without changing the form shape. To do this, you need not only to complete the properties, but also determine the coefficients that are scaled. The procedure is as follows:

Private sub setprinterscale (Obj As Object)

DIM PWID AS SINGLE, PHGT AS SINGLE, XMID AS SINGLE, YMID AS SINGLE

Dim Owid As Single, Ohgt As Single

Owid = obj.scalex (obj.scalewidth, obj.scalemode, vbtwips)

Ohgt = obj.scaley (obj.scaleheight, obj.scalemode, vbtwips)

'Get the size of the form in twips

PWID = printer.scaleX (Printer.scaleWidth, Printer.Scalemode, VBTWIPS)

PHGT = printer.scaley (Printer.ScaleHeight, Printer.Scalemode, VBTWIPS)

'Get the size of the printer in twips

IF (OHGT / OWID> PHGT / PWID) THEN

s = phgt / ohgt

Else

S = PWID / OWID

END IF 'calculating zoom factor

PWID = Obj.ScaleX (PWID, VBTWIPS, OBJ.SCALEMODE) / S

PHGT = Obj.scaley (PHGT, VBTWIPS, OBJ.SCALEMODE) / S

'Convert the size of the printer into an OBJ coordinate system / zoom factor

X_mid = obj.scaleleft Obj.scaleWidth / 2

Y_MID = Obj.scaletop Obj.scaleHeight / 2 'Set the center point coordinates of the print area

Printer.scale (X_MID - PWID / 2, Y_MID - PHGT / 2) - (X_MID PWID / 2, Y_MID PHGT / 2)

'Set new ScaleWidth and ScaleHeight in the printer

End Sub

⑹ coordinate positioning

The textWidth and TextHeight methods provided by the form, the picture frame control, and the Printer object are useful in text positioning applications. TEXTWIDTH, TEXTHEIGHT returns the width and height of the string with the current scale unit, respectively. However, it must be noted that the character width in many fonts is not all the same, so the width of the single character cannot be simply multiplied by the number of characters to obtain the width of the string. For those controls without TextWidth and TextHeight, by setting the FONT attribute of the parent form, it matches the properties of the control, and then obtains the width and height of its string using the textWIDTH and TextHeight of the parent form. However, after completing the screen display program, we have a phenomenon in which the screen display and print results are displayed in the printout, and the coordinate relationship between the printed font is different from the screen display, for example: screen display The two characters on the upper number do not overlap, and the characters printed have a phenomenon that overlap each other. The debug found that the problem appeared on the font size. In the program, the character coordinations displayed on the screen are calculated by the TEXTWIDTH and TextHeight methods of the object, which is directly affected by the size of the font size. The font provided to the user is within the range of the printer point required by the user. If you need the font size of 73 points, the printed actual font size is 72.75. However, the FONTSIZE for the form, the picture frame control can only be an integer, for example it cannot set the size of the font to 72.75, in fact, this causes an error between display fonts and actual print font size on the screen. We use the following method to solve this problem, that is, in the calculation font size, according to the calculated font size, determine its existing range, then position it to several fixed-size fonts provided by VB, not will specify . Of course, there can be other solutions. For example: the font size selects even the like. Due to space limitations, this is no longer introduced.

The above is some of the experience and experience in summing up practical application work, hoping that this article can help the designers who first use VB to develop print programs.

转载请注明原文地址:https://www.9cbs.com/read-67958.html

New Post(0)