wxWidget入门(五)

这一期讲一讲提示框

毕竟为了界面友好性和使用性考虑,有必要做一些提示框,留下那些想走的人,和提醒那些作死的人。

边贴代码边说明好了

这次做四种消息提示

分别是:

  1. 信息提示
  2. 错误提示
  3. 问题提示
  4. 警告提示
#include 

class Message : public wxFrame
{
public:
	Message(const wxString &title);

	void ShowMessage1(wxCommandEvent &event);
	void ShowMessage2(wxCommandEvent &event);
	void ShowMessage3(wxCommandEvent &event);
	void ShowMessage4(wxCommandEvent &event);
};

设置下四种消息的flag 

const int ID_INFO = 1;
const int ID_ERROR = 2;
const int ID_QUESTION = 3;
const int ID_ALERT = 4;

①信息提示:样例展示,下载完成提示 

void Message::ShowMessage1(wxCommandEvent &event)
{
	wxMessageDialog *dial = new wxMessageDialog(NULL,
		wxT("Download completed"), wxT("info"), wxOK);
	dial->ShowModal();
}

②错误提示:

void Message::ShowMessage2(wxCommandEvent& event) 
{
	wxMessageDialog *dial = new wxMessageDialog(NULL, 
		wxT("Error loading file"), wxT("Error"), wxOK | wxICON_ERROR);
	dial->ShowModal();
}

③问题提示:

void Message::ShowMessage3(wxCommandEvent & event)
{
	wxMessageDialog *dial = new wxMessageDialog(NULL,
		wxT("Are you sure to quit"), wxT("Question"),
		wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
	dial->ShowModal();
}

④警告提示:

void Message::ShowMessage4(wxCommandEvent& event) 
{
	wxMessageDialog *dial = new wxMessageDialog(NULL, 
		wxT("Unallowed operation"), wxT("Exclamation"), 
		wxOK | wxICON_EXCLAMATION);
	dial->ShowModal();
}

最后是画几个按钮来触发事件发生。

对于布局不了解的可以查看上一期内容自行学习哈。

Message::Message(const wxString &title)
	:wxFrame(NULL, -1, title, wxPoint(-1, -1), wxSize(300, 240))
{
	wxPanel * panel = new wxPanel(this, -1);

	wxBoxSizer *hbox = new wxBoxSizer(wxVERTICAL);
	wxGridSizer * gs = new wxGridSizer(2, 2, 2, 2);

	wxButton * btn1 = new wxButton(panel, ID_INFO, wxT("Info"));
	wxButton * btn2 = new wxButton(panel, ID_ERROR, wxT("Error"));
	wxButton * btn3 = new wxButton(panel, ID_QUESTION, wxT("Question"));
	wxButton * btn4 = new wxButton(panel, ID_ALERT, wxT("Alert"));

	Connect(ID_INFO, wxEVT_COMMAND_BUTTON_CLICKED,
		wxCommandEventHandler(Message::ShowMessage1));
	Connect(ID_ERROR, wxEVT_COMMAND_BUTTON_CLICKED,
		wxCommandEventHandler(Message::ShowMessage2));
	Connect(ID_QUESTION, wxEVT_COMMAND_BUTTON_CLICKED,
		wxCommandEventHandler(Message::ShowMessage3));
	Connect(ID_ALERT, wxEVT_COMMAND_BUTTON_CLICKED,
		wxCommandEventHandler(Message::ShowMessage4));

	gs->Add(btn1, 1, wxEXPAND);
	gs->Add(btn2, 1, wxEXPAND);
	gs->Add(btn3, 1, wxEXPAND);
	gs->Add(btn4, 1, wxEXPAND);
	hbox->Add(gs, 0, wxALL, 15);
	panel->SetSizer(hbox);
	Centre();
}

结果图片显示: 

wxWidget入门(五)_第1张图片

wxWidget入门(五)_第2张图片wxWidget入门(五)_第3张图片wxWidget入门(五)_第4张图片wxWidget入门(五)_第5张图片

你可能感兴趣的:(wxWidgets学习笔记)