Denis
Denis

Reputation: 5

MessageBox in Dialog windows (VS 2012, MFC C++)

I've created the new dialog window (Visual Studio 2012, dialog based MFC application) and call it from program menu like this:

CDialog dlg(IDD_Dialog1);
dlg.DoModal();

In new window (in IDD_Dialog1) i'm trying to make a MessageBox. By clicking of the button MessageBox isn't showed.

How to make it correctly?

Upvotes: 0

Views: 1632

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31649

Here is some basic code which you shouldn't really need. It's better to use Visual Studio Wizard to make an MFC application, dialog based or something, then go to resource editor, create a dialog, double click on that dialog in resource editor, it gets done for you. While still in resource editor, drag & drop a button to the dialog, double click on that button which you just dropped in...

//mydialog.h
class CMyDialog : public CDialog
{
public:
   CMyDialog(int id, CWnd* parent = NULL);
   void OnButton1();
   DECLARE_MESSAGE_MAP()
};

//mydialog.cpp
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
   ON_COMMAND(IDC_BUTTON1, OnButton1)
END_MESSAGE_MAP()

CMyDialog::CMyDialog(int id, CWnd* parent) : CDialog(id, parent){
}

void CMyDialog::OnButton1(){
   MessageBox(L"hello world");
}

//myapp.cpp
BOOL CMyApp::InitInstance()
{
   CWinApp::InitInstance();
   CMyDialog dlg(IDD_DIALOG1);
   dlg.DoModal();
   return 0;
}

Upvotes: 1

Related Questions