11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12. Screen Capturing & Printing<br />

(0, 0) (m, 0)<br />

(0, 0)<br />

(m, 0)<br />

y<br />

(0, n) (m, n)<br />

MM_TEXT mode<br />

y<br />

(0, -n) (m, -n)<br />

Other modes<br />

Figure 12-5. The direction of y axis is different for different mapping modes<br />

Converting between Logical and Device Units<br />

Sometimes we need to implement the conversion between logical unit and actual device unit. Class CDC<br />

has a bunch of functions that allow us to do the conversion between two coordinate systems. For example,<br />

CDC::LPtoDP(…) allows us to convert a point (POINT type variable) or a size (CSize type variable) measured<br />

in logical unit coordinate system to device coordinate system. And CDC::DPtoLP(…) does the reverse.<br />

Implementing Print<br />

Actually it is easy to implement printing for applications generated from the Application Wizard.<br />

When the user executes File | Print or File | Print Preview command, a series of printing messages will be<br />

sent to the application. Upon receiving these messages, the frame window finds out the current active view,<br />

and call that view’s CView::OnPrint(…) function to output drawings to the target device.<br />

By default, CView::OnPrint(…) does nothing but calling function CView::OnDraw(…), so everything<br />

contained in the client window (view) will also be output to the printer. We can experiment this with the<br />

sample already implemented. For example, after executing sample 12.2\GDI, if we make a snapshot and<br />

execute File | Print command, the captured image will be sent to the printer. The actual size of the output<br />

image depends on the type of printer because in CGDIView::OnDraw(…), we didn’t set the mapping mode so<br />

the default mode MM_TEXT is used.<br />

We must know the resolution of the target device so that we can either scale the output to let it fit into<br />

the device or we can manage to print one image on separate papers. One way of obtaining the target<br />

device’s resolution is to call function CDC::GetDeviceCap(…) using HORZRES and VERTRES parameters. The<br />

returned value of this function will be the horizontal or vertical resolution of the target device, measured in<br />

its device unit. Besides this, we can also use LOGPIXELSX and LOGPIXELSY to retrieve the number of pixels<br />

per logical inch in the target device for both horizontal and vertical directions. Since the width and height<br />

of a minimum pixel in the target device may not be the same, the above two parameters may be different.<br />

Scaling the Image before Printing<br />

Sample 12.3\GDI is based on sample 12.2\GDI. In this sample, when doing the printing, the output is<br />

scaled so that it can fit within one sheet of paper no matter what the resolution of target device is. In order<br />

to implement this, we need to calculate the proportion between the logical unit and the physical unit of the<br />

target device before the image is output to the device.<br />

For example, suppose we have an image whose logical dimension is x×y, and want to output it to the<br />

target device whose physical resolution is px×py. We further assume that the aspect ratio of a minimum<br />

physical pixel on the target device is rx : ry. This is illustrated by Figure 12-6:<br />

380

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

Saved successfully!

Ooh no, something went wrong!