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 13. Adding Special Features to Application<br />

……<br />

pDocTemplate = new CMultiDocTemplate(<br />

IDR_PIETYPE,<br />

RUNTIME_CLASS(CChartDoc),<br />

RUNTIME_CLASS(CChildFrame),<br />

RUNTIME_CLASS(CPieView));<br />

AddDocTemplate(pDocTemplate);<br />

With the above implementation, when a new client is about to be created, the user will be prompted to<br />

choose from one of the two types of views. Here strings that are included in the prompt dialog box should<br />

be prepared as sub-strings contained in the string resources that are used to identify document type<br />

(IDR_CHARTTYPE and IDR_PIETYPE in the sample).<br />

The format of the document type string is the same with that of a normal MDI application, which<br />

comprises several sub-strings. The most important ones are the first two sub-strings: one will be used as the<br />

default title of the client window and the other will be used in the prompt dialog box to let the user select an<br />

appropriate view when a new client window is about to be created. In the sample, the contents of string<br />

IDR_CHARTYPE and IDR_PIETYPE are as follows:<br />

\nBar\nBar\n\n\nChart.Document\nChart Document<br />

\nPie\nPie\n\n\nChart.Document\nChart Document<br />

Both the window and the string contained in the prompt dialog box for chart view are set to “Bar”, for<br />

pie view, they are set to “Pie”.<br />

Window Origin and View Port Origin<br />

When implementing drawing on the target device, sometimes it is more convenient if we use<br />

appropriate coordinate system. As a programmer, when we write code to draw geometrical shapes, we are<br />

always working on Page-Space (logical space). The actual output would happen on Device-Space. By<br />

default, one logical unit is mapped to one device unit, and both origins are located at upper-left corners.<br />

When drawing certain types of geometrical shapes, for example, a circle, it would be more convenient<br />

if we adjust the origin of the coordinate system so that it is located at the center of the circle (See Figure<br />

13-3).<br />

Describing this circle<br />

is difficult, because its<br />

coordinates are not<br />

symmetric<br />

Figure 13-3. Two types of coordinate systems<br />

To offset origin in either page-space or device-space, we can use the following functions:<br />

Function Name<br />

CDC::SetWindowOrg(…)<br />

CDC::OffsetWindowOrg(…)<br />

CDC::SetViewportOrg(…)<br />

CDC::OffsetViewportOrg(…)<br />

Usage<br />

Set origin to a specific position in page-space<br />

Offset origin to a new position in page-space<br />

Set origin to a specific position in device-space<br />

Offset origin to a new position in device-space<br />

399

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

Saved successfully!

Ooh no, something went wrong!