Home

Managing Dialog Boxes

    

Introduction to Messages

 

Description

 

Some of your applications will be made of various objects. Most of the time, more than one application is running on the computer. These two scenarios mean that the operating system is constantly asked to perform some assignments. Because there can be so many requests presented unpredictably, the operating system leaves it up to the objects to specify what they want, when they want something, and what behavior or result they expect.

The Microsoft Windows operating system cannot predict what kinds of requests one object would need to be taken care of and what type of assignment another object would need. To manage all these assignments and requests, the objects send messages, one message at a time, to the operating system. For this reason, Microsoft Windows is said to be a message-driven operating system.

The messages are divided in various categories but as mentioned already, each object has the responsibility to decide what message to send and when. Therefore, most of the messages we will review here are part of a general window. Particular messages will be addressed when necessary.

Once an object has composed a message, it must send it to the right target, which could be the operating system. Composing and sending a message is referred to as an event. The object is also said to fire an event. To make a distinction between a message and an event, a message's name usually starts with WM_ which stands for Window Message. The name of an event usually starts with On which indicates an action. Remember, the message is what needs to be sent. The event is the action of sending the message.

Practical LearningPractical Learning: Introducing Messages

  1. Start Microsoft Visual Studio
  2. To create a new Application, on the Start Page, click New Project...
  3. In the left list, click Visual C++ and, in the right list, click Win32 Project
  4. Set the Name to CruisePreparation4
  5. Click OK
  6. In the first page of the wizard, click Next
  7. In the second page, click Windows Application and click Empty Project
  8. Click Finish
  9. On the main menu, click Project -> Properties...
  10. In the left list, click Configuration Properties.
    In the right list, change Use of MFC to Use MFC in a Shared DLL
  11. Click OK
  12. To create a dialog box, in the Resource View, right-click CruisePreparation4 -> Add -> Resource...
  13. In the Add Resource dialog box, click Dialog
  14. Click New
  15. Right-click the middle of the dialog box and click Properties
  16. In the Properties window, click ID, type IDD_PREPARATION_DLG and press Enter
  17. Click Caption and type Cruise Preparation
  18. To create a class, on the main menu, click Project -> Add Class...
  19. Make sure C++ Class is selected and click Add
  20. In the Class Name text box, type CPreparationDlg
  21. In the Base Class, type CDialog
  22. Click Finish
  23. Read the message and click Yes
  24. Complete the PreparationDlg.h header file as follows:
    #pragma once
    
    #include "afxwin.h"
    #include "resource.h"
    
    class CPreparationDlg :	public CDialog
    {
    public:
    	enum { IDD = IDD_PREPARATION_DLG };
    
    	CPreparationDlg(void);
    	~CPreparationDlg(void);
    };
  25. Access the PreparationDlg.cpp source file and change it as follows:
    #include "PreparationDlg.h"
    
    CPreparationDlg::CPreparationDlg(void)
    	: CDialog(CPreparationDlg::IDD)
    {
    }
    
    CPreparationDlg::~CPreparationDlg(void)
    {
    }
  26. On the main menu, click Project -> Add Class...
  27. Make sure C++ Class is selected and click Add
  28. In the Class Name text box, type CPreparationApp
  29. In the Base Class, type CWinApp and click Finish
  30. Complete the header file as follows:
    #pragma once
    
    #include "afxwin.h"
    #include <afxdlgs.h>
    
    class CPreparationApp :	public CWinApp
    {
    public:
    	CPreparationApp(void);
    	BOOL InitInstance();
    	~CPreparationApp(void);
    };
  31. Access the Preparation.cpp source file and change it as follows:
    #include "PreparationApp.h"
    #include "PreparationDlg.h"
    
    CPreparationApp::CPreparationApp(void)
    {
    }
    
    BOOL CPreparationApp::InitInstance()
    {
    	CPreparationDlg Dlg;
    
    	m_pMainWnd = &Dlg;
    	Dlg.DoModal();
    
    	return TRUE;
    }
    
    CPreparationApp::~CPreparationApp(void)
    {
    }
    
    CPreparationApp theApp;
  32. Execute the application
  33. Close the dialog box and return to your programming environment

