red_rain
red_rain

Reputation: 398

MFC - How to call a dialog from the menu mainframe

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.

Upvotes: 1

Views: 3428

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308500

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.

Upvotes: 3

Related Questions