Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Fast Track Visual C++ 6.0 Programming (Publisher: John Wiley & Sons, Inc.) Author(s): Steve Holzner ISBN: 0471312908 Publication Date: 09/01/98 Search this book:
Previous | Table of Contents | Next
The new version of the second page appears in Print Preview in Figure 5.5. Now we’re able to set device context characteristics by page. This is very useful if you have several pages to print, because you can move the origin of the printing device context in OnPrepareDC() using the SetViewOrg() function to the beginning of the page about to be printed. Figure 5.5 Setting font size and type by printed page in Editor. We can also use the OnPrint() function. This function is called when the page is printed, and you can override it to call OnDraw() yourself. For example, we might add a header to each page that is printed, displaying the current page’s page number. #### Printing a Header on Each Page We create that header in a text string using the m_nCurPage member of the CPrintInfo object.
    void CEditorView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
    {
        char header[20];

        wsprintf(header, “Page %ld”, pInfo->m_nCurPage);
         .
         .
         .
Then we display the header in the page to be printed.
    void CEditorView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
    {
        char header[20];

        wsprintf(header, “Page %ld”, pInfo->m_nCurPage);

        pDC->TextOut(0, 0, header, strlen(header));               ⇐
         .
         .
         .
    }
Finally, we call OnDraw() to print the rest of the page.
    void CEditorView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
    {
        char header[20];

        wsprintf(header, “Page %ld”, pInfo->m_nCurPage);

        pDC->TextOut(0, 0, header, strlen(header));

        OnDraw(pDC);                                ⇐
        CView::OnPrint(pDC, pInfo);                 ⇐
    }
A page with a header about to be printed appears in Print Preview in Figure 5.6. Our printing work is a success. That covers the Editor program. EditorDoc.h and EditorDoc.cpp appear in Listing 5.1, and EditorView.h and EditorView.cpp appear in Listing 5.2. Figure 5.6 Adding a header to printed pages in Editor. Listing 5.1 EditorDoc.h and EditorDoc.cpp
    // EditorDoc.h : interface of the CEditorDoc class
    //
    /////////////////////////////////////////////////////////////////////////////
    #if
    !defined(AFX_EDITORDOC_H__3FD89C6D_A149_11D1_887F_D42B07C10710__INCLUDED_)
    #define AFX_EDITORDOC_H__3FD89C6D_A149_11D1_887F_D42B07C10710__INCLUDED_
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    class CData : public CObject {
    private:
        CString m_text;
        int m_x, m_y;
        DECLARE_SERIAL(CData);
    public:
        CData() {m_text = CString(””); m_x = m_y = 0;}
        CString GetText() {return m_text;}
        void SetText(CString new_text) {m_text = new_text;}
        int GetX() {return m_x;}
        void SetX(int new_x) {m_x = new_x;}
        int GetY() {return m_y;}
        void SetY(int new_y) {m_y = new_y;}
        void Serialize(CArchive& archive);
    };

    class CEditorDoc : public CDocument
    {
    protected: // create from serialization only
        CEditorDoc();
        DECLARE_DYNCREATE(CEditorDoc)

    // Attributes
    public:
        CData m_data;
    // Operations
    public:
    // Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CEditorDoc)
        public:
        virtual BOOL OnNewDocument();
        virtual void Serialize(CArchive& ar);
        //}}AFX_VIRTUAL

    // Implementation
    public:
        virtual ~CEditorDoc();
    #ifdef _DEBUG
        virtual void AssertValid() const;
        virtual void Dump(CDumpContext& dc) const;
    #endif

    protected:
        CString m_text;
    // Generated message map functions
    protected:
        //{{AFX_MSG(CEditorDoc)
         // NOTE - the ClassWizard will add and remove member functions here.

         //    DO NOT EDIT what you see in these blocks of generated code !
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
    };

    /////////////////////////////////////////////////////////////////////////////

    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_EDITORDOC_H__3FD89C6D_A149_11D1_887F_D42B07C10710__INCLUDED_)

    // EditorDoc.cpp : implementation of the CEditorDoc class
    //

    #include “stdafx.h”
    #include “Editor.h”

    #include “EditorDoc.h”

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    /////////////////////////////////////////////////////////////////////////////
    // CEditorDoc

    IMPLEMENT_DYNCREATE(CEditorDoc, CDocument)

    BEGIN_MESSAGE_MAP(CEditorDoc, CDocument)
        //{{AFX_MSG_MAP(CEditorDoc)
         // NOTE - the ClassWizard will add and remove mapping macros here.
         //    DO NOT EDIT what you see in these blocks of generated code!
        //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CEditorDoc construction/destruction

    CEditorDoc::CEditorDoc()
    {
        // TODO: add one-time construction code here
    }

    CEditorDoc::~CEditorDoc()
    {
    }

    BOOL CEditorDoc::OnNewDocument()
    {
        if (!CDocument::OnNewDocument())
         return FALSE;

        // TODO: add reinitialization code here
        // (SDI documents will reuse this document)

        return TRUE;
    }

    /////////////////////////////////////////////////////////////////////////////
    // CEditorDoc serialization

    void CEditorDoc::Serialize(CArchive& ar)
    {
        m_data.Serialize(ar);
    }

    /////////////////////////////////////////////////////////////////////////////
    // CEditorDoc diagnostics

    #ifdef _DEBUG
    void CEditorDoc::AssertValid() const
    {
        CDocument::AssertValid();

    }

    void CEditorDoc::Dump(CDumpContext& dc) const
    {
        CDocument::Dump(dc);
    }
    #endif //_DEBUG

    /////////////////////////////////////////////////////////////////////////////
    // CEditorDoc commands
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.