A Map of Messages

For the compiler to manage messages, they should be included in the class definition. The list of messages starts on a section driven by, but that ends with, the DECLARE_MESSAGE_MAP macro. The section can be created as follows:

#include <afxwin.h>
#include <afxdlgs.h>
#include "resource.h"

class CExerciseApp : public CWinApp
{
public:
	BOOL InitInstance();

	DECLARE_MESSAGE_MAP()
};

class CExerciseDlg : public CDialog
{
public:
	enum { IDD = IDD_EXERCISE_DLG };

	CExerciseDlg();
	~CExerciseDlg();

	DECLARE_MESSAGE_MAP()
};

The DECLARE_MESSAGE_MAP macro should be provided at the end of the class definition. The actual messages (as we will review them shortly) should be listed just above the DECLARE_MESSAGE_MAP line. This is mostly a suggestion. In some circumstances, and for any reason, you may want, or have, to provide one message or a few messages under the DECLARE_MESSAGE_MAP line.

To implement the messages, you should/must create a table of messages that your program is using. This table uses two delimiting macros. It starts with a BEGIN_MESSAGE_MAP and ends with an END_MESSAGE_MAP macros. The BEGIN_MESSAGE_MAP macro takes two arguments, the name of your class and the MFC class you derived your class from. An example would be:

BEGIN_MESSAGE_MAP(CSimpleFrame, CFrameWnd)

Like the DECLARE_MESSAGE_MAP macro, END_MESSAGE_MAP takes no argument. Its job is simply to specify the end of the list of messages. The table of messages can be created as follows:

#include <afxwin.h>
#include <afxdlgs.h>
#include "resource.h"

class CExerciseApp : public CWinApp
{
public:
	BOOL InitInstance();

	DECLARE_MESSAGE_MAP()
};

class CExerciseDlg : public CDialog
{
public:
	enum { IDD = IDD_EXERCISE_DLG };

	CExerciseDlg();
	~CExerciseDlg();

	DECLARE_MESSAGE_MAP()
};

CExerciseDlg::CExerciseDlg()
	: CDialog(CExerciseDlg::IDD)
{
}

BEGIN_MESSAGE_MAP(CExerciseDlg, CDialog)

END_MESSAGE_MAP()

CExerciseDlg::~CExerciseDlg()
{
}

BEGIN_MESSAGE_MAP(CExerciseApp, CWinApp)

END_MESSAGE_MAP()

BOOL CExerciseApp::InitInstance()
{
	CExerciseDlg Dlg;

	m_pMainWnd = &Dlg;
	Dlg.DoModal();

	return TRUE;
}

CExerciseApp theApp;

There various categories of messages the operating system receives. Some of them come from the keyboard, some from the mouse, and some others from various other origins. For example, some messages are sent by the application itself while some other messages are controlled by the operating.

Practical Learning: Creating a Map of Messages

  1. Click the PreparationApp.h tab to access its file and change it as follows:
    #pragma once
    
    #include "afxwin.h"
    #include <afxdlgs.h>
    
    class CPreparationApp :	public CWinApp
    {
    public:
    	CPreparationApp(void);
    	BOOL InitInstance();
    	~CPreparationApp(void);
    
    	DECLARE_MESSAGE_MAP()
    };
  2. Click the PreparationApp.cpp tab to access its file and change it as follows:
    #include "PreparationApp.h"
    #include "PreparationDlg.h"
    
    BEGIN_MESSAGE_MAP(CPreparationApp, CWinApp)
    
    END_MESSAGE_MAP()
    
    CPreparationApp::CPreparationApp(void)
    {
    }
    
    BOOL CPreparationApp::InitInstance()
    {
    	CPreparationDlg Dlg;
    
    	m_pMainWnd = &Dlg;
    	Dlg.DoModal();
    
    	return TRUE;
    }
    
    CPreparationApp::~CPreparationApp(void)
    {
    }
    
    CPreparationApp theApp;
  3. Click the PreparationDlg.h tab to access its file and change it as follows:
    #pragma once
    
    #include "afxwin.h"
    #include "resource.h"
    
    class CPreparationDlg :	public CDialog
    {
    public:
        enum { IDD = IDD_PREPARATION_DLG };
    
        CPreparationDlg(void);
        ~CPreparationDlg(void);
    
        DECLARE_MESSAGE_MAP()
    };
  4. Click the PreparationDlg.cpp tab to access its file and change it as follows:
    #include "PreparationDlg.h"
    
    CPreparationDlg::CPreparationDlg(void)
    	: CDialog(CPreparationDlg::IDD)
    {
    }
    
    BEGIN_MESSAGE_MAP(CPreparationDlg, CDialog)
    	
    END_MESSAGE_MAP()
    
    CPreparationDlg::~CPreparationDlg(void)
    {
    }
  5. Test the application and return to your programming environment

