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
Here, we accept new keys from the user and store those keys in the document. In particular, we get a pointer to the document object using the GetDocument() function, and then add the current character to the text string with the following line:
    void CKeysSDIView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        CKeysSDIDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        pDoc->text += nChar;                                          ⇐

        CView::OnChar(nChar, nRepCnt, nFlags);

        Invalidate();
    }
At the end of OnChar(), we call Invalidate() to invalidate the view, which means the program will call our OnDraw() function. In that function, we’ve placed code to display the text in the text object, starting at (0, 0) in the view (in an SDI program, the view covers the main window’s client area, which is the area of the window excluding borders, toolbars, menus, and title bar).
    void CKeysSDIView::OnDraw(CDC* pDC)
    {
        CKeysSDIDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        pDC->TextOut(0, 0, pDoc->text);                                 ⇐
    }
In this way, the view object handles two tasks: interacting with the user and displaying data. As we’ll see throughout the book, that’s just what the view usually does. The view object’s support files, KeysSDIView.h and KeysSDIView.cpp, appear in Listing 1.5. Listing 1.5 KeysSDIView.h and KeysSDIView.cpp
    // KeysSDIView.h : interface of the CKeysSDIView class
    //
    /////////////////////////////////////////////////////////////////////////////

    #if
    !defined(AFX_KEYSSDIVIEW_H__701B1ADD_9BB7_11D1_887F_D42B07C10710__INCLUDED_)
    #define AFX_KEYSSDIVIEW_H__701B1ADD_9BB7_11D1_887F_D42B07C10710__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    class CKeysSDIView : public CView
    {
    protected: // create from serialization only
        CKeysSDIView();
        DECLARE_DYNCREATE(CKeysSDIView)

    // Attributes
    public:
        CKeysSDIDoc* GetDocument();

    // Operations
    public:

    // Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CKeysSDIView)
        public:
        virtual void OnDraw(CDC* pDC);  // overridden to draw this view
        virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
        protected:
        virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
        virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
        virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
        //}}AFX_VIRTUAL

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

    protected:
    // Generated message map functions
    protected:
        //{{AFX_MSG(CKeysSDIView)
        afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
    };

    #ifndef _DEBUG  // debug version in KeysSDIView.cpp
    inline CKeysSDIDoc* CKeysSDIView::GetDocument()
       { return (CKeysSDIDoc*)m_pDocument; }
    #endif

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

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

    #endif //
    !defined(AFX_KEYSSDIVIEW_H__701B1ADD_9BB7_11D1_887F_D42B07C10710__INCLUDED_)

    // KeysSDIView.cpp : implementation of the CKeysSDIView class
    //

    #include “stdafx.h”
    #include “KeysSDI.h”

    #include “KeysSDIDoc.h”
    #include “KeysSDIView.h”

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

    /////////////////////////////////////////////////////////////////////////////
    // CKeysSDIView

    IMPLEMENT_DYNCREATE(CKeysSDIView, CView)

    BEGIN_MESSAGE_MAP(CKeysSDIView, CView)
        //{{AFX_MSG_MAP(CKeysSDIView)
        ON_WM_CHAR()
        //}}AFX_MSG_MAP
        // Standard printing commands
        ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
        ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
        ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CKeysSDIView construction/destruction

    CKeysSDIView::CKeysSDIView()
    {
        // TODO: add construction code here
    }

    CKeysSDIView::~CKeysSDIView()
    {
    }

    BOOL CKeysSDIView::PreCreateWindow(CREATESTRUCT& cs)
    {
        // TODO: Modify the Window class or styles here by modifying
        //  the CREATESTRUCT cs

        return CView::PreCreateWindow(cs);
    }

    /////////////////////////////////////////////////////////////////////////////
    // CKeysSDIView drawing

    void CKeysSDIView::OnDraw(CDC* pDC)
    {
        CKeysSDIDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        pDC->TextOut(0, 0, pDoc->text);
    }

    /////////////////////////////////////////////////////////////////////////////
    // CKeysSDIView printing

    BOOL CKeysSDIView::OnPreparePrinting(CPrintInfo* pInfo)
    {
        // default preparation
        return DoPreparePrinting(pInfo);
    }

    void CKeysSDIView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
        // TODO: add extra initialization before printing
    }

    void CKeysSDIView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
        // TODO: add cleanup after printing
    }

    /////////////////////////////////////////////////////////////////////////////
    // CKeysSDIView diagnostics

    #ifdef _DEBUG
    void CKeysSDIView::AssertValid() const
    {
        CView::AssertValid();
    }

    void CKeysSDIView::Dump(CDumpContext& dc) const
    {
        CView::Dump(dc);
    }

    CKeysSDIDoc* CKeysSDIView::GetDocument() // non-debug version is inline
    {
        ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CKeysSDIDoc)));
        return (CKeysSDIDoc*)m_pDocument;
    }
    #endif //_DEBUG

    /////////////////////////////////////////////////////////////////////////////
    // CKeysSDIView message handlers

    void CKeysSDIView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        CKeysSDIDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        pDoc->text += nChar;
        CView::OnChar(nChar, nRepCnt, nFlags);
        Invalidate();
    }
### What’s Ahead In the next chapter, we’ll start digging into fresh material. We’ve worked with two of the three kinds of EXE projects in this chapter: dialog-based and SDI programs. In the next chapter, we’ll start working with MDI programs and supporting multiple windows and documents.
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.