Home

Introduction to MFC Applications

    

Visual C++ Projects and Files

 

Creating a New Project

 

Microsoft Visual C++ allows you to create various types of projects ranging from console applications to complex libraries, from Win32 applications to communications modules or multimedia assignments.

Various options are available to create a project:

  • On the main menu, you can click File -> New Project...
  • On the Start Page, click New Project...
  • Press Ctrl + N

Any of these actions would display the New Project dialog box from where you can select the type of project you want.

Practical LearningPractical Learning: Creating a Microsoft Visual C++ Project

  1. Start Microsoft Visual Studio
  2. On the main menu, click File -> New Project...
  3. In the New Project dialog box, in the left list, click Visual C++.
    In the right list, click Win32 Console Application
  4. In the Name edit box, type IntroductionToMFC1
  5. Click OK
  6. In the Win32 Application Wizard, click Finish

Creating a File

Although the most popular files used in Microsoft Visual C++ are header and source files, this IDE allows you to create various other types of files that are not natively C++ types.

To create a file, if a project is currently opened:

  • On the main menu, click Project -> Add New Item... This would open the New dialog box from where you can select the desired type of file and click Open. By default, the new file would open in the Source Code Editor. If you want to open the file otherwise, after selecting it in the New dialog box, click the arrow of the Open As combo box.
  • In the Solution Explorer or the Class View, right- click the name of the project, position the mouse of Add, and click the option you want

Adding Existing Files to a Project

If a file had already been created and exists on a separate folder, drive, directory or project, you can import it and add it to your application. When you decide to add an existing file to a project, because Microsoft Visual Studio allows you a great level of flexibility on the types of files you can add, it is your responsibility to check that the file is valid.

If you copy a file from somewhere and paste it in the folder that contains your project, the file is still not considered as part of the project. You must explicitly add it. Before adding an existing file to your project:

  • On the main menu, you can click File -> Add Existing Item…
  • On the main menu, you can also click Project -> Add Existing Item…
  • In the Solution Explorer, you can right-click the name of the project, position your mouse on Add and click Add Existing Item…

Any of these actions would open the Add Existing Item dialog box. This requires you to locate the item and select it. Once the item is ready, to add it to the project, click the Open button. If you want to open it first, click the arrow of the Open button and specify how you want to open the file.

Adding Classes

To add a class to your project, you have various options. You can separately create a header file then create a source file. You can also import an existing header file and a source file from any valid path. Microsoft Visual C++ makes it easy to create a C++ class. When doing this, a header and a source files are prepared. A (default) constructor and a destructor are added. You can delete or completely change the supplied constructor and/or the destructor. Also, Microsoft Visual C++ includes the name of the header file in the source file so you would not have forgotten to do this. Most classes in Microsoft Visual C++ have their names starting with the letter C. Although this is only a suggestion, and any valid class name would work fine, to keep some harmony, in our lessons, we will follow this convention.

To create a new class and add it to your project:

  • On the main menu, click Insert -> Add Class...
  • In the Class View, right-click the name of the project, position the mouse on Add, and click Class...

Any of these actions would open the New Class dialog box. From there, you can specify the type of class you want to create and click Open. You will then be asked to provide a name for the class.

Opening Files

To open a file:

  • On the main menu, click File -> Open File...
  • Press Ctrl + O

If you had previously created or opened a file, Microsoft Visual C++ keeps a list of the most recently used (MRU) files under the File menu. To control the maximum number of files that can be listed, on the mein menu, click Tools -> Options. In the left list of the Options dialog box, click Environment and click General:

Options

After specifying the desired value, click OK.

Opening Existing Projects

A project is made of various files and subject to the environment in which it was created. A project itself is a file but it is used to "connect" all the other files that compose its particular application.

There is a limit on the types of projects you can open in Microsoft Visual Studio. This is not an anomaly. Every computer-programming project is created using a particular environment and each environment configures and arranges its project as its judges it necessary. Any attempt to open an unrecognizable project would produce an error.

