- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Dialogs in wxWidgets
Dialog windows or dialogs are an indispensable part of most modern GUI applications. A dialog is defined as a conversation between two or more persons. In a computer application a dialog is a window which is used to "talk" to the application. A dialog is used to input data, modify data, change the application settings etc. Dialogs are important means of communication between a user and a computer program.
There are essentially two types of dialogs: predefined dialogs and custom dialogs.
Predefined dialogs
Predefined dialogs are dialogs available in the wxWidgets toolkit. These are dialogs for common programming tasks like showing text, receiving input, loading and saving files etc. They save programmer's time and enhance using some standard behaviour.
Message dialogs
Message dialogs are used to show messages to the user. They are customisable. We can change icons and buttons that will be shown in a dialog.
Messages.h
#include <wx/wx.h> class Messages : public wxFrame { public: Messages(const wxString& title); void ShowMessage1(wxCommandEvent & event); void ShowMessage2(wxCommandEvent & event); void ShowMessage3(wxCommandEvent & event); void ShowMessage4(wxCommandEvent & event); }; const int ID_INFO = 1; const int ID_ERROR = 2; const int ID_QUESTION = 3; const int ID_ALERT = 4;
Messages.cpp
#include "Messages.h" Messages::Messages(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(210, 110)) { wxPanel *panel = new wxPanel(this, wxID_ANY); wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL); 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(Messages::ShowMessage1)); Connect(ID_ERROR, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Messages::ShowMessage2)); Connect(ID_QUESTION, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Messages::ShowMessage3)); Connect(ID_ALERT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Messages::ShowMessage4)); gs->Add(btn1, 1, wxEXPAND); gs->Add(btn2, 1); gs->Add(btn3, 1); gs->Add(btn4, 1); hbox->Add(gs, 0, wxALL, 15); panel->SetSizer(hbox); Center(); } void Messages::ShowMessage1(wxCommandEvent& event) { wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Download completed"), wxT("Info"), wxOK); dial->ShowModal(); } void Messages::ShowMessage2(wxCommandEvent& event) { wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Error loading file"), wxT("Error"), wxOK | wxICON_ERROR); dial->ShowModal(); } void Messages::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 Messages::ShowMessage4(wxCommandEvent& event) { wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Unallowed operation"), wxT("Exclamation"), wxOK | wxICON_EXCLAMATION); dial->ShowModal(); }
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "Messages.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Messages *msgs = new Messages(wxT("Messages")); msgs->Show(true); return true; }
In our example, we have created four buttons and put them in a grid sizer. These buttons will show four different dialog windows. We create them by specifying different style flags.
wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Error loading file"), wxT("Error"), wxOK | wxICON_ERROR); dial->ShowModal();
The creation of the message dialog is simple. We set the dialog to be a toplevel window by providing NULL
as a parent. The two strings provide the message text and the dialog title. We show an OK button and an error icon by specifying the wxOK
and wxICON_ERROR
flags. To show the dialog on screen, we call the ShowModal()
method.
wxFileDialog
This is a common dialog for opening and saving files.
openfile.h
#include <wx/wx.h> class Openfile : public wxFrame { public: Openfile(const wxString& title); void OnOpen(wxCommandEvent& event); wxTextCtrl *tc; };
openfile.cpp
#include "openfile.h" Openfile::Openfile(const wxString & title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200)) { wxMenuBar *menubar = new wxMenuBar; wxMenu *file = new wxMenu; file->Append(wxID_OPEN, wxT("&Open")); menubar->Append(file, wxT("&File")); SetMenuBar(menubar); Connect(wxID_OPEN, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Openfile::OnOpen)); tc = new wxTextCtrl(this, -1, wxT(""), wxPoint(-1, -1), wxSize(-1, -1), wxTE_MULTILINE); Center(); } void Openfile::OnOpen(wxCommandEvent& event) { wxFileDialog * openFileDialog = new wxFileDialog(this); if (openFileDialog->ShowModal() == wxID_OK){ wxString fileName = openFileDialog->GetPath(); tc->LoadFile(fileName); } }
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "openfile.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Openfile *open = new Openfile(wxT("Openfile")); open->Show(true); return true; }
In our example, we display a open file menu item and a simple multiline text control. If we click on the open file menu item a wxFileDialog
is displayed. We can load some simple text files into the text control.
tc = new wxTextCtrl(this, -1, wxT(""), wxPoint(-1, -1), wxSize(-1, -1), wxTE_MULTILINE);
We load text files into this text control.
wxFileDialog * openFileDialog = new wxFileDialog(this);
Here we create a wxFileDialog. We use the default parameters. (The open file dialog is the default dialog.)
if (openFileDialog->ShowModal() == wxID_OK){ wxString fileName = openFileDialog->GetPath(); tc->LoadFile(fileName); }
Here we show the dialog. We get the selected file name and load the file into the text control.

