- 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
wxWidgets widgets II
In this chapter, we will continue introducing various other widgets. We will mention a wxListBox
, a wxNotebook
, and a wxScrolledWindow
.
wxListBox
A wxListBox
widget is used for displaying and working with a list of items. As its name indicates, it is a rectangle that has a list of strings inside. We could use it for displaying a list of MP3 files, book names, module names of a larger project or names of our friends. A wxListBox
can be created in two different states. In a single selection state or a multiple selection state. The single selection state is the default state. There are two significant events in wxListBox
. The first one is the wxEVT_COMMAND_LISTBOX_SELECTED
event. This event is generated when we select a string in a wxListBox
. The second one is the wxEVT_COMMAND_LISTBOX_DOUBLE_CLICKED
event. It is generated when we double click an item in a wxListBox
. The number of elements inside a wxListBox
is limited on GTK platform. According to the documentation, it is currently around 2000 elements. The elements are numbered from zero. Scrollbars are displayed automatically if needed.
Listbox.h
#include <wx/wx.h> #include <wx/listbox.h> class MyPanel : public wxPanel { public: MyPanel(wxPanel *parent); void OnNew(wxCommandEvent& event); void OnRename(wxCommandEvent& event); void OnClear(wxCommandEvent& event); void OnDelete(wxCommandEvent& event); wxListBox *m_lb; wxButton *m_newb; wxButton *m_renameb; wxButton *m_clearb; wxButton *m_deleteb; }; class Listbox : public wxFrame { public: Listbox(const wxString& title); void OnDblClick(wxCommandEvent& event); wxListBox *listbox; MyPanel *btnPanel; }; const int ID_RENAME = 1; const int ID_LISTBOX = 5;
Listbox.cpp
#include "listbox.h" #include <wx/textdlg.h> Listbox::Listbox(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(270, 200)) { wxPanel * panel = new wxPanel(this, -1); wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL); listbox = new wxListBox(panel, ID_LISTBOX, wxPoint(-1, -1), wxSize(-1, -1)); hbox->Add(listbox, 3, wxEXPAND | wxALL, 20); btnPanel = new MyPanel(panel); hbox->Add(btnPanel, 2, wxEXPAND | wxRIGHT, 10); Connect(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, wxCommandEventHandler(Listbox::OnDblClick)); panel->SetSizer(hbox); Center(); } MyPanel::MyPanel(wxPanel * parent) : wxPanel(parent, wxID_ANY) { wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL); Listbox *lb = (Listbox *) parent->GetParent(); m_lb = lb->listbox; m_newb = new wxButton(this, wxID_NEW, wxT("New")); m_renameb = new wxButton(this, ID_RENAME, wxT("Rename")); m_deleteb = new wxButton(this, wxID_DELETE, wxT("Delete")); m_clearb = new wxButton(this, wxID_CLEAR, wxT("Clear")); Connect(wxID_NEW, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyPanel::OnNew) ); Connect(ID_RENAME, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyPanel::OnRename) ); Connect(wxID_CLEAR, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyPanel::OnClear) ); Connect(wxID_DELETE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyPanel::OnDelete) ); vbox->Add(-1, 20); vbox->Add(m_newb); vbox->Add(m_renameb, 0, wxTOP, 5); vbox->Add(m_deleteb, 0, wxTOP, 5); vbox->Add(m_clearb, 0, wxTOP, 5); SetSizer(vbox); } void MyPanel::OnNew(wxCommandEvent& event) { wxString str = wxGetTextFromUser(wxT("Add new item")); if (str.Len() > 0) m_lb->Append(str); } void MyPanel::OnClear(wxCommandEvent& event) { m_lb->Clear(); } void MyPanel::OnRename(wxCommandEvent& event) { wxString text; wxString renamed; int sel = m_lb->GetSelection(); if (sel != -1) { text = m_lb->GetString(sel); renamed = wxGetTextFromUser(wxT("Rename item"), wxT("Rename dialog"), text); } if (!renamed.IsEmpty()) { m_lb->Delete(sel); m_lb->Insert(renamed, sel); } } void MyPanel::OnDelete(wxCommandEvent& event) { int sel = m_lb->GetSelection(); if (sel != -1) { m_lb->Delete(sel); } } void Listbox::OnDblClick(wxCommandEvent& event) { wxString text; wxString renamed; int sel = listbox->GetSelection(); if (sel != -1) { text = listbox->GetString(sel); renamed = wxGetTextFromUser(wxT("Rename item"), wxT("Rename dialog"), text); } if (!renamed.IsEmpty()) { listbox->Delete(sel); listbox->Insert(renamed, sel); } }
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "Listbox.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Listbox *listbox = new Listbox(wxT("Listbox")); listbox->Show(true); return true; }
listbox = new wxListBox(panel, ID_LISTBOX, wxPoint(-1, -1), wxSize(-1, -1));
This is the constructor of the listbox widget.
In our example, we have a list box and four buttons. The buttons are used to add, rename, delete and clear all items in the listbox.
wxString str = wxGetTextFromUser(wxT("Add new item")); if (str.Len() > 0) m_lb->Append(str);
To add a new string to the listbox, we display a wxGetTextFromUser
dialog. We call the Append()
method to append string to the listbox.
m_lb->Clear();
To clear all items is the easiest action to do. We just call the Clear()
method.
int sel = m_lb->GetSelection(); if (sel != -1) { m_lb->Delete(sel); }
To delete an item, we figure out the selected item. Then we call the Delete()
method.
Renaming an item requires several steps.
wxString text; wxString renamed;
We define two local variables.
int sel = listbox->GetSelection(); if (sel != -1) { text = listbox->GetString(sel); renamed = wxGetTextFromUser(wxT("Rename item"), wxT("Rename dialog"), text); }
We get the selected string and save it to the renamed variable.
if (!renamed.IsEmpty()) { m_lb->Delete(sel); m_lb->Insert(renamed, sel); }
We check whether the renamed variable is empty. This is to avoid inserting empty strings. Then we delete the old item and insert a new one.

