阿牧路泽

哪有那么多坚强,无非是死扛罢了
  博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

9、wxWidgets 组件wxListBox wxNotebook wxScrolledWindow

Posted on 2018-09-05 19:28  阿牧路泽  阅读(1755)  评论(0编辑  收藏  举报

待更新。。。。

wxListBox

一个wxListBox组件被用来显示一些元素,它是一个有一系列字符串的矩形。我们可以使用它来显示一个MP3文件的列表,一些书名,或者是一个大工程的模块名。一个wxListBox可以以两种不同的状态被创建。一个单一选择的状态或者一个多重选择的状态。单一状态是默认的状态,在wxListBox里面有两个事件类型,第一个是wxEVT_COMMAND_LISTBOX_SELECTED,当我们选择wxListBox里面的一个元素时这个事件产生;第二个是wxEVT_COMMAND_LISTBOX_DOUBLE_CLICKED,当我们双击wxListBox里面的一个元素时这个事件产生。在wxListBox内部的元素在GTK的平台上有限制,大概不能超过2000个左右的元素。元素的ID从0开始计算,滚动条会在需要的时候自动生成。

main.h

 1 #include <wx/wx.h>
 2 #include <wx/listbox.h>
 3 
 4 class MyPanel : public wxPanel
 5 {
 6 public:
 7     MyPanel(wxPanel *parent);
 8 
 9     //定义了四个事件处理函数
10     void OnNew(wxCommandEvent& event);
11     void OnRename(wxCommandEvent& event);
12     void OnClear(wxCommandEvent& event);
13     void OnDelete(wxCommandEvent& event);
14     //定义一个listbox组件
15     wxListBox *m_lb;
16     //定义四个按钮组件
17     wxButton *m_newb;
18     wxButton *m_renameb;
19     wxButton *m_clearb;
20     wxButton *m_deleteb;
21 
22 };
23 
24 class Listbox : public wxFrame
25 {
26 public:
27     Listbox(const wxString& title);
28 
29     void OnDblClick(wxCommandEvent& event);
30 
31     wxListBox *listbox;
32     MyPanel *btnPanel;
33 
34 };
35 
36 const int ID_RENAME = 1;
37 const int ID_LISTBOX = 5;
38 
39 class MyApp : public wxApp
40 {
41   public:
42     virtual bool OnInit();
43 };

main.cpp

  1 #include "main.h"
  2 #include "listbox.h"
  3 
  4 Listbox::Listbox(const wxString& title)
  5        : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(270, 200))
  6 {
  7 
  8     wxPanel * panel = new wxPanel(this, -1);
  9 
 10     wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
 11     //listbox组件的构造器
 12     listbox = new wxListBox(panel, ID_LISTBOX, wxPoint(-1, -1), wxSize(-1, -1));
 13 
 14     hbox->Add(listbox, 3, wxEXPAND | wxALL, 20);
 15 
 16     btnPanel = new MyPanel(panel);
 17     hbox->Add(btnPanel, 2, wxEXPAND | wxRIGHT, 10);
 18 
 19     Connect(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED,
 20       wxCommandEventHandler(Listbox::OnDblClick));
 21 
 22     panel->SetSizer(hbox);
 23     Center();
 24 }
 25 
 26 
 27 MyPanel::MyPanel(wxPanel * parent)
 28        : wxPanel(parent, wxID_ANY)
 29 {
 30   wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
 31 
 32   Listbox *lb = (Listbox *) parent->GetParent();
 33   m_lb = lb->listbox;
 34 
 35   m_newb = new wxButton(this, wxID_NEW, wxT("New"));
 36   m_renameb = new wxButton(this, ID_RENAME, wxT("Rename"));
 37   m_deleteb = new wxButton(this, wxID_DELETE, wxT("Delete"));
 38   m_clearb = new wxButton(this, wxID_CLEAR, wxT("Clear"));
 39 
 40   Connect(wxID_NEW, wxEVT_COMMAND_BUTTON_CLICKED,
 41       wxCommandEventHandler(MyPanel::OnNew) );
 42   Connect(ID_RENAME, wxEVT_COMMAND_BUTTON_CLICKED,
 43       wxCommandEventHandler(MyPanel::OnRename) );
 44   Connect(wxID_CLEAR, wxEVT_COMMAND_BUTTON_CLICKED,
 45       wxCommandEventHandler(MyPanel::OnClear) );
 46   Connect(wxID_DELETE, wxEVT_COMMAND_BUTTON_CLICKED,
 47       wxCommandEventHandler(MyPanel::OnDelete) );
 48 
 49   vbox->Add(-1, 20);
 50   vbox->Add(m_newb);
 51   vbox->Add(m_renameb, 0, wxTOP, 5);
 52   vbox->Add(m_deleteb, 0, wxTOP, 5);
 53   vbox->Add(m_clearb, 0, wxTOP, 5);
 54 
 55   SetSizer(vbox);
 56 }
 57 
 58 void MyPanel::OnNew(wxCommandEvent& event)
 59 {
 60   wxString str = wxGetTextFromUser(wxT("Add new item"));
 61   if (str.Len() > 0)
 62       m_lb->Append(str);
 63 }
 64 
 65 void MyPanel::OnClear(wxCommandEvent& event)
 66 {
 67   m_lb->Clear();
 68 }
 69 
 70 void MyPanel::OnRename(wxCommandEvent& event)
 71 {
 72   wxString text;
 73   wxString renamed;
 74 
 75   int sel = m_lb->GetSelection();
 76   if (sel != -1) {
 77       text = m_lb->GetString(sel);
 78       renamed = wxGetTextFromUser(wxT("Rename item"),
 79                   wxT("Rename dialog"), text);
 80   }
 81 
 82   if (!renamed.IsEmpty()) {
 83       m_lb->Delete(sel);
 84       m_lb->Insert(renamed, sel);
 85   }
 86 }
 87 
 88 
 89 void MyPanel::OnDelete(wxCommandEvent& event)
 90 {
 91   int sel = m_lb->GetSelection();
 92   if (sel != -1) {
 93       m_lb->Delete(sel);
 94   }
 95 }
 96 
 97 void Listbox::OnDblClick(wxCommandEvent& event)
 98 {
 99   wxString text;
100   wxString renamed;
101 
102   int sel = listbox->GetSelection();
103   if (sel != -1) {
104       text = listbox->GetString(sel);
105       renamed = wxGetTextFromUser(wxT("Rename item"),
106                   wxT("Rename dialog"), text);
107   }
108 
109   if (!renamed.IsEmpty()) {
110       listbox->Delete(sel);
111       listbox->Insert(renamed, sel);
112   }
113 }
114 IMPLEMENT_APP(MyApp)
115 
116 bool MyApp::OnInit()
117 {
118 
119     Listbox *listbox = new Listbox(wxT("Listbox"));
120     listbox->Show(true);
121 
122     return true;
123 }
listbox = new wxListBox(panel, ID_LISTBOX, wxDEFAULT_POSITION, wxDEFAULT_SIZE);

  这个是listbox组件的构造器.

  在我们的例子中,我们有一列四个按钮,这些按钮用来在listbox内添加、修改、删除、清空元素。