Microsoft Visual Studio is configured to easily open various types of projects. To open a project:

  • If you have just used one of a certain number of projects
    • On the Start Page of Microsoft Visual Studio, click its name
    • On the main menu, click File, position the mouse on Recent Projects and Solutions, and click the name of the project
  • On the main menu, click File -> Open Project... Locate the folder that contains the project, select it and click Open

Introduction to the Microsoft Foundation Class Library

 

Introduction to Win32

Win32 is a library made of data types, variables, constants, functions, and classes (mostly structures) that can be used to create applications for the Microsoft Windows family of operating systems. A typical application is made of at least two objects: a control and a host object on which the control is positioned.

To create a Win32 application using Microsoft Visual C++:

  • On the Start Page, you can click New Project
  • On the main menu, you can click File -> New Project...
  • On the Standard toolbar, you can click the New Project button
  • You can press Ctrl + N

Any of these actions would display the New Project dialog box. From there, select Win32 Project item.

Just like a C++ program always has a main() function, a Win32 program uses a central function called WinMain. The syntax of that function is:

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine, int nCmdShow );

To support international characters, Microsoft Windows provides another version of this function named _tWinMain. This version uses the same syntax (same arguments) as the first.

Unlike the C++'s main() function, the arguments of the WinMain() function are not optional. Your program will need them to communicate with the operating system. Here is an example of starting a program with the WinMain() function:

#include <windows.h>

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow )
{ 
    return 0;
}

Introduction to MFC

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

Practical LearningPractical Learning: Starting an MFC Application

  1. On the main menu, click File -> New Project...
  2. In the Project Types, click Visual C++
  3. In the Templates list, click Win32 Project
  4. Set the Name to Exercise1
    New Project
  5. Click OK
  6. On the first page of the wizard, click Next
     
    Win32 Application Wizard
  7. On the second page, accept Windows Application and click Empty Project
     
    Win32 Application Wizard
  8. Click Finish

CObject, the Ancestor of All/Most MFC Classes

To implement its functionality, the MFC is organized as a hierarchical tree of classes, the ancestor of which is CObject:

CObject

Although you can create C++ classes for your applications, most of the classes you will use throughout our lessons descend directly or indirectly from CObject.

The CObject class lays a valuable foundation that other classes can build on. Using the rules of inheritance, the functionality of CObject can be transparently applied to other classes as we will learn little by little. Some of the features that CObject provides are:

  • Performing value streaming for saving or opening the contents of files
  • Controlling dynamic creation and destruction of its inherited classes
  • Checking the validity of variables of classes, etc

You will hardly use CObject directly in your program. Instead, you may create your own classes that are based on CObject. Here is an example:

class CCar : public CObject
{
public:
    CCar();
    char *Make;
    char *Model;
    int Year;
    long Mileage;
    int Doors;
    double Price;
};

Practical LearningPractical Learning: Supporting MFC

  1. To make sure that this application uses MFC, on the main menu, click Project -> Properties...
  2. In the left list, click Configuration Properties
  3. In the right list, click User of MFC, click the arrow of its combo box and select Use MFC In A Shared DLL
    Property Pages
  4. Click OK

CObject Methods

When inheriting a class from CObject, your class can take advantage of the features of its parent CObject. This means that you will have available the functionality laid by its methods. Some of the methods of the CObject class are:

  • CObject(): This constructor allows you to use an instance of CObject. If you have created a class based on CObject, when you declare an instance of your class, the default CObject constructor is available and gives you access to the CObject methods.
  • CObject(const CObject &Src): If you want to copy a variable of your CObject derived class to use in your program, for example to assign it to another variable, you can use the inherited copy constructor. Since the object is derived from CObject, the compiler would make a copy of the variable on a member-by-member basis.
  • Serialize(): This method allows you to stream the values of the members of your objects.
  • AssertValid(): This method is used to check your class. Because an inherited class is usually meant to provide new functionality based on the parent class, when you create a class based on CObject, you should provide a checking process for your variables. In this case you should provide your own implementation of the AssertValid() method. You can use it for example to check that an unsigned int variable is always positive.

The objects of an application send messages to the operating system to specify what they want. The MFC library provides a special class to manage these many messages. The class is called CCmdTarget. We will come back to it when dealing with messages. 

A Basic Application