Adding Messages to a Dialog Box

 

Manually Adding Messages

A message is primarily implemented like a normal member function of the dialog box's class. This means that you should declare a method in the header file. This could look as follows:

#pragma once

#include "afxwin.h"
#include "resource.h"

class CExerciseDlg :	public CDialog
{
public:
	enum { IDD = IDD_EXERCISE_DLG };

	CExerciseDlg();
	~CExerciseDlg();

	ReturnType FunctionName(Arguments);
	
	DECLARE_MESSAGE_MAP()
};

After declaring the method, you should implement it the source file. This could look as follows:

#include "ExerciseDlg.h"

CExerciseDlg::CExerciseDlg(void)
	: CDialog(CExerciseDlg::IDD)
{
}

BEGIN_MESSAGE_MAP(CExerciseDlg, CDialog)
	
END_MESSAGE_MAP()

CExerciseDlg::~CExerciseDlg()
{
}

ReturnType CExerciseDlg::FunctionName(Arguments)
{

}

Because there are many messages intended for different reasons, the formula of each method is different. That is, the return type, the argument, and the number of arguments depend on the type of message.

The messages in Microsoft Windows are part of what is referred to as the application framework (afx). For this reason, they are created with the afx_msg tag. This could look as follows:

#pragma once

#include "afxwin.h"
#include "resource.h"

class CExerciseDlg :	public CDialog
{
public:
    enum { IDD = IDD_EXERCISE_DLG };

    CExerciseDlg();
    ~CExerciseDlg();

    afx_msg ReturnType FunctionName(Arguments);
	
    DECLARE_MESSAGE_MAP()
};

After creating and implementing the method, you must indicate what message it represents. To do this,

:

#include "ExerciseDlg.h"

CExerciseDlg::CExerciseDlg(void)
	: CDialog(CExerciseDlg::IDD)
{
}

BEGIN_MESSAGE_MAP(CExerciseDlg, CDialog)
    ON_WM_MESSAGE()
END_MESSAGE_MAP()

CExerciseDlg::~CExerciseDlg()
{
}

ReturnType CExerciseDlg::FunctionName(Arguments)
{

}

Automatically Generating a Message

There are two ways you can create an event for a dialog box (or for any object). If the dialog box is being designed, in the Properties window, click the Messages button Messages. This would display a list of messages:

Messages

To generate an event, click the message's name to reveal a combo box. Then, on its right side, click the arrow of the combo box and select the <Add> option:

Messages

You can also use the MFC Class Wizard to get an event of your choice for a dialog box. To do this, in the MFC Class Wizard, after selecting the class of the dialog box in the Class Name combo box, click the Messages tab. In the Messages list, locate the message you want:

MFC Class Wizard

Then, either double-click the message or click it once and click the Add Handler button. Once you are ready, click OK. 

 
 
 

Fundamental Dialog Box Messages

 

Creating a Dialog Box

When an object such as a dialog box is created, it sends a message named ON_WM_CREATE. Its syntax is:

afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

This message takes one argument, which is a pointer to a CREATESTRUCT class. The CREATESTRUCT class provides all the information needed to create a window. It is defined as follows:

typedef struct tagCREATESTRUCT {
   LPVOID    lpCreateParams;
   HANDLE    hInstance;
   HMENU     hMenu;
   HWND      hwndParent;
   int       cy;
   int       cx;
   int       y;
   int       x;
   LONG      style;
   LPCSTR    lpszName;
   LPCSTR    lpszClass;
   DWORD     dwExStyle;
} CREATESTRUCT;

