Simple implementation of WYSIWYG printing in VC

Source: Internet
Author: User
In VC ++ 6.0, the single or multi-document program generated by the Application Wizard (Appwizard) provides the implementation of the printing function, unfortunately, if you do not make any improvements to the automatically generated Framework Program, the printed documents or images will be very small compared to the display on the screen. Why?

This paper analyzes the cause of this phenomenon and the printer System of MFC, and puts forward a particularly simple method, in the original program, only a few lines of code can be added to solve this problem and achieve WYSIWYG printing.

First, analyze the printer System of MFC and clarify the principle. It is not difficult to understand the cause of the phenomenon and propose a solution. The core of the MFC application is the document object and the concept of the relevant View window, that is, the composition and relationship between the cdocument class and the cview class. Simply put, the cdocument class is responsible for data generation and storage, the cview class displays data and interacts with users. The data output to the screen and output to the printer are essentially the same, so the printing function is also implemented by the cview class.

In the cview class, the source code automatically generated by the Application Wizard provides an ondraw (CDC * PDC) function. By reloading this function, you can use the PDC (device context) pointer provided by it, various images and data can be displayed on the screen. The printing of the cview class is implemented through the onprint (CDC * PDC, cprintinfo * pinfo) function. The framework of this function is not available in the source code automatically generated by the Application Wizard, the implementation of this function for printing is to simply call the ondraw (CDC * PDC) function to pass the context pointer PDC of the printer to the ondraw (CDC * PDC) function.

It can be seen that the cview class processes the output to the screen and the output to the printer in the same way, but only in a different device context. Why is the output to the printer image very small?

This is related to the default coordinate ing method mm_text used by VC. The advantage of this method is that the user's graphic coordinates are completely consistent with the pixel of the device. However, when the screen pixel size is 800*600, the screen pixel size per logical inch is 96, and the printer's points are several times more, for example, if the printer is HP LaserJet 6l, the number of printer points per logical inch is 600, that is, the definition of the printer is much higher than that of the screen.

The consequence is that the full screen image displayed on the screen is only a little large on the printed paper. How can this problem be solved? A simple method is to convert the coordinate ing method so that the proportion of coordinates used for printing is several times larger than that used for display.

The detailed method is provided below.

Note that the cview class will call the virtual member function virtual void onpreparedc (CDC * PDC, cprintinfo * pinfo = NULL) to prepare the device context before display and printing, we can reload this virtual member function in the cview class for coordinate transformation.

First, use the classwizard of VC to overload the onpreparedc (CDC * PDC, cprintinfo * pinfo = NULL) function. The source code generated by classwizard is as follows:
Void ctempview: onpreparedc (CDC * PDC, cprintinfo * pinfo)
{// Todo: add your specialized code here and/or call the base class

Cview: onpreparedc (PDC, pinfo );

}

We only need to add the following lines of code to the source code:

Void cprintsameview: onpreparedc

(CDC * PDC, cprintinfo * pinfo)

{Cview: onpreparedc (PDC, pinfo );

PDC-> setmapmode (mm_anisotropic); // converts the coordinate ing mode.

Csize size = csize (800,560 );

PDC-> setpaiwext (size );

// Determine the window size // obtain the number of pixels per logical inch of the actual device. Int xlogpixperinch = PDC->
Getdevicecaps (logpixelsx );

Int ylogpixperinch = PDC-> getdevicecaps (logpixelsy );

// Obtain the ratio of device coordinates to logical coordinates long xext = (long) size. CX * xlogpixperinch/96;

Long yext = (long) size. Cy * ylogpixperinch/96;

PDC-> setviewportext (INT) xext, (INT) yext );

// Determine the preview size}
As shown above, the coordinate ing method is changed to the mm_anisotropic method, that is, the meaning of the opposite sex. In this method, the logical units of the X axis and Y axis can be scaled at any time. After changing the coordinate ing mode, you must determine the window size and the window size. Note that the window size is what we see on the screen, while the window size is the actual device, such as a printer, the proportional size compared to the number of pixels per logical inch of the display device. The function is used to obtain the number of pixels per logical inch for the display and printer. Then, the camera port size is scaled accordingly to make the display on the screen consistent with the printer output.

In this way, we can print what we see as what we get with just a few lines of simple code.

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.