Adding the Helper Functions
The next step is to add the new helper functions. By using the Add Function pop-up menu command in ClassView, you can declare and define them in one step.
As mentioned, the PrintTitlePage
function prints a title page, and PrintPageHeader
prints a header on the drawing page.
To add the PrintTitlePage helper function
In the ClassView, point to
CScribbleView
and click the right mouse button.From the pop-up menu, click the Add Function command.
In the Function Type box, type the return type of the function (in this case,
void
).In the Function Declaration box, type the following:
PrintTitlePage(CDC* pDC, CPrintInfo* pInfo)
In the Access area, select Public, and click OK.
ClassWizard adds the declaration to the Public section of the header file and creates a starter definition in the implementation file.
Expand
CScribbleView
and double-clickPrintTitlePage
to jump to the body of the definition so you can begin typing your application-specific code.Fill in the starter
PrintTitlePage
function with the following code:// Prepare a font size for displaying the file name LOGFONT logFont; memset(&logFont, 0, sizeof(LOGFONT)); logFont.lfHeight = 75; // 3/4th inch high in MM_LOENGLISH CFont font; CFont* pOldFont = NULL; if (font.CreateFontIndirect(&logFont)) pOldFont = pDC->SelectObject(&font); // Get the file name, to be displayed on title page CString strPageTitle = GetDocument()->GetTitle(); // Display the file name 1 inch below top of the page, // centered horizontally pDC->SetTextAlign(TA_CENTER); pDC->TextOut(pInfo->m_rectDraw.right/2, -100, strPageTitle); if (pOldFont != NULL) pDC->SelectObject(pOldFont);
The
PrintTitlePage
function uses m_rectDraw, which stores the usable drawing area of the page, as the rectangle in which the title should be centered.Notice that
PrintTitlePage
declares a local CFont object to use when printing the title page. If you needed the font for the entire printing process, you could declare a CFont member variable in your view class, create the font in the OnBeginPrinting member function, and destroy it in EndPrinting. However, since Scribble uses the font for just the title page, the font doesn’t have to exist beyond thePrintTitlePage
function. When the function ends, the destructor is automatically called for the local CFont object.
To add the PrintPageHeader helper function
In ClassView, point to
CScribbleView
and click the right mouse button.From the pop-up menu, click the Add Function command.
In the Function Type box, type the return type of the function (in this case,
void
).In the Function Declaration box, type the following:
PrintPageHeader(CDC* pDC, CPrintInfo* pInfo, CString& strHeader)
In the Access area, select Public, and click OK.
Fill in the starter
PrintPageHeader
function with the following code:// Specify left text alignment pDC->SetTextAlign(TA_LEFT); // Print a page header consisting of the name of // the document and a horizontal line pDC->TextOut(0, -25, strHeader); // 1/4 inch down // Draw a line across the page, below the header TEXTMETRIC textMetric; pDC->GetTextMetrics(&textMetric); int y = -35 - textMetric.tmHeight; // line 1/10th in. // below text pDC->MoveTo(0, y); // from left margin pDC->LineTo(pInfo->m_rectDraw.right, y); // to right margin // Subtract from the drawing rectangle the space used by header. y -= 25; // space 1/4 inch below (top of) line pInfo->m_rectDraw.top += y;
The
PrintPageHeader
member function prints the name of the document at the top of the page, and then draws a horizontal line separating the header from the drawing. It adjusts the m_rectDraw member of the pInfo parameter to account for the height of the header; recall thatOnPrint
uses this value to adjust the window origin before it callsOnDraw
.