Visual C++

Introduction to MFC

Introduction to Applications

The Microsoft Foundation Class (MFC) library is a set of data types, functions, classes, and constants used to create applications for the Microsoft Windows family of operating systems.

The first thing you should do to start a program is to create an application. In Win32, an application is created by a call to the WinMain() function and building a WNDCLASS or WNDCLASSEX structure. In MFC, this process has been resumed in a class called CWinApp (Class-For-A-Windows-Application). Based on this, to create an application, you must derive your own class from CWinApp.

An application by itself is an empty thing that only lets the operating system know that you are creating a program that will execute on the computer. It doesn't display anything on the screen. If you want to display something, the CWinApp class provides the InitApplication() method that you must override in your class. InitApplication() is a Boolean method. If it succeeds in creating the application, it returns TRUE. If something went wrong when trying to create the application, it would return FALSE. The minimum skeleton of an application would appear as follows:

class CExerciseApp : public CWinApp { public:     virtual BOOL InitInstance(); };  BOOL CExerciseApp::InitInstance() {     return TRUE; } 

After creating the application, to make it available to other parts of the program, you must declare a global variable of your class. It is usually called theAppbut you can call it anything you want.

The fundamental classes of MFC are declared in the afxwin.h header file. Therefore, this is the primary header you may have to add to each one of your applications. Based on this, a basic application can be created as follows:

#include <AFXWIN.H>   class CExerciseApp : public CWinApp { public:     virtual BOOL InitInstance(); };  BOOL CExerciseApp::InitInstance() {     return TRUE; }  CExerciseApp theApp; 

Introduction to Frames

As its name implies, a frame of a window includes the borders, the location, and the dimensions of a window. There are two types of MFC applications: those that use a frame and those that don't. A frame-based application uses a concept known as the Document/View Architecture. This allows the frame to serve as a place holder for other parts of an application (such as the document and the view).

To create a frame, the MFC library provides various classes. One of these is called CFrameWnd and it is the most commonly used frame class. To use a frame, you can derive your own class from CFrameWnd as follows:

class CApplicationFrame : public CFrameWnd { }; 

Because there can be many frames or various types of frames in an application, the first or main frame is usually called CMainFrame, we will follow the same habit but you can call your frame class anything you want:

class CMainFrame : public CFrameWnd { }; 

The skeleton of this frame only serves as a foundation for your class. You must actually create a window frame that would display to the user. To create a window frame, the CFrameWnd class provides the Create() method. Its syntax is:

BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName,        DWORD dwStyle = WS_OVERLAPPEDWINDOW, const RECT& rect = rectDefault,        CWnd* pParentWnd = NULL, LPCTSTR lpszMenuName = NULL, DWORD dwExStyle = 0,        CCreateContext* pContext = NULL ); 

As you can see, the only two required arguments are the class name and the window name. We will come back to all these arguments when we study window classes in more detail. For now, a minimum frame can be created by simply passing the class name as NULL and the window name with a null-terminated string. Here is an example:

class CMainFrame : public CFrameWnd { public:     CMainFrame(); };  CMainFrame::CMainFrame() {     Create(NULL, "MFC Fundamentals"); } 

In order to provide a window to the application, you must create a thread. This would be done using the CWinThread class. To make this a little easy,CWinThread is equipped with a public member variable called m_pMainWnd. This variable can be used to create a thread for the main window of the application. One of its advantages is that it makes sure that your application terminates smoothly when the user decides to close it. CWinThread is the base class of CWinApp and therefore makes m_pMainWnd available to any CWinThread derived class such as CFrameWnd. Based on this, to create a thread for the main window to display, you can assign a pointer of your frame class to m_pMainWnd. After this assignment, m_pMainWnd can be used as the window object to display the frame, which is usually done by calling the ShowWindow() method. This would be done as follows:

BOOL CExerciseApp::InitInstance() {     m_pMainWnd = new CMainFrame;     m_pMainWnd->ShowWindow(SW_NORMAL);      return TRUE; } 

This application was created as follows:

  1. Start Microsoft Visual C++ or Visual Studio.
  2. On the main menu, click either File -> New... or File -> New Project...
  3. In the New dialog box, click Projects or, in the New Project dialog box, click Visual C++ Projects.
  4. Click either Win32 Application or Win32 Project.
  5. Type a name for the application in the Name edit box. An example would be MFCFundamentals1.
  6. Click OK.
  7. Specify that you want to create a Windows Application as an Empty Project and click Finish.
  8. To use MFC in MSVC 6, click Project -> Settings... In MSVC 7, in the Solutions Explorer property page, right-click the project name (MFCFundamentals1) and click Properties.
  9. In the Microsoft Foundation Classes combo box or in the Use of MFC combo box, select Use MFC In A Shared DLL.
  10. Click OK.
  11. To add a file to create the application, on the main menu of MSVC 6, click File -> New... or, for MSVC 7, on the main menu, click Project -> Add New Item...
  12. Click C++ (Source) File and, in the Name edit box, type a name for the file. An example would be Exercise.
  13. Click OK or Open.
  14. In the empty file, type the above code:
    #include <AFXWIN.H>   class CExerciseApp : public CWinApp { public:   virtual BOOL InitInstance(); };  class CMainFrame : public CFrameWnd { public:   CMainFrame(); };  CMainFrame::CMainFrame() {   Create(NULL, "MFC Fundamentals"); }  BOOL CExerciseApp::InitInstance() {   m_pMainWnd = new CMainFrame;   m_pMainWnd->ShowWindow(SW_NORMAL);    return TRUE; }  CExerciseApp theApp;
  15. Execute the application.
  16. Close it and return to MSVC.