To create a program, also called an application, you derive a class from the MFC's CWinApp class. The CWinApp class is derived from the CWinThread class. CWinThread is derived from a class named CCmdTarget, which is derived from CObject. CWinApp stands for Class For A Windows Application:

CWinApp

 
 
 

Creating and Managing Classes in Microsoft Visual Studio

 

Creating Header and Source Files

One of the most regular actions you perform when working on a project consists of creating classes. Microsoft Visual Studio provides various means and tools to assist you with this.

From your knowledge of C++, you can create a class in different files: a header file and a source file. To create any of these:

  • On the main menu, click Project -> Add New Item...
  • In the Solution Explorer, right-click the name of the project or one of the folders names -> Add -> New Item...

Any of these actions would open the Add New Item dialog box. In the Installed Templates list, click Code. Then,

  • To create a header file, click Header File (.h)
    Add New Item
    Give a name to the file
  • To create a source file, click C++ File (.cpp)
    Add New Item
    Give a name to the file

After naming the file, click Add.

After generating the files, you can use them following rules and suggestions of the C++ language. For example, you can create the layout of a class in the header file and define it in the source file.

Creating a Class

As you know from your knowledge of C++, you can create a stand-alone class or derive one from another existing class. Microsoft Visual Studio provides all the necessary tools to visually create or derive a class. To create a class:

  • On the main menu, click Project -> Add Class...
  • In the Solution Explorer, the Class View, or the Resource View, right-click the name of the project -> Add -> Class...

Any of these actions would open the Add Class dialog box:

Add Class

Make sure C++ Class is selected and click Add. This would open the Generic C++ Class Wizard. The wizard presents many options:

  • In the Class Name text box, enter the name of the intended class. By convention (or pure habits), the names of classes in the MFC library and/or MFC-based applications start with C (this is not a rule or an obligation; just a valuable suggestion):

Generic C++ Class Wizard

If you are creating a stand-alone class, click Finish.
  • If you want to create a class that is derived from another class
    • In the Base Class text box, enter the name of the parent class
    • In the Access combo box, select the type of inheritance the derived class will use from the parent class

Generic C++ Class Wizard

  • Since the new class will use a destructor, if you want that destructor to be virtual, click the Virtual Destructor check box
  • By default, the wizard will create a header file (.h) and a source file (.cpp). If you want the whole class to be created in only one file, click the Inline check box

Once you are ready, click Finish. If you specified that you are deriving a class, when you click Finish, the wizard would look for the base class, first in the current project, then in the libraries (if any) referenced in the project. If the wizard doesn't find the parent class, it would display a message box. Here is an example:

Generic C++ Class Wizard

If know for sure that the class exists or you will create it later, click Yes. If you click, you will be given the opportunity of rectifying.

Here is an example of deriving a class from CWinApp:

class CSimpleApp : public CWinApp
{
};

Because the CWinApp class is defined in the AFXWIN.H header file, make sure you include that file where CWinApp is being used.

Practical LearningPractical Learning: Creating a CWinApp-Based Class

  1. In the Solution Explorer, right-click Source Files, position the mouse on Add, and click New Item...
  2. In the Templates lists click C++ File (.cpp)
  3. Set the Name to Exercise
    Add New Item
  4. Click Add
  5. In the empty document, type the following:
    #include <afxwin.h>
    
    class CExerciseApp : public CWinApp
    {
    };

Managing the Member Variables of a Class

After creating a class, you can manage it. Microsoft Visual C++ provides various tools to assist you. Managing a class consists of accessing its file(s), adding member variables, and adding methods.

The Microsoft Foundation Class Library

  

The Framework

