7a.wxWidgets – wxMessageDialog

OVERVIEW

In the sample code below, we will invoke 4 different kinds of system dialogs when a button is clicked.

SAMPLE CODE

dialog-messages.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
	#include <wx/wx.h>
#endif


class DialogMessages: public wxFrame {
	public:
		DialogMessages(const wxString& title);
		void OnClick(wxCommandEvent &event);

};

const int ID_BTN_1 = 101;
const int ID_BTN_2 = 102;
const int ID_BTN_3 = 103;
const int ID_BTN_4 = 104;

dialog-messages.cpp
#include "dialog-messages.h"

DialogMessages::DialogMessages(const wxString& title):
	wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {

	wxPanel *panelMain = new wxPanel(this, -1);
	wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
	panelMain->SetSizer(vbox);
	
	wxButton *btn1 = new wxButton(panelMain, ID_BTN_1, wxT("Info"));
	wxButton *btn2 = new wxButton(panelMain, ID_BTN_2, wxT("Error"));
	wxButton *btn3 = new wxButton(panelMain, ID_BTN_3, wxT("Question"));
	wxButton *btn4 = new wxButton(panelMain, ID_BTN_4, wxT("Exclamation"));

	vbox->Add(btn1, 1, wxALL | wxEXPAND | wxCENTER,5);
	vbox->Add(btn2, 1, wxALL | wxEXPAND | wxCENTER,5);
	vbox->Add(btn3, 1, wxALL | wxEXPAND | wxCENTER,5);
	vbox->Add(btn4, 1, wxALL | wxEXPAND | wxCENTER,5);

	btn1->Bind(wxEVT_BUTTON, &DialogMessages::OnClick, this);
	btn2->Bind(wxEVT_BUTTON, &DialogMessages::OnClick, this);
	btn3->Bind(wxEVT_BUTTON, &DialogMessages::OnClick, this);
	btn4->Bind(wxEVT_BUTTON, &DialogMessages::OnClick, this);
	Center();
}

	
void DialogMessages::OnClick(wxCommandEvent &event) {
	
	int id = event.GetId();
	wxString msg = wxT("");
	wxString title = wxT("");
	int flags = wxOK;
	
	switch (id) {
		case ID_BTN_1:
			title = "Info";
			msg = "This is an informational message";
			break;
		case ID_BTN_2:
			title = "Error";
			msg = "This is an error message";
			flags |= wxICON_ERROR;
			break;

		case ID_BTN_3:
			title = "Question";
			msg = "This is a question";
			flags |= wxICON_QUESTION;
			break;

		case ID_BTN_4:
			title = "Exclamation";
			msg = "This is a message with exclamation";
			flags |= wxICON_EXCLAMATION;
			break;
	}

	wxMessageDialog *dlg = new wxMessageDialog(NULL, msg, title, flags);
	dlg->ShowModal();
}


main.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
	#include <wx/wx.h>
#endif

class Main: public wxApp {

	public:
		virtual bool OnInit();
};
main.cpp
#include "main.h"
#include "dialog-messages.h"

IMPLEMENT_APP(Main)

bool Main::OnInit() {
	DialogMessages *app = new DialogMessages(wxT("Dialog Messages"));
	app->Show(true);

}

The output is shown below

Be the first to comment

Leave a Reply

Your email address will not be published.


*