When sending the ON_WM_CREATE message, the dialog box is usually created without your intervention but when calling it, you should check that the window has been created. This can be done by checking the result returned by the OnCreate() message from the parent class. If the message returns 0, the window was created. If it returns -1, the class was not created or it would simply be destroyed.

Before processing your own implementation of the ON_WM_CREATE message, you should call the implementation of the parent class and find out what value it returns. If the parent return 0, then you can proceed. Once you have done this, you can define your own version. This can be done as follows:

int CExerciseDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here

	return 0;
}

To use this message, in the class definition, type its syntax. In the message table, type the name of the message ON_WM_CREATE():

BEGIN_MESSAGE_MAP(CExerciseDlg, CDialog)
	ON_WM_CREATE()
END_MESSAGE_MAP()

Initializing a Dialog Box

As the most regular host of Windows Controls, a dialog box must be able to handle messages that not only pertain to its own existence but also to the availability, visibility, and behaviors of the controls it is holding. For example, if you create a control and position it on the dialog box, sometimes you will need to initialize it by giving it primary values.

The WM_INITDIALOG message fires the OnInitDialog() event after a dialog box has been created but before it displays. This event should be your favorite place to initialize a control that is hosted by the dialog box. Do not initialize controls in your dialog's constructor. Use the constructor only to allocate memory for a dynamically created control using the new operator.

If you create a dialog-based application using the MFC Application option, the wizard automatically generates the WM_INITDIALOG message for you. If you add a dialog box to an existing application, this message may not be added automatically. You would have to fire it. In reality, the WM_INITDIALOG message is inherited from the CDialog class. Therefore, you would only be overriding it.

Painting a Dialog Box

After creating the dialog box, if you want to paint it or draw something on it, such as changing its background color, use the WM_PAINT message. If you create a dialog-based application, this message is automatically generated by the wizard. If you add a dialog-based object to your application and need the OnPaint() event, you would have to add it yourself.

Scrolling a Dialog Box

If a dialog object is equipped with one or two scroll bars or one of its controls is based on scrolling operations, when the user clicks a scroll bar, the OnHScroll() or the OnVScroll() event is sent based on the WM_HSCROLL or the WM_VSCROLL messages respectively.

Closing a Dialog Box

As mentioned already, a regular dialog box is equipped with the system Close button on its title bar. This allows the user to close it any time. As a programmer, one of your jobs is to control your application, to be able to predict "bad moves" from the user. For example, imagine you create a dialog-based object without a Cancel button but with OK (like the most basic message box). If the user clicks the system Close button , you may not know what the user did. The WM_CLOSE message fires the OnClose() event when the user clicks the system Close button. It is important to understand that WM_CLOSE is a message and not a method, meaning it sends a message, it does not take an action. This implies that the OnClose() event fires when the user makes an attempt to close the dialog but before the dialog is actually closed.

You can use the OnClose() event to find out what has been done on the dialog prior to the user's attempt to close it. You can also use it to deny closing the dialog box, to warn the user about something, or to do anything that you judge necessary.

If you perform some processing, such as validating some values, when the user attempts to close the dialog box, if you still want to close the dialog box, call the parent event handler with CDialog::OnClose(). In fact, this line of code is added to the event if you generate it using the wizard:

void CExerciseDlg::OnClose()
{
	// TODO: Add your message handler code here and/or call default

	CDialog::OnClose();
}

If you want to conditionally close the dialog box, you can write a conditional statement that can check whether something is necessary before closing it. If you don't want to close the dialog box, don't call CDialog::OnClose().

Destroying a Dialog Box

Once a dialog box, in fact any (CWnd) window object, has been closed, it must be destroyed so the memory it was using can be reclaimed. If you want to do something before the object is destroyed, use the WM_DESTROY to fire the OnDestroy() event. Before doing anything on this event, first call the OnDestroy() event of the parent class. this can be done as follows:

void CExerciseDlg::OnDestroy()
{
	CDialog::OnDestroy();

	// TODO: Add your message handler code here
}

