今天要分享的内容比较简单。
在开始之前先分享一个界面搭建工具wxFormBuilder 正所谓工欲善其事,必先利其器嘛,呵呵呵...下载地址https://github.com/wxFormBuilder/wxFormBuilder/releases,关于wxFormBuilder的使用教程,我就不多加赘述了请看这篇博客https://www.cnblogs.com/lxt287994374/p/3590142.html
好了,请看以下代码
#pragma once
#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/button.h>
#include <wx/string.h>
#include <wx/bitmap.h>
#include <wx/image.h>
#include <wx/icon.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/dialog.h>
class MyDialog : public wxDialog
{
private:
protected:
wxBoxSizer* bSizer1;
wxButton* m_button;
public:
MyDialog( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 304,228 ), long style = wxDEFAULT_DIALOG_STYLE );
~MyDialog(); void OnButton(wxCommandEvent &event);
};
MyDialog::MyDialog( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxDialog( parent, id, title, pos, size, style )
{
this->SetSizeHints( wxDefaultSize, wxDefaultSize );
bSizer1 = new wxBoxSizer( wxVERTICAL );
m_button = new wxButton( this, wxID_ANY, wxT("MyButton"), wxDefaultPosition, wxDefaultSize, 0 );
bSizer1->Add( m_button1, 0, wxALL, 5 );
this->SetSizer( bSizer1 );
this->Layout();
this->Centre( wxBOTH );
m_button->Bind(wxEVT_BUTTON, &MyDialog::OnButton, this);
}
MyDialog::~MyDialog()
{
m_button->Unbind(wxEVT_BUTTON, &MyDialog::OnButton, this);
}
void OnButton(wxCommandEvent &event)
{
this->EndModal(wxID_OK);
}
在外层代码中调用
MyDialog dlg (this);
int ret = dlg.ShowModal();
运行之后如下图:
当用户点击图中按钮 则会执行OnButton函数,从而执行this->EndModal(wxID_OK);这就是模态对话框的代码,也只有在模态对话框才能使用EndModal()。
关于模态对话框的使用,很对情况下还会用到模态对话款的返回值,就是EndModal带出来的。在某些业务场景是很好用的,即用户操作完准备关闭对话框,则可以根据用户点击按钮的不同来返回不同的值。