Easy implementation of what you have obtained in VC

zhaozj2021-02-16  41

The single document or multi-document program generated by the application wizard in VC 6.0 provides the implementation of the print function, but unfortunately if the automatically generated framework does not do any improvement, the printed document or The graphics and the display on the screen are particularly small. Why is this so?

This paper has analyzed the cause of this phenomenon and the printing mechanism of MFC, and proposes a particularly simple method. You can solve this problem in just a few lines of code in the original procedure. The resulting print.

First, analyze the print mechanism of the MFC, clear the principle, and it is not difficult to understand the cause of the phenomenon and the solution. The core of the MFC application is the concept of document objects and related view windows, which is the composition and relationship of CDocument classes, and CVIEW classes, simply saying that the cDocument class is responsible for data generation and storage, and the CView class is responsible for data display and user interaction. Output to the screen and output to the printer are the display of data, which is essentially the same, so the print function is also implemented by the CView class.

The source code automatically generated by the application wizard in the CView class provides an onDraw (CDC * PDC) function, by overloading this function, using the PDC (Device Context) pointer it provides, can display various graphics on the screen And data. The printing of the CVIEW class is implemented by the function of Onprint (CDC * PDC, CPrintInfo * PINFO). The application wizard automatically generates a framework in the source code of the application guide, and the implementation of this function is simple to call OnDRAW (CDC) * PDC) This function passes the printer's device context pointer PDC to the OnDRAW (CDC * PDC) function.

It can be seen that the CView class is the same for the output to the screen and output to the printer, but only changed a device context, then why is the image to the printer is particularly small?

This is related to the default coordinate mapping mode MM_Text used by the VC. The advantage of this approach is that the user graphics coordinates and the pixels of the device are exactly the same. However, when the pixel size of the screen is 800 * 600, the screen pixels included in each logical inch are 96, while the printer's point is several times, such as the number of printer points contained in each logic inch when the printer is HP LaserJet 6L. 600, that is, the clarity of the printer is much higher than the screen.

Such consequences are full screen images displayed on the screen, but only a little big, how to solve this problem? A simple method is to convert coordinate mapping mode, so that this problem can solve this problem with the coordinate ratio used in printing than the coordinate ratio used.

A detailed approach will be given below.

Note that the CView class calls Virtual Void OnPrePAREDC (CDC * PDC, CPRINTINFO * PINFO = NULL) before performing display and printing, and we can overload this virtual member function in the CView class, coordinate Conversion.

First, the overpreparedc (CDC * PDC, CPRINFO * PINFO = NULL) function is implemented using the ClassWizard, and 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 in the source code, as follows:

Void CPrintsameView :: onpreparedc

(CDC * PDC, CPRINTINFO * PINFO) {CView :: onpreparedc (PDC, PINFO);

PDC-> setmapmode (mm_anisotropic); // Convert coordinate mapping method

CSIZE SIZE = CSIZE (800, 560);

PDC-> setWindowext (size);

// Determine the window size // Get the number of pixels per logical inch in the actual device int xlogpixperinch = PDC-> getDeviceCaps (LogPixelsx);

INT YLOGPIXPERINCH = PDC-> GetDeviceCaps (Logpixelsy);

// Get the proportion of equipment coordinates and logical coordinates, long xext = (long) size.cx * xlogpixperinch / 96;

Long Yext = (long) size.cy * YLOGPIXPERINCH / 96;

PDC-> SetViewPortext (INT) Xext, (int) Yext);

// Determine the viewport size} As shown above, the coordinate mapping mode is first changed to mm_anisotropic mode, that is, the anisotropy means that in this coordinate mode, the logical unit of the X-axis and Y axes can be arbitrarily scaled. After changing the coordinate matrix mode, you must determine the size of the window and the viewport size. Note that the window size is the size you see on the screen, and the viewport size is actual device, such as printers, etc., and monitor devices per logical inch. The proportional size of the amount is compared. Get the number of pixels per logical inch of the display and printer, then scales the viewport size, so that the display on the screen and the output of the printer are consistent.

In this way, only the simple code is passed through a few lines, we have achieved printing.

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

New Post(0)