LIMITS.H and FLOAT.H

Chapter 24 - Application and Class Wizards

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

The Word Processor Application
Here is an application that will allow you to do simple text editing. From the Microsoft Visual C++ main menu bar, select the File menu and then the New menu item. The list box in the New dialog box will allow you to select the MFC AppWizard (exe) option to generate a new project. Remember to enter the name of the project, as shown in Figure 24-17.
Figure 24-17: Starting the Editor project using the AppWizard
The AppWizard will simultaneously create a new subdirectory by the same name beneath the currently selected directory and path. Now you can begin the six-step process that the AppWizard uses to build new applications. In Step 1, shown in Figure 24-18, you can see that this application will permit the user to work with multiple documents.
Figure 24-18: Step 1—Use the Multiple document interface for this project
Figures 24-19 and 24-20 show that no database or compound document support will be included with this project.
Figure 24-19: Step 2—Checkin None indicates that no database support is needed for this project
Figure 24-20: Step 3—This project does not support compound documents but will use ActiveX controls
In Step 4, shown in Figure 24-21, select options that allow this application to include a toolbar, a status bar, 3-D controls, and the ability to print the documents.
Figure 24-21: Step 4—This project uses a toolbar, status bar, and 3-D controls, Documents can also be printed
In Step 5, shown in Figure 24-22, no source code comments were requested and this project’s library option was set to a statically linked library.
Figure 24-22: Step 5—No comments are requested in the source code, A statically linked library is requested
Step 6 shows the five classes that will be created for this application: CEditorApp, CMainFrame, CChildFrame, CEditorDoc, and CEditorView. This final dialog box can be seen in Figure 24-23.
Figure 24-23: Step 6—Five unique classes will be generated to support the Editor application
When CEditorView is selected in the list box, the Base class list box will expand so that you can specify whether you want the CEditorView class to be derived from the CEditView, CFormView, CListView, CRichEditView, CScrollView, CTreeView, or CView base class.
In this example the CEditorView class, derived from the CEditView class, is used to create the base for this application’s user-defined view classes. CEditView describes a class that can be used to develop a simple text editor. After selecting the class, select the Finish button. A summary of the AppWizard’s development process is then shown. Figure 24-24 shows the summary for this example application.
Figure 24-24: A summary of the Editor applications specifications
Select the OK button in this dialog box to start the code-generation process. Figure 24-25 shows the project file list when the AppWizard’s build process is complete.
Figure 24-25: The left pane of this figure shows the various classes that were generated by the AppWizard to support this project
These files will be generated and stored in the subdirectory specified at the start of the project.
  Note The CEditView class supplies the necessary functionality of an edit control. Now your template can print, find and replace, cut, copy, paste, clear, and undo. Since the CEditView class is derived from the CView class, its objects can be used with documents and document templates. As a default, this class handles ID_FILE_PRINT, ID_EDIT_CUT, ID_EDIT_COPY, ID_EDIT_PASTE, ID_EDIT_CLEAR, ID_EDIT_UNDO, ID_EDIT_SELECT_ALL, ID_EDIT_FIND, ID_EDIT_REPLACE, and ID_EDIT_REPEAT.
The Editor application currently being built will eventually use one message handler, but we’re saving the details on that message handler for later!
Building the Application
This application can now be compiled and linked in the normal manner. When the compile and link process has been completed, an executable file will be present in the appropriate subdirectory. Run the application from the Build menu. You should be able to open existing text files or create new files.
Let’s examine the code produced by the AppWizard and discuss several additions we’ve added to the project.
Examining AppWizard Code
The AppWizard generates five C++ files for the initial Editor application. These files are named EDITOR.CPP, MAINFRM.CPP, EDITORDOC.CPP, CHILDFRM.CPP, and EDITORVIEW.CPP. Each of these C++ files has an associated header file: EDITOR.H, MAINFRM.H, EDITORDOC.H, CHILDFRM.H, and EDITORVIEW.H. The header files contain the declarations of the specific classes in each C++ file. The C++ files will be examined in the following sections.
The EDITOR.CPP File
The EDITOR.CPP file serves as the main file for the application. It contains the CEditorApp class. You’ll note that the code we’ve added to this file appears in a bolded font.
// Editor.cpp : Defines the class behaviors for application.
//

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

#include “MainFrm.h”
#include “ChildFrm.h”
#include “EditorDoc.h”
#include “EditorView.h”

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

BEGIN_MESSAGE_MAP(CEditorApp, CWinApp)
 //{{AFX_MSG_MAP(CEditorApp)
 ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
 //}}AFX_MSG_MAP
 // Standard file based document commands
 ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
 ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
 // Standard print setup command
 ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

//////////////////////////////////////////////////////////////
// CEditorApp construction

CEditorApp::CEditorApp()
{
}

//////////////////////////////////////////////////////////////
// The one and only CEditorApp object

CEditorApp theApp;

//////////////////////////////////////////////////////////////
// CEditorApp initialization

