<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # wxWidgets 中的菜單和工具欄 > 原文: [http://zetcode.com/gui/wxwidgets/menustoolbars/](http://zetcode.com/gui/wxwidgets/menustoolbars/) 菜單欄是 GUI 應用中最可見的部分之一。 它是位于各個菜單中的一組命令。 在控制臺應用中,您必須記住所有這些神秘命令,在這里,我們將大多數命令分組為邏輯部分。 有公認的標準可以進一步減少學習新應用的時間。 要在 wxWidgets 中實現菜單欄,我們需要三個類:`wxMenuBar`,`wxMenu`和`wxMenuItem`。 ## 簡單菜單示例 在 wxWidgets 中創建菜單欄非常簡單。 `menu.h` ```cpp #include <wx/wx.h> #include <wx/menu.h> class SimpleMenu : public wxFrame { public: SimpleMenu(const wxString& title); void OnQuit(wxCommandEvent& event); wxMenuBar *menubar; wxMenu *file; }; ``` `menu.cpp` ```cpp #include "menu.h" SimpleMenu::SimpleMenu(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180)) { menubar = new wxMenuBar; file = new wxMenu; file->Append(wxID_EXIT, wxT("&Quit")); menubar->Append(file, wxT("&File")); SetMenuBar(menubar); Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SimpleMenu::OnQuit)); Centre(); } void SimpleMenu::OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "menu.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { SimpleMenu *menu = new SimpleMenu(wxT("Simple Menu")); menu->Show(true); return true; } ``` ```cpp menubar = new wxMenuBar; ``` 首先,我們創建一個菜單欄對象。 ```cpp file = new wxMenu; ``` 接下來,我們創建一個菜單對象。 ```cpp file->Append(wxID_EXIT, wxT("&Quit")); ``` 我們將菜單項添加到菜單對象中。 第一個參數是菜單項的 ID。 第二個參數是菜單項的名稱。 在這里,我們沒有明確創建一個`wxMenuItem`。 它是通過`Append()`方法在后臺創建的。 稍后,我們將手動創建`wxMenuItem`。 ```cpp menubar->Append(file, wxT("&File")); SetMenuBar(menubar); ``` 之后,我們將菜單添加到菜單欄中。 &字符創建一個加速鍵。 帶下劃線的&后面的字符。 這樣,可以通過 `Alt + F` 快捷方式訪問菜單。 最后,我們調用`SetMenuBar()`方法。 該方法屬于`wxFrame`小部件。 它設置菜單欄。 ![Simle menu example](https://img.kancloud.cn/1a/81/1a8118695d2fa6eba061aa0df22cd15e_290x209.jpg) 圖:簡單菜單 example ## 子菜單 每個菜單也可以有一個子菜單。 這樣,我們可以將類似的命令分組。 例如,我們可以將隱藏或顯示各種工具欄(例如個人欄,地址欄,狀態欄或導航欄)的命令放置在稱為工具欄的子菜單中。 在菜單中,我們可以使用分隔符來分隔命令。 這是一條簡單的線。 通常的做法是使用單個分隔符將命令(例如新建,打開,保存)與命令(例如打印,打印預覽)分開。 在我們的示例中,我們將看到如何創建子菜單和菜單分隔符。 `menu.h` ```cpp #include <wx/wx.h> #include <wx/menu.h> class SubMenu : public wxFrame { public: SubMenu(const wxString& title); void OnQuit(wxCommandEvent & event); wxMenuBar *menubar; wxMenu *file; wxMenu *imp; wxMenuItem *quit; }; ``` `menu.cpp` ```cpp #include "menu.h" SubMenu::SubMenu(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180)) { menubar = new wxMenuBar; file = new wxMenu; file->Append(wxID_ANY, wxT("&New")); file->Append(wxID_ANY, wxT("&Open")); file->Append(wxID_ANY, wxT("&Save")); file->AppendSeparator(); imp = new wxMenu; imp->Append(wxID_ANY, wxT("Import newsfeed list...")); imp->Append(wxID_ANY, wxT("Import bookmarks...")); imp->Append(wxID_ANY, wxT("Import mail...")); file->AppendSubMenu(imp, wxT("I&mport")); quit = new wxMenuItem(file, wxID_EXIT, wxT("&Quit\tCtrl+W")); file->Append(quit); menubar->Append(file, wxT("&File")); SetMenuBar(menubar); Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SubMenu::OnQuit)); Centre(); } void SubMenu::OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "menu.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { SubMenu *smenu = new SubMenu(wxT("Submenu")); smenu->Show(true); return true; } ``` 我們在文件菜單中創建了一個子菜單。 這是一個導入子菜單,可以在 Opera Web 瀏覽器中看到。 ```cpp file->AppendSeparator(); ``` 創建菜單分隔線,并調用`AppendSeparator()`方法。 ```cpp imp = new wxMenu; imp->Append(wxID_ANY, wxT("Import newsfeed list...")); imp->Append(wxID_ANY, wxT("Import bookmarks...")); imp->Append(wxID_ANY, wxT("Import mail...")); file->AppendSubMenu(imp, wxT("I&mport")); ``` 子菜單的創建類似于普通菜單。 它附有`AppendSubMenu()`方法。 ![Submenu](https://img.kancloud.cn/73/4f/734f7edb53c9c6ba43db9a80430e1f01_426x274.jpg) 圖:子菜單 ## 工具欄 菜單將我們可以在應用中使用的所有命令分組。 使用工具欄可以快速訪問最常用的命令。 ```cpp virtual wxToolBar* wxFrame::CreateToolBar(long style = wxTB_DEFAULT_STYLE, wxWindowID id = wxID_ANY, const wxString & name = wxToolBarNameStr) ``` 要創建工具欄,我們調用框架窗口小部件的`CreateToolBar()`方法。 ## 一個簡單的工具欄 我們的第一個示例將創建一個簡單的工具欄。 `toolbar.h` ```cpp #include <wx/wx.h> class Toolbar : public wxFrame { public: Toolbar(const wxString& title); void OnQuit(wxCommandEvent& event); }; ``` `toolbar.cpp` ```cpp #include "toolbar.h" Toolbar::Toolbar(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 250)) { wxImage::AddHandler(new wxPNGHandler); wxBitmap exit(wxT("exit.png"), wxBITMAP_TYPE_PNG); wxToolBar *toolbar = CreateToolBar(); toolbar->AddTool(wxID_EXIT, wxT("Exit application"), exit); toolbar->Realize(); Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(Toolbar::OnQuit)); } void Toolbar::OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "toolbar.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Toolbar *toolbar = new Toolbar(wxT("Toolbar")); toolbar->Show(true); return true; } ``` 在我們的示例中,我們創建了一個工具欄和一個工具按鈕。 單擊工具欄按鈕將終止應用。 ```cpp wxToolBar *toolbar = CreateToolBar(); ``` 我們創建一個工具欄。 ```cpp toolbar->AddTool(wxID_EXIT, wxT("Exit application"), exit); ``` 我們將工具添加到工具欄。 ```cpp toolbar->Realize(); ``` 添加工具后,我們將調用`Realize()`方法。 ![Toolbar](https://img.kancloud.cn/f6/26/f62695b7a6a1bd1cca3f3a8edccb247f_290x209.jpg) 圖:工具欄 #### 工具欄 如果我們要擁有多個工具欄,則必須以其他方式創建它們,例如除了調用`CreateToolbar()`方法。 `toolbars.h` ```cpp #include <wx/wx.h> class Toolbar : public wxFrame { public: Toolbar(const wxString& title); void OnQuit(wxCommandEvent& event); wxToolBar *toolbar1; wxToolBar *toolbar2; }; ``` `toolbars.cpp` ```cpp #include "toolbars.h" Toolbar::Toolbar(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 250)) { wxImage::AddHandler(new wxPNGHandler); wxBitmap exit(wxT("exit.png"), wxBITMAP_TYPE_PNG); wxBitmap newb(wxT("new.png"), wxBITMAP_TYPE_PNG); wxBitmap open(wxT("open.png"), wxBITMAP_TYPE_PNG); wxBitmap save(wxT("save.png"), wxBITMAP_TYPE_PNG); wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL); toolbar1 = new wxToolBar(this, wxID_ANY); toolbar1->AddTool(wxID_ANY, wxT("New"), newb); toolbar1->AddTool(wxID_ANY, wxT("Open"), open); toolbar1->AddTool(wxID_ANY, wxT(""), save); toolbar1->Realize(); toolbar2 = new wxToolBar(this, wxID_ANY); toolbar2->AddTool(wxID_EXIT, wxT("Exit application"), exit); toolbar2->Realize(); vbox->Add(toolbar1, 0, wxEXPAND); vbox->Add(toolbar2, 0, wxEXPAND); SetSizer(vbox); Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(Toolbar::OnQuit)); } void Toolbar::OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "toolbars.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Toolbar *toolbar = new Toolbar(wxT("Toolbar")); toolbar->Show(true); return true; } ``` 在我們的示例中,我們創建了兩個水平工具欄。 我們將它們放置在垂直包裝機上。 ```cpp toolbar1 = new wxToolBar(this, wxID_ANY); ... toolbar2 = new wxToolBar(this, wxID_ANY); ``` 在這里,我們創建兩個工具欄。 ```cpp vbox->Add(toolbar1, 0, wxEXPAND); vbox->Add(toolbar2, 0, wxEXPAND); ``` 在這里,我們將它們添加到垂直框大小調整器中。 ![Toolbars](https://img.kancloud.cn/99/13/99132329f4f84dd75b6b51030bc2055b_290x209.jpg) 圖:工具欄 s 在 wxWidgets 教程的這一部分中,我們介紹了菜單和工具欄。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看