1 wxString str = wxGetTextFromUser(_T("Add new item"));
2 if(str.Len() > 0)
3 {
4     m_lb->Append(str);
5 }

  我们使用上面的代码显示一个wxGetTextFromUser对话框接收用户输入,然后调用Append()函数添加到listbox内。

m_lb->Clear();

  清空listbox只需要简单的调用一个Clear()方法。

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

  我们调用GetSelection()方法计算出当前选中的是第几项,然后我们调用Delete()方法删除元素。

修改一个元素需要以下几个步骤:

1 wxString text;
2 wxString renamed;
3 //我们定义了两个字符串对象。
4 int sel = listbox->GetSelection();
5 if(sel != -1)
6 {
7     text = listbox->GetString(sel);
8     Renamed = wxGetTextFromUser(_T("Rename item"), _T("Rename dialog"), text);
9 }

我们获取了当前选中的的字符串,并保存起来,然后请求用户的输入。

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

我们检查了renamed字符串是否为空,避免了插入空字符串,然后我们删除了旧的元素,插入新的元素。

效果展示:

wxNotebook

wxNotebook可以在一个窗体上通过标签列举多个窗口。可以通过下面四个标识符指定标签的位置:

  1. wxNB_LEFT
  2. wxNB_RIGHT
  3. wxNB_TOP
  4. wxNB_BOTTOM

默认的是wxNB_TOP

main.h

 1 #include <wx/wx.h>
 2 #include <wx/notebook.h>
 3 #include <wx/grid.h>
 4 
 5 class Notebook : public wxFrame
 6 {
 7 public:
 8     Notebook(const wxString& title);
 9 
10     void OnQuit(wxCommandEvent& event);
11 
12 };
13 
14 class MyGrid : public wxGrid
15 {
16 public:
17     MyGrid(wxNotebook *parent);
18 
19 };
20 
21 class MyApp : public wxApp
22 {
23   public:
24     virtual bool OnInit();
25 };

main.cpp

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

在这个例子中,我们创建了一个包含三个表格控件的notebook组件,notebook的标签被放置在窗体底部。

1 wxNotebook * nb = new wxNotebook(this, wxID_ANY, wxDEFAULTPOSITION, wxDEFAULTSIZE, wxNB_BOTTOM);

这里我们创建了一个notebook组件。

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

我们添加了三个表格控件到notebook组件中。

效果展示:

 

wxScrolledWindow

这是容器组件中的其中一个。当我们有一个比屏幕还要大的区域需要显示时,这个组件很有用。在我们的例子中,我们示范了它的用法,我们在窗口中放置了一个巨大的图片。当窗口比我们的图片小的时候,滚动条会自动显示。

 main.h

 1 #include <wx/wx.h>
 2 
 3 class ScrWindow : public wxFrame
 4 {
 5 public:
 6   ScrWindow(const wxString& title);
 7 
 8 };
 9 
10 class MyApp : public wxApp
11 {
12   public:
13     virtual bool OnInit();
14 };

main.cpp

 1 #include "main.h"
 2 
 3 ScrWindow::ScrWindow(const wxString& title)
 4        : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200))
 5 {
 6   wxImage::AddHandler(new wxJPEGHandler);
 7   wxScrolledWindow *sw = new wxScrolledWindow(this);
 8 
 9   wxBitmap bmp(wxT("castle.jpg"), wxBITMAP_TYPE_JPEG);
10   wxStaticBitmap *sb = new wxStaticBitmap(sw, -1, bmp);
11 
12   int width = bmp.GetWidth();
13   int height = bmp.GetHeight();
14 
15   sw->SetScrollbars(10, 10, width/10, height/10);
16   sw->Scroll(50,10);
17 
18   Center();
19 }
20 
21 IMPLEMENT_APP(MyApp)
22 
23 bool MyApp::OnInit()
24 {
25 
26     ScrWindow *sw = new ScrWindow(wxT("ScrolledWindow"));
27     sw->Show(true);
28 
29     return true;
30 }

在我们的例子中,我们显示了一张图片。

wxImage::AddHandler(new wxJPEGHandler);

我们必须初始化wxJPEGHandler,才能处理jpg图像。

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

我们创建了一个滚动窗口,并放置了一张静态图。

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

我们设置了滚动条。

sw->Scroll(50, 10);

滚动到初始位置。

 效果展示: