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

IDR_CHARTTYPE,<br />

RUNTIME_CLASS(CChartDoc),<br />

RUNTIME_CLASS(CChildFrame),<br />

RUNTIME_CLASS(CChartView));<br />

AddDocTemplate(pDocTemplate);<br />

The constructor of class CMultiDocTemplate has four parameters, the first of which is a string resource<br />

ID, which comprises several sub-strings for specifying the default child window title, document type, and<br />

so on. The rest three parameters must use RUNTIME_CLASS macro, and we can use the appropriate class<br />

name as its parameter. In the code listed above, the child window uses class CChildFrame to create the<br />

frame window, and uses CChartView to create the client window. The child window is attached to the<br />

document implemented by class CChartDoc.<br />

Attaching Multiple Views to One Document<br />

If we need only one type of view, this is enough. However, if we want to attach multiple views to a<br />

single document, we can call function CWinApp::AddDocTemplate(…) again to bind a new type of view to<br />

the document.<br />

Sample 13.3\Chart<br />

Sample 13.3\Chart demonstrates how to attach multiple views to one document. It is a standard MDI<br />

application generated by Application Wizard. The purpose of this application is to interpret data stored in<br />

the document in different ways. The original classes generated by Application Wizard are CChartApp,<br />

CChartDoc, CChartView, CMainFrame and CChildFrame. After the application skeleton is generated, a new<br />

class CPieView (derived from CView) is add to the application through using Class Wizard.<br />

Data stored in the document is very simple, there are three variables declared in class CChartDoc:<br />

CChartDoc::m_nA, CChartDoc::m_nB and CChartDoc::m_nC. The variables are initialized in the constructor<br />

as follows:<br />

CChartDoc::CChartDoc()<br />

{<br />

m_nA=20;<br />

m_nB=70;<br />

m_nC=10;<br />

}<br />

Three variables each represents a percentage, so adding them up will result in 100. There are many<br />

different types of charts that can be used to interpret them, two most common ones are “bar chart” and “pie<br />

chart”.<br />

In the sample application, two different types of views are attached to one document, so the user can<br />

use either “Bar chart” or “Pie chart” to view the data. To obtain data from the document, function<br />

CChartDoc::GetABC(…) is implemented to let these values be accessible in the attached views.<br />

In function CChartView::OnDraw(…), three bars are drawn using different colors, their heights<br />

represent the percentage of three member variables. For class CPieView, three pies are drawn in different<br />

colors and they form a closed circle, whose angles represent different percentages.<br />

Two views are attached to the document in function CChartApp::InitInstance(). Besides the<br />

standard implementation, a new document template is created and added to the application. The following<br />

portion of function CChartApp::InitInstance() demonstrates how the two views are attached to the same<br />

document:<br />

……<br />

CMultiDocTemplate* pDocTemplate;<br />

pDocTemplate = new CMultiDocTemplate(<br />

IDR_CHARTTYPE,<br />

RUNTIME_CLASS(CChartDoc),<br />

RUNTIME_CLASS(CChildFrame),<br />

RUNTIME_CLASS(CChartView));<br />

AddDocTemplate(pDocTemplate);<br />

398

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

Saved successfully!

Ooh no, something went wrong!