BOOL CEditorApp::InitInstance()
{
 COLORREF clrCtlBk, clrCtlText;

 // Set dialog background color to blue, text to white

 
SetDialogBkColor(clrCtlBk=RGB(0,0,255),
                  clrCtlText=RGB(255,255,255));


 AfxEnableControlContainer();

 // Standard initialization
#ifdef _AFXDLL
 Enable3dControls();      // Call when using as shared DLL
#else
 Enable3dControlsStatic();  // Call when statically linking
#endif

 // Change the registry key under which settings are stored.
 SetRegistryKey(_T(“Local AppWizard-Generated Applications”));

 LoadStdProfileSettings();  // Load standard INI file options

 // Register document templates

 CMultiDocTemplate* pDocTemplate;
 pDocTemplate = new CMultiDocTemplate(
   IDR_EDITORTYPE,
   RUNTIME_CLASS(CEditorDoc),
   RUNTIME_CLASS(CChildFrame), // custom MDI child frame
   RUNTIME_CLASS(CEditorView));
 AddDocTemplate(pDocTemplate);

 // create main MDI Frame window
 CMainFrame* pMainFrame = new CMainFrame;
 if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
   return FALSE;
 m_pMainWnd = pMainFrame;

 // Parse command line for standard shell commands, etc.
 CCommandLineInfo cmdInfo;
 ParseCommandLine(cmdInfo);

 // Dispatch commands specified on the command line
 if (!ProcessShellCommand(cmdInfo))
   return FALSE;
 pMainFrame->ShowWindow(m_nCmdShow);
 pMainFrame->UpdateWindow();

 return TRUE;
}

//////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
 CAboutDlg();

// Dialog Data
 //{{AFX_DATA(CAboutDlg)
 enum { IDD = IDD_ABOUTBOX };
 //}}AFX_DATA

 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CAboutDlg)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    
 //}}AFX_VIRTUAL

// Implementation
protected:
 //{{AFX_MSG(CAboutDlg)
   // No message handlers
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
 //{{AFX_DATA_INIT(CAboutDlg)
 //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CAboutDlg)
 //}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
 //{{AFX_MSG_MAP(CAboutDlg)
   // No message handlers
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CEditorApp::OnAppAbout()
{
 CAboutDlg aboutDlg;
 aboutDlg.DoModal();
}


//////////////////////////////////////////////////////////////
// CEditorApp commands
The message map, near the top of the listing, belongs to the CEditorApp class. This message map specifically links the ID_APP_ABOUT, ID_FILE_NEW, ID_FILE_OPEN, and ID_FILE_PRINT_SETUP messages with their member functions OnAppAbout( ), CWinApp::OnFileNew( ), CWinApp::OnFileOpen( ), and CWinAppOnFilePrintSetup( ). Also notice in the listing that a constructor, an initial instance InitInstance( ), and a member function OnAppAbout( ) are implemented.
One change that has been made to the AppWizard’s code is a change in the background and foreground colors for all dialog boxes used by this application. The portion of code required for this change is shown in bold in the previous listing.
Also, as the following code shows, this application will use a multiple-document interface instead of the single-document interface used in the previous example:
// Register document templates
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
 IDR_EDITORTYPE,
 RUNTIME_CLASS(CEditorDoc),
 RUNTIME_CLASS(CChildFrame), // custom MDI child frame
 RUNTIME_CLASS(CEditorView));
AddDocTemplate(pDocTemplate);
The About dialog box is derived from the CDialog class just as in the previous example. There are no initial CEditorApp commands, as you can see from the end of the listing.
The MAINFRM.CPP File
The MainFrm.cpp file, shown here, contains the frame class CMainFrame. This class is derived from CFrameWnd and is used to control all multiple-document-interface (MDI) frame features. Pay particular attention to the portion of the listing set in a bold font.
// MainFrm.cpp : implementation of the CMainFrame class
//

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

#include “MainFrm.h”

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

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
 //{{AFX_MSG_MAP(CMainFrame)
 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()
{
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (CMDIFrameWnd::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
}


 m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
 EnableDocking(CBRS_ALIGN_ANY);
 DockControlBar(&m_wndToolBar);
 return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
 if( !CMDIFrameWnd::PreCreateWindow(cs) )
   return FALSE;
 return TRUE;
}

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

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
 CMDIFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
 CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
When you examine this listing, you will notice that the message map does handle ON_WM_CREATE messages. The constructor and destructor, however, still contain no code.
However, notice the inclusion of this small portion of code:
static UINT indicators[] =
{
 ID_SEPARATOR,           // status line indicator
 ID_INDICATOR_CAPS,
 ID_INDICATOR_NUM,
 ID_INDICATOR_SCRL,
};
Recall that the AppWizard was asked to generate a template with an initial toolbar and status bar. This group of custom controls will require ID values for the various status-line indicators.
The inclusion of the toolbar and status bar is handled by the second portion of bolded code, shown in the MAINFRM.CPP listing.
The member functions AssertValid( ) and Dump( ) use definitions contained in the parent class. CMainFrame initially contains no message handlers.
The EDITORDOC.CPP File
The EDITORDOC.CPP file, shown here, contains the CEditorDoc class, which is unique to this application. This file is used to hold document data and to load and save files.
// 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)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()
//////////////////////////////////////////////////////////////
// CEditorDoc construction/destruction

