11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12. Screen Capturing & Printing<br />

12.3 Simple Printing<br />

Although we didn’t write a single line of code to implement printing feature, all our SID or MDI<br />

samples have the default printing functionality. This includes default printer set up, print preview, and<br />

printing the client window. Like display, printer is another type of graphic device that can be used to output<br />

drawings. Its interface to the software programmer is similar to that of display: instead of writing code to<br />

control the hardware directly, we can use DC to output drawings to the printers. Actually we can call<br />

member functions of class CDC to output dot, line, curve, rectangle and bitmaps to a printer.<br />

Mapping Mode<br />

However, there are some differences between printing devices and display devices. One main<br />

difference is that two devices may have different capabilities. Because all displays have similar sizes and<br />

resolutions, it is relatively convenient to measure everything on the screen using pixel. For example, it<br />

doesn’t make much difference if we display a 256×256 bitmap on an 800×600 display or a 1024×768<br />

display. Since every window is able to display an object that is larger than the dimension of its client area<br />

(using scroll bars), it is relatively easy to base every drawing on the minimum possible unit ⎯ pixel.<br />

For printers, this is completely different. There are many types of printers in the world, whose<br />

resolutions are remarkably different from one another. For example, there are line printers, one pixel on this<br />

kind of printers may be 0.1mm×0.1mm; also, there are many types of laser printers, whose resolution can<br />

be 600dpi, 800dpi or even denser. If we display a 256×256 image on the two types of printers, their sizes<br />

will vary dramatically.<br />

Anther difference between printing devices and display devices is that when doing the printing, it is<br />

desirable to make sure that all the outputs fit within the device. For a window, since we can customize the<br />

total scroll size, it doesn’t matter what the actual output dimension is (The scroll size can always be set to<br />

the dimension of output drawings). For the printer, we need to either scale the output to let it fit within the<br />

device or print the output on separate papers.<br />

In order to handle this complicated situation, under Windows, OS and devices have some common<br />

agreements. When we draw a dot, copy a bitmap to device, everything is actually based on logical units<br />

(pixels). By default, the size of one logical unit is mapped to one minimum physical pixel on the device,<br />

however, this can be changed. Actually class CDC has a function that let us customize it:<br />

virtual int CDC::SetMapMode(int nMapMode);<br />

Parameter nMapMode specify how to map one logical unit to physical units of the target device. By<br />

default it is set to MM_TEXT, which maps one logical unit to one physical unit. It can also be set to one of the<br />

following parameters:<br />

Parameter<br />

Meaning<br />

MM_HIENGLISH One logical unit is mapped to 0.001 inch in the target device<br />

MM_HIMETRIC One logical unit is mapped to 0.01 millimeter of the target device<br />

MM_LOENGLISH One logical unit is mapped to 0.01 inch of the target device<br />

MM_LOMETRIC One logical unit is mapped to 0.1 millimeter of the target device<br />

MM_TWIPS One logical unit is mapped to 1/1440 inch<br />

Instead of mapping one logical unit to a fixed number of pixels, it is mapped to a fixed size on the<br />

target device. It is the device driver’s task to figure out the actual number of pixels that should be used for<br />

drawing one logical pixel. By doing this type of mappings, the output will have the same dimension no<br />

matter what type of target device we use.<br />

There is one difference between MM_TEXT mapping mode and other modes: for MM_TEXT mode, the<br />

positive y axis points downward. For other mapping modes, the positive y axis points upward. So if we<br />

decide to use one of the mapping mode listed above, and the origin of the bitmap is still the same, we need<br />

to use negative values to reference a pixel’s vertical position (Figure 12-5).<br />

379

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!