Achieve "WYSIWYG" in VB

Source: Internet
Author: User
Windows Program Most features include WYSIWYG (what you see is what you get. "What you see is what you get" means that the printing and output effects of the software are exactly the same as those displayed on the screen. "WYSIWYG" is one of the main advantages of Windows programs.

This article describes how to use Visual Basic (VB) to compile a Windows program with the "WYSIWYG" function.

I. Method Overview
To simulate the printing effect on the screen, we use a form of VB to simulate a printing paper. In VB, each form has a printform method. The printform method can print the content of a form exactly the same as that of a printer. Because the printform method prints the content of the form, the text and graphics in the form are printed in the same way. Therefore, through simple programming, the printform method can be used to conveniently implement text-and-image hybrid printing.

① Determine the dual-function display and printing modes that can be used by users

In the "What you see is what you get" program, you must use a dual-function module that supports both Windows display devices and print devices. Only these fonts can produce the same effect on the screen and printer. Therefore, the program must first obtain the names of these display and print dual-function fonts.

② Set the size of the "paper" form on the screen

By reading the paper size settings of the Windows Print manager, you can create a form with the same size ratio as the printed paper in VB as the "paper" that simulates the printing effect on the screen ", on the screen, users can see the distribution of text and graphics on the print paper.

③ The user simulates the printing effect on the screen "Paper"

You can use print, line, and circle methods to draw text and graphics in a simulated printing form. You can also use a graphic box or graph. vbx, Ole, and other controls are used to draw graphics, statistical charts, formulas, and artistic fonts.

④ Output the content of the screen "paper" to the printer

Use the printform method of the form to output the text and graphics drawn in step 3 to the printer, and print and copy the text exactly the same as the screen display to achieve "What you see is what you get ".

2. Exact match of printer and display Fonts
In Windows, there are two types of fonts: printer and display. Various TrueType fonts (such as and) are dual-function display and printing fonts. Other non-TrueType fonts can only be used for display or printing.

If you use a dashboard that can only be used for display in the text output of a simulated form, when the printform method is executed, in Windows, you can only search for a pattern matching the displayed pattern to print it. This will cause the print copy to be inconsistent with the screen display.

To enable the program to achieve "WYSIWYG", you must use the dual-function dashboard for display and printing in the form output. Therefore, the key to a program's ability to achieve "What you see is what you get" is to ensure that only the display and print dual functional fonts are used in the screen output.

In VB, two global objects are provided: Screen Object and printer object printer. Both objects provide the fontcount attribute and fonts attribute. The fontcount attribute provides the number of fonts that can be used, while the fonts attribute provides the names that can be used. In the program, we can take out the fonts shared by the screen object and the printer object. These fonts are all the dual-function display and print fonts.

The specific procedure is as follows:

First, set a list box named list1, and then call the following getfont function to store the names of all display and print dual-function fonts in list1.

Sub getfont ()
For I = 0 to screen. FontCount-1
For J = 0 to printer. FontCount-1
If screen. fonts (I) = printer. fonts (j) then
List1.add1tem (sereen. fonts (I ))
End if
Next J
Next I
End sub

Iii. Matching of form size and paper size
In order to clearly see the position and size of text and graphics on the printed paper on the screen, the size ratio of the VB display window must be consistent with that of the printed paper. The size of the printed paper can be obtained using the width and height attributes of the printer object. Based on the obtained property value, you can set the size ratio of the VB display form to conform to the paper size.

The following program has a variable size, which ranges from 0 to 1. It controls the size of the printed image. When the value of size is 1, the print image will be the same size as the paper set in windows. The specific procedure is as follows:

Sub form_load ()
Dim sizeas intecer
Size = 0.95
Form1.height = printer. Height * size
Form1.wdth = printer. Width * size
End sub

4. Analog printing and Output
The following program demonstrates how to simulate text and graphics printing on the display form, and how to use printform to output text and graphics in the display form to the printer.

Note: To use the printform method, you must set the autoredraw attribute of the display form to true.

Because the printform method prints the content of a form using the bit-by-bit COPY method, all text and images in the form can be printed as they are. For example, you can use the OLE Control to display the formulas edited in the word formula editor, and use the picture and graph controls to display graphs and statistical graphs. Then print them out using the printform method.

The specific procedure is as follows:

Sub printonscreen ()
Form1.scale (100,100)-() 'sets coordinates
Form1.line (0,100)-() 'show draw line
Form1.currentx = 0
Form1.currenty = 0
Form1.fontname = "" 'output text
Form1.fontsize = 20
Form1.print "simulated printing"
Picture1.picture = loadpicture ("setup.bmp") 'output image
Form1.printform: Send the content of the displayed form to the printer
Printer. enddoc ??? 'Start printing
End sub

 

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.