CEditorDoc::CEditorDoc()
{
}

CEditorDoc::~CEditorDoc()
{
}

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

 return TRUE;
}


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

void CEditorDoc::Serialize(CArchive& ar)
{
 ((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}

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

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

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

//////////////////////////////////////////////////////////////
// CEditorDoc commands
When you examine this listing, you will again notice that the message map, constructor, and destructor contain no code. Several member functions can be used to provide vital document support. OnNewDocument( ) uses the definition provided by the parent class. Serialize( ) supports persistent objects. This line of code, set in bold type in the previous listing, provides the functionality to the file I/O menu commands, allowing text files to be created, opened, and saved.
The member functions AssertValid( ) and Dump( ) use definitions contained in the parent class. There are no initial CEditorDoc commands.
The EDITORVIEW.CPP File
The EDITORVIEW.CPP, shown next, provides the view of the document. In this implementation, CEditorView is derived from the CEditView class.
// EditorView.cpp : implementation of the CEditorView class
//

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

#include “EditorDoc.h”
#include “EditorView.h”

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

///////////////////////////////////////////////////////////////////
// CEditorView

IMPLEMENT_DYNCREATE(CEditorView, CEditView)

BEGIN_MESSAGE_MAP(CEditorView, CEditView)
 //{{AFX_MSG_MAP(CEditorView)
 ON_WM_RBUTTONDOWN()
 //}}AFX_MSG_MAP
 // Standard printing commands
 ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
 ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
 ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////
// CEditorView construction/destruction

CEditorView::CEditorView()
{
}

CEditorView::~CEditorView()
{
}

BOOL CEditorView::PreCreateWindow(CREATESTRUCT& cs)
{
 BOOL bPreCreated = CEditView::PreCreateWindow(cs);
 cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL);  // Enable word-wrapping

 return bPreCreated;
}

///////////////////////////////////////////////////////////////////
// CEditorView drawing

void CEditorView::OnDraw(CDC* pDC)
{
 CEditorDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
}
///////////////////////////////////////////////////////////////////
// CEditorView printing

BOOL CEditorView::OnPreparePrinting(CPrintInfo* pInfo)
{
 // default CEditView preparation
 return CEditView::OnPreparePrinting(pInfo);
}

void CEditorView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
 CEditView::OnBeginPrinting(pDC, pInfo);
}

void CEditorView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
 CEditView::OnEndPrinting(pDC, pInfo);
}

///////////////////////////////////////////////////////////////////
// CEditorView diagnostics

#ifdef _DEBUG
void CEditorView::AssertValid() const
{
 CEditView::AssertValid();
}

void CEditorView::Dump(CDumpContext& dc) const
{
 CEditView::Dump(dc);
}

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

void CEditorView::OnRButtonDown(UINT nFlags, CPoint point)
{
 
char szTimeStr[20];
 
CTime tm=CTime::GetCurrentTime( );
                               
sprintf(szTimeStr, “It’s now  %02d:%02d:%02d”,
         tm.GetHour( ),tm.GetMinute( ),
           tm.GetSecond( ));
        
 
MessageBox(szTimeStr, “Is it time to quit yet?”,
            MB_OK);



 
CEditView::OnRButtonDown(nFlags, point);
}
When you examine the message map, you will see that it contains ON_WM_RBUTTONDOWN, which was added by the ClassWizard, and ID_FILE_PRINT and ID_FILE_PREVIEW, which are provided when the CEditorView class is used. The constructor and destructor are empty.
The OnDraw( ) member function uses the pointer pDoc to point to the document. CEditorView handles document printing with OnPreparePrinting( ), OnBeginPrinting( ), and OnEndPrinting( ). The member functions AssertValid( ) and Dump( ) use definitions contained in the parent class.
The message handler code for OnRButtonDown( ) is an easy enhancement to the application. Use the ClassWiz ard, shown in Figure 24-26, to add WM_RBUTTONDOWN message capabilities and the OnRButtonDown class.
Figure 24-26: A new class is added by the classWizard to intercept right mouse button down messages
Double-click the mouse on the OnRButtonDown member function, shown in Figure 24-26, to move directly to the CEditorView message handler section of EDITORVIEW.CPP. Figure 24-27 shows the point where the code was inserted.
Figure 24-27: The OnRButtonDown message handler code is added to the application
Now, if the user clicks the right mouse button while using the text editor, a small dialog box will pop up on the screen and display the current time.
Figure 24-28 shows the application running with two text files opened for inspection.
Figure 24-28: Multiple documents can be opened with this simple text editor
Want to know what time it is while you are working? Maybe it’s time to quit? Depress the right mouse button while over a document to pop up a message box with the current time. Figure 24-29 shows an example of this message box.
Figure 24-29: The Editor application provides a simple message box that gives the current time

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net