原文: http://zetcode.com/gui/wxwidgets/widgetsII/

在本章中,我们将继续介绍其他各种小部件。 我们将提到wxListBoxwxNotebookwxScrolledWindow

wxListBox

wxListBox小部件用于显示和使用项目列表。 顾名思义,它是一个矩形,里面有一个字符串列表。 我们可以使用它来显示 MP3 文件,书籍名称,较大项目的模块名称或朋友名称的列表。 可以在两种不同的状态下创建wxListBox。 在单选状态或多选状态下。 单一选择状态是默认状态。 wxListBox中有两个重要事件。 第一个是wxEVT_COMMAND_LISTBOX_SELECTED事件。 当我们在wxListBox中选择一个字符串时,将生成此事件。 第二个事件是wxEVT_COMMAND_LISTBOX_DOUBLE_CLICKED事件。 当我们双击wxListBox中的项目时会生成该文件。 wxListBox内部的元素数量在 GTK 平台上受到限制。 根据文档,当前大约有 2000 个元素。 元素从零开始编号。 如果需要,滚动条会自动显示。

Listbox.h

  1. #include <wx/wx.h>
  2. #include <wx/listbox.h>
  3. class MyPanel : public wxPanel
  4. {
  5. public:
  6. MyPanel(wxPanel *parent);
  7. void OnNew(wxCommandEvent& event);
  8. void OnRename(wxCommandEvent& event);
  9. void OnClear(wxCommandEvent& event);
  10. void OnDelete(wxCommandEvent& event);
  11. wxListBox *m_lb;
  12. wxButton *m_newb;
  13. wxButton *m_renameb;
  14. wxButton *m_clearb;
  15. wxButton *m_deleteb;
  16. };
  17. class Listbox : public wxFrame
  18. {
  19. public:
  20. Listbox(const wxString& title);
  21. void OnDblClick(wxCommandEvent& event);
  22. wxListBox *listbox;
  23. MyPanel *btnPanel;
  24. };
  25. const int ID_RENAME = 1;
  26. const int ID_LISTBOX = 5;

Listbox.cpp

  1. #include "listbox.h"
  2. #include <wx/textdlg.h>
  3. Listbox::Listbox(const wxString& title)
  4. : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(270, 200))
  5. {
  6. wxPanel * panel = new wxPanel(this, -1);
  7. wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
  8. listbox = new wxListBox(panel, ID_LISTBOX,
  9. wxPoint(-1, -1), wxSize(-1, -1));
  10. hbox->Add(listbox, 3, wxEXPAND | wxALL, 20);
  11. btnPanel = new MyPanel(panel);
  12. hbox->Add(btnPanel, 2, wxEXPAND | wxRIGHT, 10);
  13. Connect(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED,
  14. wxCommandEventHandler(Listbox::OnDblClick));
  15. panel->SetSizer(hbox);
  16. Center();
  17. }
  18. MyPanel::MyPanel(wxPanel * parent)
  19. : wxPanel(parent, wxID_ANY)
  20. {
  21. wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
  22. Listbox *lb = (Listbox *) parent->GetParent();
  23. m_lb = lb->listbox;
  24. m_newb = new wxButton(this, wxID_NEW, wxT("New"));
  25. m_renameb = new wxButton(this, ID_RENAME, wxT("Rename"));
  26. m_deleteb = new wxButton(this, wxID_DELETE, wxT("Delete"));
  27. m_clearb = new wxButton(this, wxID_CLEAR, wxT("Clear"));
  28. Connect(wxID_NEW, wxEVT_COMMAND_BUTTON_CLICKED,
  29. wxCommandEventHandler(MyPanel::OnNew) );
  30. Connect(ID_RENAME, wxEVT_COMMAND_BUTTON_CLICKED,
  31. wxCommandEventHandler(MyPanel::OnRename) );
  32. Connect(wxID_CLEAR, wxEVT_COMMAND_BUTTON_CLICKED,
  33. wxCommandEventHandler(MyPanel::OnClear) );
  34. Connect(wxID_DELETE, wxEVT_COMMAND_BUTTON_CLICKED,
  35. wxCommandEventHandler(MyPanel::OnDelete) );
  36. vbox->Add(-1, 20);
  37. vbox->Add(m_newb);
  38. vbox->Add(m_renameb, 0, wxTOP, 5);
  39. vbox->Add(m_deleteb, 0, wxTOP, 5);
  40. vbox->Add(m_clearb, 0, wxTOP, 5);
  41. SetSizer(vbox);
  42. }
  43. void MyPanel::OnNew(wxCommandEvent& event)
  44. {
  45. wxString str = wxGetTextFromUser(wxT("Add new item"));
  46. if (str.Len() > 0)
  47. m_lb->Append(str);
  48. }
  49. void MyPanel::OnClear(wxCommandEvent& event)
  50. {
  51. m_lb->Clear();
  52. }
  53. void MyPanel::OnRename(wxCommandEvent& event)
  54. {
  55. wxString text;
  56. wxString renamed;
  57. int sel = m_lb->GetSelection();
  58. if (sel != -1) {
  59. text = m_lb->GetString(sel);
  60. renamed = wxGetTextFromUser(wxT("Rename item"),
  61. wxT("Rename dialog"), text);
  62. }
  63. if (!renamed.IsEmpty()) {
  64. m_lb->Delete(sel);
  65. m_lb->Insert(renamed, sel);
  66. }
  67. }
  68. void MyPanel::OnDelete(wxCommandEvent& event)
  69. {
  70. int sel = m_lb->GetSelection();
  71. if (sel != -1) {
  72. m_lb->Delete(sel);
  73. }
  74. }
  75. void Listbox::OnDblClick(wxCommandEvent& event)
  76. {
  77. wxString text;
  78. wxString renamed;
  79. int sel = listbox->GetSelection();
  80. if (sel != -1) {
  81. text = listbox->GetString(sel);
  82. renamed = wxGetTextFromUser(wxT("Rename item"),
  83. wxT("Rename dialog"), text);
  84. }
  85. if (!renamed.IsEmpty()) {
  86. listbox->Delete(sel);
  87. listbox->Insert(renamed, sel);
  88. }
  89. }