Other Techniques of Creating a Dialog-Based Application

 

Introduction

As a very early implementer of the Win32 library, the MFC provides various ways of creating a dialog-based application. As we saw already, you can work from scratch. Otherwise, Microsoft Visual Studio provides all the tools and accessories you need. It allows you to combine your knowledge of C++, the MFC library, and other accessories. For example, in the above example, we put all of our code in one file. Most of the time, you will want to spread code in various files, in accordance with the C++ language.

Practical LearningPractical Learning: Creating a Dialog-Based Application

  1. To create a new Application, on the main menu, click File -> New Project
  2. In the left list, click Visual C++
  3. In the right list, click Win32 Project
  4. Set the Name to CruisePreparation2
  5. Click OK
  6. In the first page of the wizard, click Next
  7. In the second page, click Windows Application and click Empty Project
  8. Click Finish
  9. On the main menu, click Project -> Properties...
  10. In the left list, click Configuration Properties.
    In the right list, change Use of MFC to Use MFC in a Shared DLL
  11. Click OK
  12. To create a dialog box, in the Resource View, right-click CruisePreparation2 -> Add -> Resource...
  13. In the Add Resource dialog box, click Dialog
  14. Click New
  15. Right-click the middle of the dialog box and click Properties
  16. In the Properties window, click ID, type IDD_PREPARATION_DLG and press Enter
  17. Click Caption and type Cruise Preparation
  18. To create a class, in the Class View, right-click CruisePreparation2 -> Add -> Class...
  19. Make sure C++ Class is selected and click Add
  20. In the Class Name text box, type CPreparationDlg
  21. In the Base Class, type CDialog
  22. Click Finish.
    You will receive a message box stating that the base class was not found on the project:
     
    Dialog Box
  23. Read the message and click Yes
  24. Complete the PreparationDlg.h header file as follows:
    #pragma once
    
    #include "afxwin.h"
    #include "resource.h"
    
    class CPreparationDlg :	public CDialog
    {
    public:
    	enum { IDD = IDD_PREPARATION_DLG };
    
    	CPreparationDlg(void);
    	~CPreparationDlg(void);
    };
  25. Click the PreparationDlg.cpp tab and complete the source file as follows:
    #include "PreparationDlg.h"
    
    CPreparationDlg::CPreparationDlg(void)
    	: CDialog(CPreparationDlg::IDD)
    {
    }
    
    CPreparationDlg::~CPreparationDlg(void)
    {
    }
  26. In the Class View, right-click CruisePreparation2 -> Add -> Class...
  27. Make sure C++ Class is selected and click Add
  28. In the Class Name text box, type CPreparationApp
  29. In the Base Class, type CWinApp and click Finish
  30. Read the message and click Yes
  31. Complete the header file as follows:
    #pragma once
    
    #include "afxwin.h"
    #include <afxdlgs.h>
    
    class CPreparationApp :	public CWinApp
    {
    public:
    	CPreparationApp(void);
    	BOOL InitInstance();
    	~CPreparationApp(void);
    };
    
    
  32. Click the Preparation.cpp tab to get to the file and change it as follows:
    #include "PreparationApp.h"
    #include "PreparationDlg.h"
    
    CPreparationApp::CPreparationApp(void)
    {
    }
    
    BOOL CPreparationApp::InitInstance()
    {
    	CPreparationDlg Dlg;
    
    	m_pMainWnd = &Dlg;
    	Dlg.DoModal();
    
    	return TRUE;
    }
    
    CPreparationApp::~CPreparationApp(void)
    {
    }
    
    CPreparationApp theApp;
  33. Execute the application 
  34. Close the dialog box and return to your programming environment

The MFC Wizard for a Dialog-Based Application

Microsoft Visual C++ provides an easier way to create an application that is mainly based on a dialog box. To use this technique, start a new project and specify that you want to create an MFC Application. In the MFC Application Wizard, set the Application Type to Dialog Based.

