MFC: Getting Started with Visual C++

Author: Ronald S. Holland
at
Total Application Works






To visit my site

HOME Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript


To contact us

© 2002 - 2017 All Rights Reserved Total Application Works


  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)

  • <html>
    <head>
    <title>Example of a link </title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/GettingStarted.html">
    MFC: Getting Started with Visual C++ </a>
    </table>
    </body>
    </html>
    Download Source and see a screen shot


Introduction

This tutorial is intended for that person who is getting started with Microsoft's MFC. In order for you to be able to follow this tutorial, you will need Microsoft's Visual C++ compiler. It assumes that you have completed A basic first C++ Program tutorial. In addition, you should be familiar with the concepts of inheritance. A more detailed explanation of inheritance can be found at

In this tutorial, we will create an Hello World application, using MFC, and then in the next tutorial, we will create a simple application that demonstrates event handling. The following section will give a brief description of classes and inheritance. Below are some links to tutorials on MFC.

Class

Let's consider the class of automobile, which contains the set of all automobiles. A sampling might be:
  • Acura TL
  • BMW 5 series
  • Buick Century
  • Ford Explorer
  • Honda Pilot
  • Mazda6
  • Toyota Avalon
  • Toyota Highlander
  • Volvo S60
  • Etc.
If we were to write one of the examples above in C++, it might look like:
  
class Toyota : automobile {
   ...
}

class Avalon : Toyota {
   ...
}
  
Figure 1: Avalon derived from automobile
This example is interpreted as class Avalon inherits from class Toyota and class Toyota inherits from class automobile.

Requirements

  1. Create a MFC application template.
  2. Use the MFC application template to create an Hello world application.
    • The application should display a text label.

MFC Application Template

If you look in a dictionary, template is defined as

    a pattern, usually in the form of a thin plate of metal, wood, plastic, etc., for forming an accurate copy of an object or shape

In C++, a template is a keyword that allows you to create generic functions and/or classes. Java has a keyword interface that is similar, but for our purposes, we will say they are not the same. How does the term template/pattern apply? Remember that a template is a pattern; therefore, the application template for Hello World looks like:

  

// include application framework windows class definitions:
#include <afxwin.h>
#include "MyApp.h"

/****************************************
 * constructor initializes resources
 ***************************************/
MyApp::MyApp()
{
   
}

/****************************************
 * destructor frees resources
 ***************************************/
MyApp::~MyApp()   
{ 
  
}

class CHelloWorldApp : public CWinApp  
{
public:
   virtual BOOL InitInstance()   // override default CWinApp method
   {
     m_pMainWnd = new MyApp();             // instantiate MyApp
     m_pMainWnd->ShowWindow( m_nCmdShow ); // make frame visible
     m_pMainWnd->UpdateWindow();           // refresh frame window

     return true;                          // report success
   }

} helloWorldApp;     

  
Figure 2: Hello World Application

And the header file would look like:
  
// define our window based on the MFC frame window 
class MyApp : public CFrameWnd 
{
public:
   MyApp();          // MyApp constructor
   virtual ~MyApp(); // free-up resources
 
private:

};
  
Figure 3: Hello World Application - Header file

You will notice that there is no main() method. In this example, the following code becomes the de facto main() method.
  
1-  class CHelloWorldApp : public CWinApp 
2-  {
3-  public:
4-     virtual BOOL InitInstance()   // override default CWinApp method
5-     {
6-        m_pMainWnd = new MyApp();             // instantiate MyApp
7-          . . .
8-     }
9-
10- } helloWorldApp;                // instantiate app
  
Figure 4: Hello World Application - Main

How does this de facto main() method work?

  1. Line 10:
      instantiates the CHelloWorldApp class and causes a call to the base class constructor CWinApp
  2. Line 1:
      CWinApp constructor calls WinMain, which calls the overridden method InitInstance() at line: 4
  3. Line 6:
      instantiates the MydApp class

Where do we go from here? The next step is to use the template to create a Hello World application. In the next section we will take this next step.

Expand Our Template to Create an Hello World Application

In the section, we will create a

  • main frame window, and
  • a label window that contains text.
Since MyApp is derived from CFrameWnd, to create a frame window, we use the following code.
  
Create( 0,                       // default CFrameWnd class
        "Hello World",              // window title
         WS_OVERLAPPEDWINDOW,     // full-featured frame window
         CRect( 100, 100,          // initial position and
                 600, 400 ) );       // frame size 
 
  
