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 Main Window Object The main window class is CMainFrame, and the support files for this class are MainFrm.h and MainFrm.cpp. You can see how the main window is built in the CMainFrame::OnCreate() function. Here, the window’s toolbar and status bars are created and added to the window.
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
         return -1;

        if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE |
    CBRS_TOP
         | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
        {
         TRACE0(“Failed to create toolbar\n”);
         return -1;      // fail to create
        }

        if (!m_wndStatusBar.Create(this) ||
         !m_wndStatusBar.SetIndicators(indicators,
           sizeof(indicators)/sizeof(UINT)))
        {
         TRACE0(“Failed to create status bar\n”);
         return -1;      // fail to create
        }

        // TODO: Delete these three lines if you don’t want the toolbar to
        //  be dockable
        m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
        EnableDocking(CBRS_ALIGN_ANY);
        DockControlBar(&m_wndToolBar);

        return 0;
    }
Another useful function is CMainFrame::PreCreateWindow(), because you can change the window’s class before it is first displayed.
    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
        if( !CFrameWnd::PreCreateWindow(cs) )
         return FALSE;
        // TODO: Modify the Window class or styles here by modifying
        //  the CREATESTRUCT cs

        return TRUE;
    }
We’ll see how to change the cursor our window uses later in this book by modifying this function. The MainFrame class is a good one to know, but the real meat of the program comes in the document and view classes, which we’ll turn to now. The listing for MainFrm.h and MainFrm.cpp appear in Listing 1.3. Listing 1.3 MainFrm.h and MainFrm.cpp
    // MainFrm.h : interface of the CMainFrame class
    //
    /////////////////////////////////////////////////////////////////////////////

    #if
    !defined(AFX_MAINFRM_H__701B1AD9_9BB7_11D1_887F_D42B07C10710__INCLUDED_)
    #define AFX_MAINFRM_H__701B1AD9_9BB7_11D1_887F_D42B07C10710__INCLUDED_

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

    class CMainFrame : public CFrameWnd
    {
    protected: // create from serialization only
        CMainFrame();
        DECLARE_DYNCREATE(CMainFrame)

    // Attributes
    public:

    // Operations
    public:

    // Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CMainFrame)
        virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
        //}}AFX_VIRTUAL

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

    protected:  // control bar embedded members
        CStatusBar  m_wndStatusBar;
        CToolBar    m_wndToolBar;

    // Generated message map functions
    protected:
        //{{AFX_MSG(CMainFrame)
        afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
         // 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_MAINFRM_H__701B1AD9_9BB7_11D1_887F_D42B07C10710__INCLUDED_)

    // MainFrm.cpp : implementation of the CMainFrame class
    //

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

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

    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame

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

    static UINT indicators[] =
    {
        ID_SEPARATOR,           // status line indicator
        ID_INDICATOR_CAPS,
        ID_INDICATOR_NUM,
        ID_INDICATOR_SCRL,
    };

    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame construction/destruction

    CMainFrame::CMainFrame()
    {
        // TODO: add member initialization code here

    }

    CMainFrame::~CMainFrame()
    {
    }

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
         return -1;

        if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE |
    CBRS_TOP
         | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
        {
         TRACE0(“Failed to create toolbar\n”);
         return -1;      // fail to create
        }

        if (!m_wndStatusBar.Create(this) ||
         !m_wndStatusBar.SetIndicators(indicators,
           sizeof(indicators)/sizeof(UINT)))
        {
         TRACE0(“Failed to create status bar\n”);
         return -1;      // fail to create
        }

        // TODO: Delete these three lines if you don’t want the toolbar to
        //  be dockable
        m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
        EnableDocking(CBRS_ALIGN_ANY);
        DockControlBar(&m_wndToolBar);

        return 0;
    }

    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
        if( !CFrameWnd::PreCreateWindow(cs) )
         return FALSE;
        // TODO: Modify the Window class or styles here by modifying
        //  the CREATESTRUCT cs

        return TRUE;
    }

    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame diagnostics

    #ifdef _DEBUG
    void CMainFrame::AssertValid() const
    {
        CFrameWnd::AssertValid();
    }

    void CMainFrame::Dump(CDumpContext& dc) const
    {
        CFrameWnd::Dump(dc);
    }

    #endif //_DEBUG
    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame message handlers
When you work with a Visual C++ program, you mainly work with the document and view objects, as we have already done in our KeysSDI program. Let’s turn to these objects now, beginning with the document object. ### The Document Object The document and view objects in a Visual C++ program have a special relationship. The document object stores the program’s data, and the view object displays it. The original idea was to make it easy to store and retrieve the data to and from disk (we’ll cover file handling in Chapter 5, “Real-World Programming: The Editor Application”), but this separation of data storage and display has come to seem natural to Visual C++ programmers. In our KeysSDI program, we’ve placed the string that the user has typed in an MFC CString object named text. Public versus Private Data Note that although we’ve made the text object public here so we can reach it from the view object, we could have declared it private or protected and let other objects reach it only through the use of get and set functions, such as GetText() and SetText(). The benefit of restricting access this way is that it prevents unintentional changes to the text object, which is why the MFC classes often insist you use get and set functions instead of giving you direct access to internal data.
     -----------------------
    | Document Object      |
    |                      |
    |                      |
    |         text         |
    |                      |
     -----------------------
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.