Practical LearningPractical Learning: Using the Wizard to create a Dialog-Based Application

  1. On the main menu, click File -> New Project...
  2. In the New Project dialog box, in the Templates list, click MFC Application
  3. Set the Project Name to CruisePreparation3
    New Project
  4. Click OK
  5. In the first page of the MFC Application Wizard, click Next
  6. In the second page of the wizard, on the right side, click the Dialog Based radio button
  7. Click Next (or click User Interface Features)
  8. In the third page of the wizard, click About Box to remove its check box
  9. Under Dialog Title, edit the text to display Cruise Preparation
  10. Click Next (or click Advanced Features)
  11. Click Next (or click Generated Classes)
  12. In the Generated Classes list, click CCruisePreparation3App and, in the Class Name, replace the text with CPreparationApp
  13. In the Generated Classes list, click CCruisePreparation3Dlg
    In the Class Name, replace the name with CPreparationDlg
    In the .h file edit box, replace the name of the file with PreparationDlg.h
    In the .cpp file edit box, replace the name of the file with PreparationDlg.cpp
    Make sure the Base Class is set to CDialog
     
    Dialog Box
  14. Click Finish
  15. Execute the application to see the result
  16. Close the dialog box return to your programming environment

Managing the Class of a Dialog Box

 

Introduction

As you may have realized by now, a dialog box is created in various objects and files: the "physical" object that is being designed, the representation of the dialog box in the resource file that has the .rc extension, the header resource file that has a .h extension, and the class associated with the dialog box (a header file and/or a source file). Microsoft Visual C++ provides many tools used to create and manage all aspects of a dialog box.

Dealing With the Resource File

In the Win32 and MFC programming, the resource file is probably the most important aspect of a dialog box. It is used to hold the description of the object. The compiler refers to it when it comes time to display the dialog box (unless it is manipulated with code). Most of the time, you will visually design your dialog box and the objects positioned on it as we will see starting in the next lesson. Sometimes, there will be problems. You may make a change in the designed but for some reason, either the .rc file doesn't get updated, the resource.h doesn't receive the change, or both ignore the new modification. When any of these situations happens, either you will receive a bad message immediately or the compiler would display a bad message box when you compile the project, and the intended result would not appear. In such cases, you can continue making changes or the dialog box or, in some cases, you will have to open the resource file and manually make the necessary changes.

As we saw in Lesson 2, when you create an application, you should also create a resource file, a file with the .rc extension. If you start a project using a wizard, the resource file is automatically created for you. The resource file is text-based. This means that you can open it using any text editor.

By default, in the Solution Explorer, if you double-click a a file with .rc extension, the Resource View would display. In the Resource View, to access any object, you can expand its parent folder and double-click the object such as a dialog box. The "physical" object would display, ready to be designed. An alternative is to open the resource file as text.

Before opening a resource file as text, you should close any opened dialog box that is being designed. To open a resource file as text, in the Solution Explorer, right-click the file with .rc extension and click Open With... In the Open With dialog box, click Classic Editor (Text)

Open With

Click OK. You may receive a message box indicating that the resource file is already open and asking you whether you want to open it. If so, click Yes. In the file, locate the section you want. Here is an example of a section for the dialog box:

/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_EXAMPLE_DLG DIALOGEX 0, 0, 224, 126
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "Example"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,113,105,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,167,105,50,14
END

Once you get to the section, you can make the necessary changes.

Don't change anything you are are sure of. In other words, change only something you know for sure needs to be changed. If you don't know or don't understand why something is there, don't touch it. Whenever in doubts or you are having a second tought, don't change anything. Make a change only if you know exactly what you are doing. Any change you make will have to be understood by the compiler and the studio. If the studio doesn't understand something, the dialog box or any object on it would either be lost or show weird behavior.

After making changes to the resource file, save it.

Dealing With the Resource Header File

Besides the "physical" resource file, we already know that an application also has a resource header file named Resource.h (or resource.h). This too is a text-based file that you can open and manipulate. For example, you may change the identifier of a dialog box but for some reason the resource header file would not update itself. You can open and change it.

To open a resource header file, in the Solution Explorer, double-click the Resource.h (or resource.h) file. Locate the identifier you want to modify and change it.

Creating and Managing Member Variables

