Visual C++ and MFC Fundamentals Chapter 12: Dialog-Based Windows
dialog’s constructor. Use the constructor only to allocate memory space for a dynamic
control using the new operator.
If you create a dialog-based or a form-based application, AppWizard automatically
generates the WM_INITDIALOG message for you. If you add a dialog box, a form, a
proeprty sheet, or a property page 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.
Practical Learning: Generating Dialog Messages
- Start a new MFC Application and name it DialogMessages
- Create it as a Single Document and click Finish
- To add a new object, display the Add Resource dialog box and double-click Dialog
- Change its ID to IDD_DYNAMIC_DLG and its Caption to Dynamic Objects
- Add a class for it. Name it CDynamicDlg and base it on the CDialog class. Click
Finish - To make sure you can display the dialog box, display the IDR_MAINFRAME menu.
Under View, add a separator. Then add a menu item with the caption as
&Dynamic... and press Enter - Right-click the Dynamic menu item and click Add Event Handler...
- Accept the Message type as COMMAND. Accept the suggested Function Handler
Name. In the Class List, click CMainFrame then click Add and Edit - Implement the event as follows:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "DialogMessages.h"
#include "MainFrm.h"
#include "DynamicDlg.h"
...
void CMainFrame::OnViewDynamic()
{
// TODO: Add your command handler code here
CDynamicDlg Dlg;
Dlg.DoModal();
}
- Test the application and return to MSVC
- Click the Class View tab and, if necessary, expand the DynamicMessages node.
Right-click CDynamicDlg and click Add Variable... - In the Variable Type combo box, type CWnd * and, in the Variable Name edit box,
type m_Panel