wxNotebook
The wxNotebook
widget joins multiple windows with corresponding tabs. You can position the Notebook widget using the following style flags:
wxNB_LEFT
wxNB_RIGHT
wxNB_TOP
wxNB_BOTTOM
The default position is wxNB_TOP
.
Notebook.h
#include <wx/wx.h> #include <wx/notebook.h> #include <wx/grid.h> class Notebook : public wxFrame { public: Notebook(const wxString& title); void OnQuit(wxCommandEvent& event); }; class MyGrid : public wxGrid { public: MyGrid(wxNotebook *parent); };
Notebook.cpp
#include "Notebook.h" Notebook::Notebook(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(400, 350)) { wxNotebook *nb = new wxNotebook(this, -1, wxPoint(-1, -1), wxSize(-1, -1), wxNB_BOTTOM); wxMenuBar *menubar = new wxMenuBar; wxMenu *file = new wxMenu; file->Append(wxID_EXIT, wxT("Quit"), wxT("")); menubar->Append(file, wxT("&File")); SetMenuBar(menubar); Connect(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Notebook::OnQuit)); MyGrid *grid1 = new MyGrid(nb); MyGrid *grid2 = new MyGrid(nb); MyGrid *grid3 = new MyGrid(nb); nb->AddPage(grid1, wxT("Sheet1")); nb->AddPage(grid2, wxT("Sheet2")); nb->AddPage(grid3, wxT("Sheet3")); CreateStatusBar(); Center(); } void Notebook::OnQuit(wxCommandEvent& event) { Close(true); } MyGrid::MyGrid(wxNotebook * parent) : wxGrid(parent, wxID_ANY) { CreateGrid(30, 30); SetRowLabelSize(50); SetColLabelSize(25); SetRowLabelAlignment(wxALIGN_RIGHT, wxALIGN_CENTRE); SetLabelFont(wxFont(9, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD)); for (int i = 0; i < 30 ; i++) { this->SetRowSize(i, 25); } }
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "Notebook.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Notebook *notebook = new Notebook(wxT("Notebook")); notebook->Show(true); return true; }
In this example, we have created a notebook widget with three grids. The notebook widget is positioned at the bottom.
wxNotebook *nb = new wxNotebook(this, -1, wxPoint(-1, -1), wxSize(-1, -1), wxNB_BOTTOM);
Here we create the notebook widget.
nb->AddPage(grid1, wxT("Sheet1")); nb->AddPage(grid2, wxT("Sheet2")); nb->AddPage(grid3, wxT("Sheet3"));
We add three grid objects into the notebook widget.

wxScrolledWindow
This is one of the container widgets. It can be useful, when we have a larger area than a window can display. In our example, we demonstrate such a case. We place a large image into our window. When the window is smaller than our image, Scrollbars are displayed automatically.
scrolledwindow.h
#include <wx/wx.h> class ScrWindow : public wxFrame { public: ScrWindow(const wxString& title); };
scrolledwindow.cpp
#include "scrolledwindow.h" ScrWindow::ScrWindow(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200)) { wxImage::AddHandler(new wxJPEGHandler); wxScrolledWindow *sw = new wxScrolledWindow(this); wxBitmap bmp(wxT("castle.jpg"), wxBITMAP_TYPE_JPEG); wxStaticBitmap *sb = new wxStaticBitmap(sw, -1, bmp); int width = bmp.GetWidth(); int height = bmp.GetHeight(); sw->SetScrollbars(10, 10, width/10, height/10); sw->Scroll(50,10); Center(); }
main.h
#include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); };
main.cpp
#include "main.h" #include "scrolledwindow.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { ScrWindow *sw = new ScrWindow(wxT("ScrolledWindow")); sw->Show(true); return true; }
In our example, we display a picture of a Spis castle.
wxImage::AddHandler(new wxJPEGHandler);
To handle JPG images, we must initiate the wxJPEGHandler
.
wxScrolledWindow *sw = new wxScrolledWindow(this); wxBitmap bmp(wxT("castle.jpg"), wxBITMAP_TYPE_JPEG); wxStaticBitmap *sb = new wxStaticBitmap(sw, -1, bmp);
We create a scroll window and put a static bitmap into it.
sw->SetScrollbars(10, 10, width/10, height/10);
We set the scrollbars.
sw->Scroll(50,10);
We scroll the window a bit.
In this chapter, we continued covering widgets in wxWidgets library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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