Foundation Classes Simplify C/C++ Programming --Code Listing 6
Staff -- Test & Measurement World, 2/1/2000
![]() |
|
Global TMW:
|
![]() |
|
| Listing 6 Saving to Disk As advertised, MFC makes storage to disk rather simple. Provided you started with either the SDI or MDI AppWizard frameworks, you already have menu selections for Open, Save, and Save As. These menu functions are already wired into the CDocument derived class. All you have to do is add in your CDocument data members to the serialization routines found in the default TestAppDoc.cpp file. void CTestAppDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { ar << m_SerialNumber << m_ModelNumber; } else { ar >> m_SerialNumber >> m_ModelNumber; } m_Results.Serialize(ar); // this guy can serialize itself } Again, compile and run, modify something on the view, and save the file. Exit, run again, and load the file, voila, the view returns to where you left off. Printing and Print Preview Once again the framework is here for you, but you need to fill in some blanks. The printing and print preview functions are part of your view class. Some of the CView derived classes, such as CEditView, already know how to render the view onto the printer because there is really only one way to do it. However, since we chose CFormView, we must render the information ourselves, much like we had to serialize the CDocument data members in the previous section. At a minimum, you must edit the OnPrint member function of the view class. The following code represents a minimal implementation of OnPrint. void CTestAppView::OnPrint(CDC* pDC, CPrintInfo* pInfo) { TEXTMETRIC tm; int y, cy, deltay, cx; CFont font; CTestAppDoc* pDoc = GetDocument(); // get a local pointer for convenience pDC->SetMapMode(MM_TWIPS); // set mapping mode to something useful font.CreateFont(-280,0,0,0,400,FALSE,FALSE,0, ANSI_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, DEFAULT_PITCH|FF_MODERN,"Courier New");
CFont* pOldFont = (CFont*) (pDC->SelectObject(&font));
pDC->GetTextMetrics(&tm); deltay = tm.tmHeight+tm.tmExternalLeading; cx = 1000; // left margin cy = -1000; // top margin // print a header pDC->TextOut(cx,cy,"Test Report"); // print the rest cy-=deltay; for (y=0;y { pDC->TextOut(cx,cy-=deltay,pDoc->m_Results.GetAt(y)); } pDC->SelectObject(pOldFont); } As shown, the code only supports a single page of printing. A few more lines would be needed to paginate and number the pages. After adding the code above, compile and run. Run the test to get some strings in the listbox, and then do a print preview. Go ahead and print as well. You should be able to select any printer, networked or local. It's all set. |
|
SPONSORED LINKS |