main.h

  1. #include <wx/wx.h>
  2. class MyApp : public wxApp
  3. {
  4. public:
  5. virtual bool OnInit();
  6. };

main.cpp

  1. #include "main.h"
  2. #include "Listbox.h"
  3. IMPLEMENT_APP(MyApp)
  4. bool MyApp::OnInit()
  5. {
  6. Listbox *listbox = new Listbox(wxT("Listbox"));
  7. listbox->Show(true);
  8. return true;
  9. }
  1. listbox = new wxListBox(panel, ID_LISTBOX,
  2. wxPoint(-1, -1), wxSize(-1, -1));

这是列表框窗口小部件的构造器。

在我们的示例中,我们有一个列表框和四个按钮。 这些按钮用于添加,重命名,删除和清除列表框中的所有项目。

  1. wxString str = wxGetTextFromUser(wxT("Add new item"));
  2. if (str.Len() > 0)
  3. m_lb->Append(str);

要向列表框中添加新字符串,我们将显示一个wxGetTextFromUser对话框。 我们调用Append()方法将字符串附加到列表框。

  1. m_lb->Clear();

清除所有项目是最简单的操作。 我们只调用Clear()方法。

  1. int sel = m_lb->GetSelection();
  2. if (sel != -1) {
  3. m_lb->Delete(sel);
  4. }

要删除一个项目,我们找出选定的项目。 然后我们调用Delete()方法。

重命名项目需要几个步骤。

  1. wxString text;
  2. wxString renamed;

我们定义了两个局部变量。

  1. int sel = listbox->GetSelection();
  2. if (sel != -1) {
  3. text = listbox->GetString(sel);
  4. renamed = wxGetTextFromUser(wxT("Rename item"),
  5. wxT("Rename dialog"), text);
  6. }

我们获取选定的字符串并将其保存到重命名的变量中。

  1. if (!renamed.IsEmpty()) {
  2. m_lb->Delete(sel);
  3. m_lb->Insert(renamed, sel);
  4. }

我们检查重命名的变量是否为空。 这是为了避免插入空字符串。 然后,我们删除旧项目并插入新项目。

wxWidgets 小部件 II - 图1

图:列表框

wxNotebook

wxNotebook小部件将多个窗口与相应的选项卡连接在一起。 您可以使用以下样式标志来定位wxNotebook小部件:

  • wxNB_LEFT
  • wxNB_RIGHT
  • wxNB_TOP
  • wxNB_BOTTOM

默认位置为wxNB_TOP

Notebook.h

  1. #include <wx/wx.h>
  2. #include <wx/notebook.h>
  3. #include <wx/grid.h>
  4. class Notebook : public wxFrame
  5. {
  6. public:
  7. Notebook(const wxString& title);
  8. void OnQuit(wxCommandEvent& event);
  9. };
  10. class MyGrid : public wxGrid
  11. {
  12. public:
  13. MyGrid(wxNotebook *parent);
  14. };