Instead of creating an application using "raw" Win32 classes and functions, the MFC library simplifies this process by providing a mechanism called the framework. The framework is a set of classes, functions, and techniques used to create an application as complete as possible with as few lines of code as possible. To provide all this functionality, the framework works behind the scenes with the CWinApp class to gather the necessary MFC classes and functions, to recognize and reconcile the Win32 classes that the application needs. This reconciliation is also made possible by a set of global functions. These functions have names that start with Afx... Some of these functions are:

  • AfxFormatString1
  • AfxFormatString2
  • AfxMessageBox
  • AfxFreeLibrary
  • AfxGetApp
  • AfxGetAppName
  • AfxGetInstanceHandle
  • AfxGetMainWnd
  • AfxGetResourceHandle
  • AfxInitRichEdit
  • AfxLoadLibrary
  • AfxMessageBox
  • AfxRegisterWndClass
  • AfxSocketInit
  • AfxSetResourceHandle
  • AfxRegisterClass
  • AfxBeginThread
  • AfxEndThread
  • AfxGetThread
  • AfxWinInit

We will come back to some of these functions and we will see many other MFC macros in later lessons.

A Global Application Object

To make your application class available and accessible to the objects of your application, you must declare a global variable from it and there must be only one variable of your application. This variable is of the type of the class derived from CWinApp. Here is an example:

class CSimpleApp : public CWinApp
{
};

CSimpleApp MyApplication;

As you can see, you can name this global variable anything you want. By tradition, in Microsoft Visual C++, this variable is named theApp. Here is an example:

CSimpleApp theApp;

To get a pointer to this variable from anywhere in your application, call the AfxGetApp() framework function. Its syntax is:

CWinApp* AfxGetApp();

To implement the role of the Win32's WinMain() function, the framework uses its own implementation of this function and the MFC provides it as AfxWinInit(). It is declared as follows:

BOOL AFXAPI AfxWinInit(HINSTANCE hInstance,
		       HINSTANCE hPrevInstance,
                       LPTSTR lpCmdLine,
		       int nCmdShow);

As you can see, the Win32's WinMain() and the MFC's AfxWinInit() functions use the same arguments.

Practical LearningPractical Learning: Creating a Global Object

  • Declare a global variable as follows:
    #include <afxwin.h>
    
    class CExerciseApp : public CWinApp
    {
    };
    
    CExerciseApp theApp;

A Window's Instance

When you start an application such as Notepad, you are said to have created an instance of the application. In the same way, when you declare a variable of a class, an instance of the class is created and made available to the project. The WinMain() function also allows you to create an instance of an application, referred to as the hInstance argument of the WinMain() function. The instance is created as an HINSTANCE. The CWinApp class provides a corresponding instance variable called m_hInstance. This variable can let you get a handle to the instance of your application. Alternatively, to get a handle to the instance of your application, you can call the AfxGetInstanceHandle() global function. Its syntax is:

HINSTANCE AfxGetInstanceHandle();

Even more, to get a handle to your application, you can call the Win32 API's GetWindowLong() function. Suppose you have opened Notepad to view the source code of an HTML document. This is said that you have an instance of Notepad. Imagine that you want to open a text document using Notepad without closing the first instance of Notepad. To do this, you must open another copy of Notepad. This second copy of Notepad is another instance. In this case, the first instance is referred to as a previous instance. For a Win32 application, the previous instance would be the hPrevInstance argument of the WinMain() function. For a Win32 application, the hPrevInstance argument always has the NULL value. If you want to find out whether a previous instance of an application already exists, you can call the CWnd::FindWindow() method. Its syntax is:

static CWnd* PASCAL FindWindow(LPCTSTR lpszClassName, LPCTSTR lpszWindowName);

If you created the window or if it is a window you know for sure, in which case it could be a WNDCLASS or WNDCLASSEX object, specify it as the lpszClassName argument. If you do not know its name with certainty, set this argument as NULL. The lpszWindowName argument is the possible caption of the window you are looking for. Imagine you position a button on a dialog box and you want the user to launch Notepad with that button and imagine that, if Notepad is already opened, there would be no reason to create another instance of it.

The CWinApp class provides all the basic functionality that an application needs. It is equipped with a method called InitInstance(). Its syntax is:

virtual BOOL InitInstance();

When creating an application, you must override this method in your own class. This method is used to create an application. If it succeeds, it returns TRUE or a non-zero value. If the application could not be created, the method returns FALSE or 0. Here is an example of implementing it:

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

Based on your knowledge of C++, keep in mind that the method could also have been implemented as:

struct CSimpleApp : public CWinApp
{
    BOOL InitInstance()
    {
	return TRUE;
    }
};

