Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Visual C++ and MFC Fundamentals programming phần 6 docx
Nội dung xem thử
Mô tả chi tiết
Visual C++ and MFC Fundamentals Chapter 12: Dialog-Based Windows
© FunctionX, Inc. 359
Practical Learning: Creating a Top-Most Window
1. To create the dialog box with a thin border, check its Static Edge check box or set it
to True
2. To indicate that this dialog can accept dragged files, check its Accept Files check
box or set it to True
12.1.8 Creating the Dialog Resource File
As mentioned already, a dialog box means nothing except for the controls it is created to
host. The controls are listed in a section that starts with the BEGIN and ends with the
END keywords. Here is an example:
IDD_SCHOOL_SURVEY DIALOGEX 0, 0, 340, 268
STYLE WS_CAPTION | WS_SYSMENU | WS_POPUP | DS_MODALFRAME
EXSTYLE WS_EX_TOOLWINDOW
CAPTION “School Survey – For Teachers Only”
FONT 8, “MS Shell Dlg”, 400, 0, 0x1
BEGIN
END
If you design a dialog box and the first object of your application, you must save it before
using it. Even if you had decided to manually create the dialog box as a text file, you
must save it before using it. In both cases, whether saving the dialog box designed or the
text file, saving it results in creating a resource file. This file should have the .rc
extension.
If your resource file is using some identifiers, which is the case for most or all controls
you will use in your application, you must list them in a header file. This file is
traditionally called Resource.h or resource.h. Each identifier must be listed using the
following syntax:
#define Identifier Constant
When you save the resource file, Visual C++ automatically creates the resource header
file and names it resource.h
After saving the resource file, it is still considered external to the application. If you are
using MSVC 6, you must add it to your project. After it has been added to your project,
you can continue working on the dialog box, adding, deleting, or manipulating the
controls. After doing that, if you save the resource, Visual C++ automatically makes the
appropriate changes in the resource and the resource header files.
Practical Learning: Saving the Resource File
1. If you are using MSVC 7, on the Standard toolbar, click the Save All button
If you are using MSVC 6:
a. To save the dialog box, click the system Close button of the window that holds
the dialog box.
b. This will display a window with a tree view that starts with Script1. Close its
window also.
Chapter 12: Dialog-Based Windows Visual C++ and MFC Fundamentals
360 © FunctionX, Inc.
c. You will receive a message box asking you whether you want to save the script.
Click Yes
d. Locate the ExoDialog1 folder that was used to create the current project.
Display it in the Save In combo box.
Change the name in the File Name with ExoDialog1
e. Click Save
2. To add the resource to your application, on the main menu, click Project -> Add To
Project -> Files…
3. Click ExoDialog1.rc and click OK
4. To verify that the dialog box has been added, in the Workspace, click the
ResourceView tab and expand the ExoDialog1 Resources node. Then expand the
Dialog folder and double-click IDD_EXERCISE_DLG
5. If you are using MSVC 7, on the Standard toolbar, the Save All. Then
12.1.9 Creating a Class for the Dialog
After creating the resource for the dialog box, you must create a class that will be used to
handle its assignment. To do this, you must derive a class from CDialog. In the header
file of the dialog’s class, define an enumerator whose only member is called IDD and
initialize it with the identifier of the dialog box. Because the identifier is listed in the
resource header file, you must include this resource header in the file in which you are
using the dialog’s identifier.
Practical Learning: Create the Dialog’s Class
1. To create a class for the dialog box, open the Exercise.cpp created earlier and add the
following:
#include <afxwin.h>
#include <afxdlgs.h>
#include "resource.h"
class CExerciseApp : public CWinApp
{
public:
Visual C++ and MFC Fundamentals Chapter 12: Dialog-Based Windows
© FunctionX, Inc. 361
BOOL InitInstance();
};
class CExerciseDlg : public CDialog
{
public:
enum { IDD = IDD_EXERCISE_DLG };
};
BOOL CExerciseApp::InitInstance()
{
return TRUE;
}
CExerciseApp theApp;
2. Save All
12.1.10 Dialog Box Methods
A dialog box is based on the CDialog class. As seen above, when creating your dialog
box, you can derive a class from CDialog. The CDialog class itself provides three
constructors as follows:
CDialog();
CDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL);
CDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL);
The default constructor, CDialog(), can be used to declare a variable whose behavior is
not yet known or, for one reason or another, cannot yet be defined. When creating your
class, you should also declare at least a default constructor.
The identifier of the dialog box, such as IDD_DIALO G1, can be used as the first
argument, nIDTemplate, of a CDialog() constructor to create a dialog box from an
existing resource.
If you are using a Win32 template to create your dialog box, pass the name of this
template as a string to a CDialog() constructor, lpszTemplateName.
When implementing your default constructor, initialize the parent CDialog constructor
with the IDD enumerator declared in your class. If your dialog box has an owner, specify
it as the pParentWnd argument. If you set it to NULL, the application will be used as the
dialog’s parent.
If you dynamically create objects for your application using your dialog class, it is a good
idea to also declare and define a destructor. This would be used to destroy such dynamic
objects.
Most other methods of a dialog box depend on circumstances we have not yet reviewed
Practical Learning: Creating a Dialog Box
1. Declare a default constructor and a destructor for your dialog class and implement
them as follows:
class CExerciseDlg : public CDialog
Chapter 12: Dialog-Based Windows Visual C++ and MFC Fundamentals
362 © FunctionX, Inc.
{
public:
enum { IDD = IDD_EXERCISE_DLG };
CExerciseDlg();
~CExerciseDlg();
};
CExerciseDlg::CExerciseDlg()
: CDialog(CExerciseDlg::IDD)
{
}
CExerciseDlg::~CExerciseDlg()
{
}
2. Before using the new class, declare a variable of it as follows:
BOOL CExerciseApp::InitInstance()
{
CExerciseDlg Dlg;
m_pMainWnd = &Dlg;
return TRUE;
}
3. Save All
12.2 Modal Dialog Boxes
12.2.1 Dialog-Based Applications
There are two types of dialog boxes: modal and modeless. A Modal dialog box is one that
the user must first close in order to have access to any other framed window or dialog
box of the same application.
One of the scenarios in which you use a dialog box is to create an application that is
centered around a dialog box. In this case, if either there is no other window in your
application or all the other windows depend on this central dialog box, it must be created
as modal. Such an application is referred to as dialog-based
There are two main techniques you can use to create a dialog-based application: from
scratch or using one of Visual C++ wizards. After creating a dialog resource and deriving
a class from CDialog, you can declare a variable of your dialog class. To display your
dialog box as modal, you can call the CDialog::DoModal() method in your
CWinApp::InitInstance() method. Its syntax is:
virtual int DoModal();
This method by itself does nothing more than displaying a dialog box as modal. We will
learn that you can use this method to find out how the user had closed such a dialog box.
Visual C++ and MFC Fundamentals Chapter 12: Dialog-Based Windows
© FunctionX, Inc. 363
Practical learning: Displaying a Modal Dialog Box
1. To display the dialog box as modal, in the InitInstance() event of your CWinApp
derived class, call the DoModal() method using your dialog variable:
#include <afxwin.h>
#include <afxdlgs.h>
#include "resource.h"
class CExerciseApp : public CWinApp
{
public:
BOOL InitInstance();
};
class CExerciseDlg : public CDialog
{
public:
enum { IDD = IDD_EXERCISE_DLG };
CExerciseDlg();
~CExerciseDlg();
};
CExerciseDlg::CExerciseDlg()
: CDialog(CExerciseDlg::IDD)
{
}
CExerciseDlg::~CExerciseDlg()
{
}
BOOL CExerciseApp::InitInstance()
{
CExerciseDlg Dlg;
m_pMainWnd = &Dlg;
Dlg.DoModal();
return TRUE;
}
CExerciseApp theApp;
2. Test the application
Chapter 12: Dialog-Based Windows Visual C++ and MFC Fundamentals
364 © FunctionX, Inc.
3. Close it and return to MSVC
12.2.2 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 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 ExoDialog2