Figure 5: MyApp frame window
We will use the following code to create the label or static text.
  
 
    pHello = new CStatic();      // create a static control

    pHello->Create(              // create Windows control
       "This is a slight modification of the Hello World. "
	   "\n \n"
	   "Hello world from Ron Holland" 	   , // text
       WS_CHILD | WS_VISIBLE | WS_BORDER  // window styles
       | SS_CENTER,                 // static object styles
       CRect( 50, 50, 450, 150 ),   // window coordinates
       this );       
 
  
Figure 6: Hello world


Putting It Together: Build an Hello World Application

Using the template and the code to create a frame and static window, we get the following header file:

  
/*******************************************************
 * File: MyApp.h
 *******************************************************/

// define our window based on the MFC frame window 
class MyApp : public CFrameWnd 
{
public:
   MyApp();          // MyApp constructor
   virtual ~MyApp(); // free-up resources
 
private:
   CStatic *pHello;        // pointer to a static control
};
  
and the following .cpp file.
  
/*************************************************************
 * File: helloWorld.cpp
 *************************************************************/

// include application framework windows class definitions:
#include 
#include "MyApp.h"


/****************************************
 * MyApp constructor initializes frame
 * 1- Create a Cframe window
 * 2- Instantiate the pHello pointer based on a CStatic control
 * 3- Create a CStatic child window
 ****************************************/
MyApp::MyApp()
{
   // Create Window with Title Bar
   Create( 0,                       // default CFrameWnd class
           "Hello World",              // window title
           WS_OVERLAPPEDWINDOW,     // full-featured window
            CRect( 100, 100,          // initial position and
                 600, 400 ) );       // frame size
 
 
    pHello = new CStatic();      // create a static control

    pHello->Create(              // create Windows control
       "This is a slight modification of the Hello World. "
	   "\n \n"
	   "Hello world from Ron Holland" 	   , // text
       WS_CHILD | WS_VISIBLE | WS_BORDER  // window styles
       | SS_CENTER,                 // static object styles
       CRect( 50, 50, 450, 150 ),   // window coordinates
       this );                      // context
}

/****************************************
 * destructor frees resources
  ***************************************/
MyApp::~MyApp()   
{ 
   delete pHello;          // only object owned by window 
}

/*************************************************
 * Derive our application class CHelloApp based on 
 * CWinApp
 * 1- Overwrite InitInstance() method
 * 2- Make main frame window visiable
 * 3- Make the main frame window visiable
 *************************************************/
class CHelloWorldApp : public CWinApp 
{
public:
   virtual BOOL InitInstance()   // override default method
   {
     m_pMainWnd = new MyApp();    // instantiate MyApp
     m_pMainWnd->ShowWindow( m_nCmdShow ); // make frame visible
     m_pMainWnd->UpdateWindow();           // refresh frame window

     return true;                          // report success
   }

} helloWorldApp;     

  
Figure 7: Hello world



top

AFXWIN - This is a header for the Microsoft Foundation Classes C++ library. It must be included in all MFC applications.

CFrameWnd - The CFrameWnd class encapsulates the functionality of a Windows single document interface (SDI) frame window. A frame window is a window that frames an application.

CWinApp - The CWinApp class is the base class from which you derive a Windows application object. An application object provides member functions for initializing your application (and each instance of it) and for running the application.

CStatic - The CStatic class provides the functionality of a Windows static control. A static control is a control used to display text, to draw frames or lines separating other controls, or to display icons. A static control doesn't accept user input, but it can notify its parent of stylus taps if it's created with SS_NOTIFY style.

event handling - an event occurs when a mouse is clicked, double clicked, a button is clicked, a key pressed, etc. Event handling is the code that is called as a result of an event.

inheritance - is the ability to create a class that derives properties from one or more base classes.

initinstance - Override InitInstance to initialize each new instance of your application running under Windows. Typically, you override InitInstance to construct your main window object and set the CWinThread::m_pMainWnd data member to point to that window.

m_pMainWnd - Holds a pointer to the application’s main window.

SS_CENTER - Designates a simple rectangle and displays the given text centered in the rectangle. The text is formatted before it is displayed. Words that would extend past the end of a line are automatically wrapped to the beginning of the next centered line.

Template - is a unique class or pattern that can be used to build other software code.

WS_BORDER - Creates a window that has a border.

WS_CHILD - Creates a child window. Cannot be used with the WS_POPUP style.

WS_VISIBLE - Creates a window that is initially visible.


top



[HOME] [Consulting] [Design] [Maintenance] [Project] [Testing] [Training] [Turnkey] [Java] [C++] [SQL] [HTML] [JavaScript ]

© 2002 - 2035 All Rights Reserved Total Application Works