or:

struct CSimpleApp : public CWinApp
{
    BOOL InitInstance();
};

BOOL CSimpleApp::InitInstance()
{
    return TRUE;
}

Practical LearningPractical Learning: Instantiating an Application

  1. In the class, implement the InitInstance method as follows:
    #include <afxwin.h>
    
    class CExerciseApp : public CWinApp
    {
        BOOL InitInstance()
        {
    	return TRUE;
        }
    };
    
    CExerciseApp theApp;
  2. Save the file

The Command Line

To execute a program, you must communicate its path and possibly some additional parameters to the compiler. This information is called the command line information and it is supplied as a string. You need to keep that in mind although all programs of our lessons will be compiled inside of Visual C++. The command line information is supplied to the compiler as the lpCmdLine argument of the WinMain() function. Internally, Visual C++ creates the path and communicates it to the compiler when you execute the program. If you want to find out what command line was used to execute your program, you can call the Win32's GetCommandLine() function. Its syntax is:

LPTSTR GetCommandLine(VOID);

This function takes no argument but returns the command line of an application as a null-terminated string.

Introduction to Windows Resources

 

Overview

In an MFC application, a resource is a text file that allows the compiler to manage such objects as pictures, sounds, mouse cursors, dialog boxes, etc. Microsoft Visual C++ makes creating a resource file particularly easy by providing the necessary tools in the same environment used to program. This means you usually do not have to use an external application to create or configure a resource file (as done in other programming environments).

Creating a Resource

Although an application can use various resources that behave independently of each other, these resources are grouped into a text file that has the .rc extension. You can create this file manually and fill out all necessary parts but it is advantageous to let Microsoft Visual C++ create it for you. To start creating the resources:

  • On the main menu, you can click Project -> Add Resource...
  • In the Solution Explorer, you can right-click the name of the project, position the mouse on Add, and click Resource...

Any of these actions would display the Add Resource dialog box. From this dialog box, you would select the type of resource you want and click New (or Import...). You will then be expected to design or customize the resources. Throughout our lessons, we will see various examples. After creating or designing a resource, you must save it. In most cases, you can try closing the resource. In this case, Microsoft Visual C++ would prompt you to save the resource. If you agree to do this (which you mostly should), Microsoft Visual Studio would create a new file with the extension .rc.

To make your resource recognizable to the other files of the program, you must also create a header file, usually called resource.h. This header file must provide a constant integer that identifies each resource and makes it available to any part that needs it. This also means that most, if not all, of your resources will be represented by an identifier.

Because resources are different entities, they are created one at a time. They can also be imported from existing files.

The Add Resource dialog box provides an extensive list of resources to accommodate almost any need. Still, if you do not see a resource you need and know you can use it, you can add it manually to the .rc file before executing the program.

Practical LearningPractical Learning: Creating a Resource

  1. If the Resource View is not visible, on the main menu, click View -> Other Windows -> Resource View.
    In the Reource View, right-click Exercise1 -> Add -> Resource...
  2. In the Add Resource dialog box, click String Table
     
    Add Resource
  3. Click New
  4. Under the Type column header, click IDS_STRING101 and edit it to display IDS_ANNOUNCE and press Enter
  5. To save, on the Standard toolbar, click the Save button
  6. Close that window by clicking the Close button Close
  7. In the Resource View, expand the Exercise1 node et notice the Exercise1.rc node

Converting a Resource Identifier

An identifier is a constant integer whose name usually starts with ID. Although in Win32 programming you usually can use the name of a resource as a string, in MFC applications, resources are usually referred to by their identifier. To make an identifier name (considered a string) recognizable to an MFC (or Win32) function, you use a macro called MAKEINTRESOURCE. Its syntax is:

LPTSTR MAKEINTRESOURCE(WORD IDentifier);

This macro takes the identifier of the resource and returns a string that is given to the function that called it. In the strict sense, after creating the resource file, it must be compiled to create a new file that has the extension .res. Fortunately, Microsoft Visual C++ automatically compiles the file and links it to the application. 

 
 
   
 

Previous Copyright © 2010-2016, FunctionX Next