Notebook.cpp

  1. #include "Notebook.h"
  2. Notebook::Notebook(const wxString& title)
  3. : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(400, 350))
  4. {
  5. wxNotebook *nb = new wxNotebook(this, -1, wxPoint(-1, -1),
  6. wxSize(-1, -1), wxNB_BOTTOM);
  7. wxMenuBar *menubar = new wxMenuBar;
  8. wxMenu *file = new wxMenu;
  9. file->Append(wxID_EXIT, wxT("Quit"), wxT(""));
  10. menubar->Append(file, wxT("&File"));
  11. SetMenuBar(menubar);
  12. Connect(wxEVT_COMMAND_MENU_SELECTED,
  13. wxCommandEventHandler(Notebook::OnQuit));
  14. MyGrid *grid1 = new MyGrid(nb);
  15. MyGrid *grid2 = new MyGrid(nb);
  16. MyGrid *grid3 = new MyGrid(nb);
  17. nb->AddPage(grid1, wxT("Sheet1"));
  18. nb->AddPage(grid2, wxT("Sheet2"));
  19. nb->AddPage(grid3, wxT("Sheet3"));
  20. CreateStatusBar();
  21. Center();
  22. }
  23. void Notebook::OnQuit(wxCommandEvent& event)
  24. {
  25. Close(true);
  26. }
  27. MyGrid::MyGrid(wxNotebook * parent)
  28. : wxGrid(parent, wxID_ANY)
  29. {
  30. CreateGrid(30, 30);
  31. SetRowLabelSize(50);
  32. SetColLabelSize(25);
  33. SetRowLabelAlignment(wxALIGN_RIGHT, wxALIGN_CENTRE);
  34. SetLabelFont(wxFont(9, wxFONTFAMILY_DEFAULT,
  35. wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD));
  36. for (int i = 0; i < 30 ; i++) {
  37. this->SetRowSize(i, 25);
  38. }
  39. }

main.h

  1. #include <wx/wx.h>
  2. class MyApp : public wxApp
  3. {
  4. public:
  5. virtual bool OnInit();
  6. };

main.cpp

  1. #include "main.h"
  2. #include "Notebook.h"
  3. IMPLEMENT_APP(MyApp)
  4. bool MyApp::OnInit()
  5. {
  6. Notebook *notebook = new Notebook(wxT("Notebook"));
  7. notebook->Show(true);
  8. return true;
  9. }

在此示例中,我们创建了带有三个网格的笔记本小部件。 笔记本小部件位于底部。

  1. wxNotebook *nb = new wxNotebook(this, -1, wxPoint(-1, -1),
  2. wxSize(-1, -1), wxNB_BOTTOM);

在这里,我们创建笔记本小部件。

  1. nb->AddPage(grid1, wxT("Sheet1"));
  2. nb->AddPage(grid2, wxT("Sheet2"));
  3. nb->AddPage(grid3, wxT("Sheet3"));

我们将三个网格对象添加到笔记本小部件中。

wxWidgets 小部件 II - 图2

图:Notebook小部件

wxScrolledWindow

这是容器小部件之一。 当我们的区域大于窗口可以显示的区域时,此功能将非常有用。 在我们的示例中,我们演示了这种情况。 我们将大图像放入窗口。 当窗口小于我们的图像时,将自动显示滚动条。

scrolledwindow.h

  1. #include <wx/wx.h>
  2. class ScrWindow : public wxFrame
  3. {
  4. public:
  5. ScrWindow(const wxString& title);
  6. };

scrolledwindow.cpp

  1. #include "scrolledwindow.h"
  2. ScrWindow::ScrWindow(const wxString& title)
  3. : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200))
  4. {
  5. wxImage::AddHandler(new wxJPEGHandler);
  6. wxScrolledWindow *sw = new wxScrolledWindow(this);
  7. wxBitmap bmp(wxT("castle.jpg"), wxBITMAP_TYPE_JPEG);
  8. wxStaticBitmap *sb = new wxStaticBitmap(sw, -1, bmp);
  9. int width = bmp.GetWidth();
  10. int height = bmp.GetHeight();
  11. sw->SetScrollbars(10, 10, width/10, height/10);
  12. sw->Scroll(50,10);
  13. Center();
  14. }

main.h

  1. #include <wx/wx.h>
  2. class MyApp : public wxApp
  3. {
  4. public:
  5. virtual bool OnInit();
  6. };

main.cpp

  1. #include "main.h"
  2. #include "scrolledwindow.h"
  3. IMPLEMENT_APP(MyApp)
  4. bool MyApp::OnInit()
  5. {
  6. ScrWindow *sw = new ScrWindow(wxT("ScrolledWindow"));
  7. sw->Show(true);
  8. return true;
  9. }

在我们的示例中,我们显示了 Spis 城堡的图片。

  1. wxImage::AddHandler(new wxJPEGHandler);

要处理 JPG 图像,我们必须启动wxJPEGHandler

  1. wxScrolledWindow *sw = new wxScrolledWindow(this);
  2. wxBitmap bmp(wxT("castle.jpg"), wxBITMAP_TYPE_JPEG);
  3. wxStaticBitmap *sb = new wxStaticBitmap(sw, -1, bmp);

我们创建一个滚动窗口,并在其中放置一个静态位图。

  1. sw->SetScrollbars(10, 10, width/10, height/10);

我们设置滚动条。

  1. sw->Scroll(50,10);

我们稍微滚动窗口。

在本章中,我们继续介绍 wxWidgets 库中的小部件。