Preprocessor Operators

Chapter 23 - Windows Applications Using the MFC

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

A Simple Application and Template
In Chapter 22, techniques for establishing a window on the screen with the MFC library were introduced. That example serves as a gateway to all such Windows applications that utilize the client area for printing and drawing.
The first application in this chapter, named MFCSWP, simply prints a message in the window’s client area. The name of this program is derived from the acronym for “MFC simple windows program.”
Before beginning the discussion of the important aspects of the application, let’s examine a complete program listing. The listing that follows includes a header file and a C++ source code file. The information in the header file was essentially contained in the source code file in the previous chapter. The header file provides information on how our application’s classes are derived from the MFC library. This is a style of coding that is promoted by Microsoft. Here is the MFCSWP.H header file listing:
class CMainWnd : public CFrameWnd
{
public:
 CMainWnd( );
 afx_msg void OnPaint( );
 DECLARE_MESSAGE_MAP( );
};

class CmfcswpApp : public CWinApp
{
public:
 BOOL InitInstance( );
};
The C++ source code file is straightforward. As you examine the following MFCSWP.CPP file, pay particular attention to the overall length of the listing:
//
//  mfcswp.cpp
//  A Simple Windows Program using the MFC.
//  This code can serve as a template for the development
//  of other MFC applications.
//  Copyright (c) William H. Murray and Chris H. Pappas, 1998
//

#include <afxwin.h>
#include “mfcswp.h”

CmfcswpApp theApp;

CMainWnd::CMainWnd( )
{
 Create(NULL,"A MFC Windows Application",
        WS_OVERLAPPEDWINDOW,rectDefault,NULL,NULL);
}
void CMainWnd::OnPaint( )
{
 CPaintDC dc(this);
 dc.TextOut(200,200,"Using the MFC Library",21);
}

BEGIN_MESSAGE_MAP(CMainWnd,CFrameWnd)
 ON_WM_PAINT( )
END_MESSAGE_MAP( )

BOOL CmfcswpApp::InitInstance( )
{
 m_pMainWnd=new CMainWnd( );
 m_pMainWnd->ShowWindow(m_nCmdShow);
 m_pMainWnd->UpdateWindow( );

 return TRUE;
}
This composite listing gives you a chance to examine all of the code necessary to produce a working application. The next sections examine those details that are unique to this example.
The MFCSWP.H Header File
As you examine the applications in this chapter, you will find two types of header files. The first type, shown in this section, is used to indicate class definitions that are unique to the application. As a matter of style, the filename and the .H extension—for example, MFCSWP.H—will always identify this type of header file. The second style of header file is the type used earlier in Chapter 21. This style header file can contain menu and dialog box resource identification values. When this second style of header file is used, it is identified with an additional “R” (standing for “resource”) at the end of the filename. For example, if the application in this section had used a resource ID header file, it would have been named MFCSWPR.H. You’ll see this second style of header file used in the final two examples in this chapter.
The definitions for two classes are contained here: CMainWnd is derived from CWinApp, and CmfcswpApp is derived from CFrameWnd.
class CMainWnd : public CFrameWnd
{
public:
 CMainWnd( );
 afx_msg void OnPaint( );
 DECLARE_MESSAGE_MAP( );
};

class CmfcswpApp : public CWinApp
{
public:
 BOOL InitInstance( );
};
  Note Recall that these classes were part of the body of the SIMPLE.CPP application and were explained in Chapter 21. Putting them in a separate header file is just a matter of style—one encouraged by Microsoft.
Notice, in particular, that CMainWnd contains a member function declaration, OnPaint( ), and the addition of a message map. For member functions such as OnPaint( ), the afx_msg keyword is used instead of virtual. The OnPaint( ) member function belongs to the CWnd class that the CMainWnd class overrides. This allows the client area of the window to be altered. The OnPaint( ) function is automatically called when a WM_PAINT message is sent to a CMainWnd object.
DECLARE_MESSAGE_MAP is used in virtually all MFC Windows applications. This line states that the class overrides the handling of certain messages. (See the body of the application.) Microsoft uses message maps instead of virtual functions because it is more space efficient.
The MFCSWP.CPP Source Code File
The majority of this application’s code is the same as the SIMPLE.CPP example from Chapter 21. However, notice the addition of the OnPaint( ) message handler function.
Examine the portion of code shown here:
void CMainWnd::OnPaint( )
{
 CPaintDC dc(this);

 dc.TextOut(200,200,"Using the MFC Library",21);
}
A device context is created for handling the WM_PAINT message. Now, any Windows GDI functions that are encapsulated in the device context can be used in this member function. This code is similar in concept to the procedure-oriented template created in Chapter 21. When the OnPaint( ) function has finished, the destructor for CPaintDC is called automatically.
This application uses a fairly short message map, as the following code indicates:
BEGIN_MESSAGE_MAP(CMainWnd,CFrameWnd)
 ON_WM_PAINT( )
END_MESSAGE_MAP( )
Two classes are specified by BEGIN_MESSAGE_MAP: CMainWnd and CFrameWnd. CMainWnd is the target class, and CFrameWnd is a class derived from CWnd. The ON_WM_PAINT( ) function handles all WM_PAINT messages and directs them to the OnPaint( ) member function just discussed. In upcoming applications, you’ll see many additional functions added to the message map.
The biggest advantage of using a message map is the elimination of many error-prone switch/case statements that are so typical of procedure-oriented Windows applications.
Running the MFCSWP Application
When you create your project file for your Visual C++ compiler, make sure that you mark this application as an MFC application. Otherwise, you will get a series of strange errors when compiling and linking. Once you have entered the application code and received an error-free compilation, run the program. The screen should be similar to the one shown in Figure 23-1.
Figure 23-1: Printing text to the client area with an MFC application
If you want to experiment with other GDI primitives, just remove the TextOut( ) function call and insert the function of your choice into the template code. You can choose from such functions as: Rectangle( ), Ellipse( ), LineTo( ), and so on. The next example will illustrate the use of several graphics functions that will draw a line, a chord, an arc, and more.

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