1
votes

I have created a MFC SDI application in Visual Studio 2010 and want to open a modal dialog from a custom menu entry in the menu mainframe.

After creating the dialog resource I added a class called Dialog1 to it. It is extended from CDialogEx. Afterwards I right clicked on a custom menu entry in the mainframe/menu bar and chose "Add Event Handler". In the following window I chose to add functions for COMMAND and UPDATE_COMMAND_UI to my class Dialog. After adding the code to call the dialog my source file ("Dialog1.cpp") looks like this:

#include "stdafx.h"
#include "MFCtest.h"
#include "Dialog1.h"
#include "afxdialogex.h"

IMPLEMENT_DYNAMIC(Dialog1, CDialogEx)

Dialog1::Dialog1(CWnd* pParent /*=NULL*/)
    : CDialogEx(Dialog1::IDD, pParent)
{

}

Dialog1::~Dialog1()
{
}

void Dialog1::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(Dialog1, CDialogEx)
    ON_COMMAND(ID_DIALOG_D1, &Dialog1::OnDialogD1)
    ON_UPDATE_COMMAND_UI(ID_DIALOG_D1, &Dialog1::OnUpdateDialogD1)
END_MESSAGE_MAP()

void Dialog1::OnDialogD1()
{
    Dialog1 dlg;
    dlg.DoModal();
}


void Dialog1::OnUpdateDialogD1(CCmdUI *pCmdUI)
{
}

I know I have proably made a stupid mistake. Thanks for your help.

1

1 Answers

3
votes

You added the handlers to the wrong class. You need to add them to the CMainframe class, not the dialog class.

Your code for bringing up the dialog looks fine, although you might want to capture the return value from DoModal to know if they cancelled the dialog.