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 />

pDC->GetDeviceCaps(LOGPIXELSX),<br />

-pDC->GetDeviceCaps(LOGPIXELSY)<br />

);<br />

pDC->SetViewportOrg(rect.right/2, rect.bottom/2);<br />

rect=CRect(-100, 100, 100, -100);<br />

ptStart.x=rect.right;<br />

ptStart.y=0;<br />

ptEnd.x=(long)(r*cos(nA*2*pi/100));<br />

ptEnd.y=(long)(r*sin(nA*2*pi/100));<br />

brush.CreateSolidBrush(RGB(255, 0, 0));<br />

pBrOld=pDC->SelectObject(&brush);<br />

pDC->Pie(rect, ptStart, ptEnd);<br />

pDC->SelectObject(pBrOld);<br />

brush.DeleteObject();<br />

ptStart=ptEnd;<br />

ptEnd.x=(long)(r*cos((nA+nB)*2*pi/100));<br />

ptEnd.y=(long)(r*sin((nA+nB)*2*pi/100));<br />

brush.CreateSolidBrush(RGB(0, 255, 0));<br />

pBrOld=pDC->SelectObject(&brush);<br />

pDC->Pie(rect, ptStart, ptEnd);<br />

pDC->SelectObject(pBrOld);<br />

brush.DeleteObject();<br />

}<br />

ptStart=ptEnd;<br />

ptEnd.x=(long)(r*cos((nA+nB+nC)*2*pi/100));<br />

ptEnd.y=(long)(r*sin((nA+nB+nC)*2*pi/100));<br />

brush.CreateSolidBrush(RGB(255, 255, 0));<br />

pBrOld=pDC->SelectObject(&brush);<br />

pDC->Pie(rect, ptStart, ptEnd);<br />

pDC->SelectObject(pBrOld);<br />

brush.DeleteObject();<br />

In the above code, first MM_ISOTROPIC mode is set. Then the window extents is set to (100, 100). To<br />

map one logical unit to an absolute size, function CDC::GetDevice(…) is called using both LOGPIXELSX and<br />

LOGPIXELSY parameters. This will cause the function to return the number of pixels per logical inch in both<br />

horizontal and vertical directions. Then we use the returned values to set view port extents. This will cause<br />

100 logical units to be mapped to 1 inch in both horizontal and vertical directions. By doing this, no matter<br />

where we run the program, the output will be the same dimension.<br />

When calling function CDC::SetViewportExt(…), we set the vertical extent to a negative value. This<br />

will change the orientation of the y-axis so that the positive values locate at the upper part of the axis (See<br />

Figure 13-3).<br />

Next, function CDC::SetViewportOrg(…) is called to set the device origin to the center of the window.<br />

This will simplify the calculation of starting and ending points when drawing pies.<br />

13.4 Multiple Documents Implementation<br />

Not only can we implement an application with more than one type of views, but also implement an<br />

application that supports more than one type of documents. For example, generally a graphic editor needs<br />

to support several types of image files, such as bitmap and GIF files. Although we can support all file<br />

formats within one document, for MDI applications, the source code will become easy to manage if we use<br />

one document type to support one file format.<br />

Actually, the procedure of implementing more than one document is almost the same with adding more<br />

than one view to an application. All we need to modify is still function CWinApp::InitInstance(), within<br />

which we must create a new document template and call function CWinApp::AddDocTemplate(…) to bind<br />

the new document (along with a view) to the mainframe window.<br />

From the sample application created in the previous section, we know that when a new document<br />

template is being created, we need to provide a resource ID, a document class name, a view class name, and<br />

a frame window class name. If we look at the menu and icon resources of an MDI application, we will find<br />

that ID IDR_MAINFRAME is used in three different places: there is a string resource using this ID, which will<br />

401

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

Saved successfully!

Ooh no, something went wrong!