Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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



  1. Start a new MFC Application and name it DialogMessages

  2. Create it as a Single Document and click Finish

  3. To add a new object, display the Add Resource dialog box and double-click Dialog

  4. Change its ID to IDD_DYNAMIC_DLG and its Caption to Dynamic Objects

  5. Add a class for it. Name it CDynamicDlg and base it on the CDialog class. Click
    Finish

  6. 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

  7. Right-click the Dynamic menu item and click Add Event Handler...

  8. Accept the Message type as COMMAND. Accept the suggested Function Handler
    Name. In the Class List, click CMainFrame then click Add and Edit

  9. 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();
}


  1. Test the application and return to MSVC

  2. Click the Class View tab and, if necessary, expand the DynamicMessages node.
    Right-click CDynamicDlg and click Add Variable...

  3. In the Variable Type combo box, type CWnd * and, in the Variable Name edit box,
    type m_Panel

Free download pdf