wxFontDialog
This is a common dialog for choosing a font.
fontdialog.h
#include <wx/wx.h> class ChangeFont : public wxFrame { public: ChangeFont(const wxString& title); void OnOpen(wxCommandEvent& event); wxStaticText *st; }; const int ID_FONTDIALOG = 1;
fontdialog.cpp
#include <wx/fontdlg.h> #include "fontdialog.h" ChangeFont::ChangeFont(const wxString & title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200)) { wxPanel *panel = new wxPanel(this, -1); wxMenuBar *menubar = new wxMenuBar; wxMenu *file = new wxMenu; file->Append(ID_FONTDIALOG, wxT("&Change font")); menubar->Append(file, wxT("&File")); SetMenuBar(menubar); Connect(ID_FONTDIALOG, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(ChangeFont::OnOpen)); st = new wxStaticText(panel, wxID_ANY, wxT("The Agoge"), wxPoint(20, 20)); Center(); } void ChangeFont::OnOpen(wxCommandEvent& WXUNUSED(event)) { wxFontDialog *fontDialog = new wxFontDialog(this); if (fontDialog->ShowModal() == wxID_OK) { st->SetFont(fontDialog->GetFontData().GetChosenFont()); } }
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "fontdialog.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { ChangeFont *change = new ChangeFont(wxT("Change font")); change->Show(true); return true; }
In this example, we will change the font of a static text example.
st = new wxStaticText(panel, wxID_ANY, wxT("The Agoge"), wxPoint(20, 20));
Here we display a static text on the panel. We will change its font using the wxFontDialog
.
wxFontDialog *fontDialog = new wxFontDialog(this); if (fontDialog->ShowModal() == wxID_OK) { st->SetFont(fontDialog->GetFontData().GetChosenFont()); }
In these code lines, we show the font dialog. Then we get the choosen font. And finally, we change the font of the static text, we created earlier.

A custom dialog
In the next example we create a custom dialog. An image editing application can change a color depth of a picture. To provide this funcionality, we could create a suitable custom dialog.
customdialog.h
#include <wx/wx.h> class CustomDialog : public wxDialog { public: CustomDialog(const wxString& title); };
customdialog.cpp
#include "customdialog.h" CustomDialog::CustomDialog(const wxString & title) : wxDialog(NULL, -1, title, wxDefaultPosition, wxSize(250, 230)) { wxPanel *panel = new wxPanel(this, -1); wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL); wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL); wxStaticBox *st = new wxStaticBox(panel, -1, wxT("Colors"), wxPoint(5, 5), wxSize(240, 150)); wxRadioButton *rb = new wxRadioButton(panel, -1, wxT("256 Colors"), wxPoint(15, 30), wxDefaultSize, wxRB_GROUP); wxRadioButton *rb1 = new wxRadioButton(panel, -1, wxT("16 Colors"), wxPoint(15, 55)); wxRadioButton *rb2 = new wxRadioButton(panel, -1, wxT("2 Colors"), wxPoint(15, 80)); wxRadioButton *rb3 = new wxRadioButton(panel, -1, wxT("Custom"), wxPoint(15, 105)); wxTextCtrl *tc = new wxTextCtrl(panel, -1, wxT(""), wxPoint(95, 105)); wxButton *okButton = new wxButton(this, -1, wxT("Ok"), wxDefaultPosition, wxSize(70, 30)); wxButton *closeButton = new wxButton(this, -1, wxT("Close"), wxDefaultPosition, wxSize(70, 30)); hbox->Add(okButton, 1); hbox->Add(closeButton, 1, wxLEFT, 5); vbox->Add(panel, 1); vbox->Add(hbox, 0, wxALIGN_CENTER | wxTOP | wxBOTTOM, 10); SetSizer(vbox); Centre(); ShowModal(); Destroy(); }
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "customdialog.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { CustomDialog *custom = new CustomDialog(wxT("CustomDialog")); custom->Show(true); return true; }
This example is a dialog based application. We illustrate, how to create a custom dialog.
class CustomDialog : public wxDialog
A custom dialog is based on the wxDialog
class.
wxStaticBox *st = new wxStaticBox(panel, -1, wxT("Colors"), wxPoint(5, 5), wxSize(240, 150)); wxRadioButton *rb = new wxRadioButton(panel, -1, wxT("256 Colors"), wxPoint(15, 30), wxDefaultSize, wxRB_GROUP);
Note that wxStaticBox
widget must be created before the widgets that it contains, and that those widgets should be siblings, not children, of the static box.
ShowModal(); Destroy();
To show the dialog on the screen, we call the ShowModal()
method. To clear the dialog from the memory, we call the Destroy()
method.

This part of the wxWidgets tutorial was dedicated to dialogs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论