A class associated with a dialog box is primarily a normal C++-based class. That is, it can have member variables. In many classes you use, the studio will automatically insert some member variables. In the next lessons, we see some ways of adding member variables associated with objects positioned on the dialog box. Still, because we are dealing with a normal class, you can add any members you judge necessary.

There are various ways you can add a member variable to a class:

  • In the Solution Explorer, double-click the header file (or the source file in some cases) to open it. In the file, manually declare the variable as you see fit. If you had declared the variable in the header file, you can access the class's constructor in the source file to initialize the variable
  • In the Class View, right-click the name of the class, position the mouse on Add, and click Add Variable... This would open the Add Member Variable Wizard. Fill it with the necessary options and click OK
  • In the Class View, click the name of the class to select it. On the main menu, click Project -> Add Variable...
  • In the MFC Class Wizard, click the Member Variables tab. In the Class Name combo box, select the class for which you want to add a variable. On the right side, click the Add Custom button. This would open the Add Member Variable dialog box that you should complete as you see fit and click OK

Once a member variable exists, you can perform some operations on it. For example, to access a variable:

  • In the Solution Explorer, double-click the header file (or the source file in some cases) to open it. In the file, locate the variable you want
  • In the upper-level of the Class View, click the name of the class. In the lower-level:
    • Double-click the name of the variable
    • If the variable was declared in the header file and you want to see how it was declared, right-click the variable and click Go To Declaration
    • Right-click the name of the variable and click Go To Definition
  • In the MFC Class Wizard, click the Member Variables tab. In the Member Variables list, either double-click the name of the variable or click the variable and click Edit Code

One of the operations you can perform on a variable consists of changing or renaming it. You can access the variable in the header file of the class and edit its name. The drawback is that you will have manually locate the variable wherever it is being used and edit its name. This can be cumbersome if the variable has been used in various parts of the project and there is a chance you will forget it in some parts. Fortunately, Microsoft Visual Studio can assist you with this operation. You can rename the variable in one place and its name would be changed everywhere.

To rename a member variable, in the top section of the Class View, click the name of the class. In the lower section, right-click the name of the variable and click Properties. In the (Name) field, select the name, change it, and press Enter. In the same way, using the Properties window, you can change any option about the variable.

Creating and Managing Member Functions

Besides member variables, a class can have member functions. If you start a project from a wizard, the studio will create some classes and add some member functions to it. You too can (and will) add member functions that you need.

There are many ways you can add a member function to a class:

  • In the Solution Explorer, double-click the header file to access the foundation of the class. In the header file, manually declare the function. Then, access the source file and define the function as you see fit
  • In the Class View, right-click the name of the class, position the mouse on Add, and click Add Function... This would open the Add Member Function Wizard. Complete it with the appropriate options and click OK
  • In the Class View, click the name of the class to select it. On the main menu, click Project -> Add Function... This would open the Add Member Function Wizard
  • In the MFC Class Wizard, if you want to add a virtual function, click the Virtual Functions tab. In the Virtual Functions list, click the name of the function you want and click Add Function. Then click OK
  • In the MFC Class Wizard, if you want to create a normal member function, click the Methods tab. Click the Add Method button. This would open the Add Method dialog box. Fill it with the necessary information and click OK

Just as mentioned for a member variable, you can manage a member function. To open a function:

  • In the Solution Explorer, double-click the header file or the source file. While in either the header file or the source, in the right combo box of the Code Editor, select the function you want
  • In the upper-level of the Class View, click the name of the class. In the lower-level:
    • Double-click the function
    • To see where the function was declared, right-click it and click Go To Declaration
    • To see how the function is implemented, right-click it and click Go To Definition
  • In the MFC Class Wizard, click the Member Variables tab. In the Member Variables list, either double-click the name of the variable or click the variable and click Edit Code

As done for a variable, you can rename a function. The best way to rename a function is to get assistance from Microsoft Visual Studio. To do this, in the top section of the Class View, click the name of the class. In the lower section, click the name of the function. In the (Name) field of the Properties window, change or edit the name, and press Enter. From the the Properties window, you can change any other option you want about the member function.

 
 
   
 

Previous Copyright © 2010 FunctionX, Inc. Home