<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # wxWidgets 中的第一個程序 > 原文: [http://zetcode.com/gui/wxwidgets/firstprograms/](http://zetcode.com/gui/wxwidgets/firstprograms/) 在本章中,我們將介紹創建 wxWidgets 應用所需的基礎知識。 我們將創建第一個簡單示例,展示如何顯示圖標。 接下來,我們將創建一個簡單的示例來演示事件的用法。 最后,我們將看到小部件如何在 wxWidgets 應用中進行通信。 ## 一個簡單的應用 首先,我們創建非常基本的 wxWidgets 程序。 `simple.h` ```cpp #include <wx/wx.h> class Simple : public wxFrame { public: Simple(const wxString& title); }; ``` `simple.cpp` ```cpp #include "simple.h" Simple::Simple(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150)) { Centre(); } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "simple.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Simple *simple = new Simple(wxT("Simple")); simple->Show(true); return true; } ``` 這個非常基本的示例在屏幕上顯示了一個小窗口。 窗口居中。 ```cpp Centre(); ``` 此方法使窗口在屏幕上水平和垂直居中。 ```cpp IMPLEMENT_APP(MyApp) ``` 實現該應用的代碼隱藏在此宏的后面。 這是復制和粘貼代碼,我們通常不必關心。 ```cpp g++ main.cpp main.h simple.cpp simple.h `wx-config --cxxflags --libs` -o simple ``` 要在 Unix 上編譯示例,請運行以上命令。 ![Simple](https://img.kancloud.cn/e2/eb/e2eb68e07b28df627547547e997761a0_252x182.jpg) 圖:簡單 ## 應用圖標 在此示例中,我們為應用提供一個圖標。 在窗口的左上角顯示小圖標已成為一種標準。 圖標是程序的圖形標識。 `icon.h` ```cpp #include <wx/wx.h> class Icon : public wxFrame { public: Icon(const wxString& title); }; ``` `icon.cpp` ```cpp #include "icon.h" Icon::Icon(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150)) { SetIcon(wxIcon(wxT("web.xpm"))); Centre(); } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "icon.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Icon *icon = new Icon(wxT("Icon")); icon->Show(true); return true; } ``` 在我們的示例中,我們顯示了一個小的 Web 圖標。 ```cpp SetIcon(wxIcon(wxT("web.xpm"))); ``` 顯示應用圖標僅需一行代碼。 XPM(X PixMap)是 ASCII 圖像格式。 ![Icon](https://img.kancloud.cn/db/a0/dba0438bdea1316c7d83cf1fa7e752e1_252x182.jpg) 圖:圖標 ## 一個簡單的按鈕 在下面的示例中,我們在框架小部件上創建一個按鈕。 我們將展示如何創建一個簡單的事件處理器。 `button.h` ```cpp #include <wx/wx.h> class Button : public wxFrame { public: Button(const wxString& title); void OnQuit(wxCommandEvent & event); }; ``` `button.cpp` ```cpp #include "button.h" Button::Button(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(270, 150)) { wxPanel *panel = new wxPanel(this, wxID_ANY); wxButton *button = new wxButton(panel, wxID_EXIT, wxT("Quit"), wxPoint(20, 20)); Connect(wxID_EXIT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Button::OnQuit)); button->SetFocus(); Centre(); } void Button::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 "button.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Button *btnapp = new Button(wxT("Button")); btnapp->Show(true); return true; } ``` ```cpp wxPanel *panel = new wxPanel(this, wxID_ANY); ``` 首先,我們創建一個`wxPanel`小部件。 它將放置在`wxFrame`小部件內。 ```cpp wxButton *button = new wxButton(panel, wxID_EXIT, wxT("Quit"), wxPoint(20, 20)); ``` 我們創建一個`wxButton`小部件。 它放在面板上。 我們為按鈕使用預定義的`wxID_EXIT` ID。 這將導致在按鈕上顯示一個小的退出圖標。 按鈕的標簽為`"Quit"`。 手動將按鈕定位在`x = 20`,`y = 20`坐標處。 坐標系的起點在左上角。 ```cpp Connect(wxID_EXIT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Button::OnQuit)); ``` 如果單擊按鈕,將生成`wxEVT_COMMAND_BUTTON_CLICKED`事件。 我們將事件連接到`Button`類的`OnQuit()`方法。 因此,當我們單擊按鈕時,將調用`OnQuit()`方法。 ```cpp button->SetFocus(); ``` 我們將鍵盤焦點設置為按鈕。 因此,如果我們按`Enter`鍵,則單擊該按鈕。 ```cpp Close(true); ``` 在`OnQuit()`方法內部,我們稱為`Close()`方法。 這將終止我們的應用。 ![Button](https://img.kancloud.cn/53/f6/53f6c2ce275ba78195dd963449eaa90d_272x182.jpg) 圖:按鈕 ## 小部件通信 了解小部件如何在應用中進行通信非常重要。 請遵循下一個示例。 `Panels.h` ```cpp #include <wx/wx.h> #include <wx/panel.h> class LeftPanel : public wxPanel { public: LeftPanel(wxPanel *parent); void OnPlus(wxCommandEvent & event); void OnMinus(wxCommandEvent & event); wxButton *m_plus; wxButton *m_minus; wxPanel *m_parent; int count; }; class RightPanel : public wxPanel { public: RightPanel(wxPanel *parent); void OnSetText(wxCommandEvent & event); wxStaticText *m_text; }; const int ID_PLUS = 101; const int ID_MINUS = 102; ``` `Panels.cpp` ```cpp #include <wx/stattext.h> #include "Communicate.h" LeftPanel::LeftPanel(wxPanel * parent) : wxPanel(parent, -1, wxPoint(-1, -1), wxSize(-1, -1), wxBORDER_SUNKEN) { count = 0; m_parent = parent; m_plus = new wxButton(this, ID_PLUS, wxT("+"), wxPoint(10, 10)); m_minus = new wxButton(this, ID_MINUS, wxT("-"), wxPoint(10, 60)); Connect(ID_PLUS, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(LeftPanel::OnPlus)); Connect(ID_MINUS, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(LeftPanel::OnMinus)); } void LeftPanel::OnPlus(wxCommandEvent & WXUNUSED(event)) { count++; Communicate *comm = (Communicate *) m_parent->GetParent(); comm->m_rp->m_text->SetLabel(wxString::Format(wxT("%d"), count)); } void LeftPanel::OnMinus(wxCommandEvent & WXUNUSED(event)) { count--; Communicate *comm = (Communicate *) m_parent->GetParent(); comm->m_rp->m_text->SetLabel(wxString::Format(wxT("%d"), count)); } RightPanel::RightPanel(wxPanel * parent) : wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(270, 150), wxBORDER_SUNKEN) { m_text = new wxStaticText(this, -1, wxT("0"), wxPoint(40, 60)); } ``` `Communicate.h` ```cpp #include "Panels.h" #include <wx/wxprec.h> class Communicate : public wxFrame { public: Communicate(const wxString& title); LeftPanel *m_lp; RightPanel *m_rp; wxPanel *m_parent; }; ``` `Communicate.cpp` ```cpp #include "Communicate.h" Communicate::Communicate(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(290, 150)) { m_parent = new wxPanel(this, wxID_ANY); wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL); m_lp = new LeftPanel(m_parent); m_rp = new RightPanel(m_parent); hbox->Add(m_lp, 1, wxEXPAND | wxALL, 5); hbox->Add(m_rp, 1, wxEXPAND | wxALL, 5); m_parent->SetSizer(hbox); this->Centre(); } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "Communicate.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Communicate *communicate = new Communicate(wxT("Widgets communicate")); communicate->Show(true); return true; } ``` 在我們的示例中,我們有兩個面板。 左右面板。 左側面板有兩個按鈕。 右側面板有一個靜態文本。 這些按鈕更改靜態文本中顯示的數字。 問題是,我們如何抓住指向靜態文本的指針? ```cpp m_parent = parent; ``` 在這里,我們將指針保存到`LeftPanel`的父窗口小部件。 這是一個`wxPanel`小部件。 ```cpp Communicate *comm = (Communicate *) m_parent->GetParent(); comm->m_rp->m_text->SetLabel(wxString::Format(wxT("%d"), count)); ``` 這兩行是示例中最重要的行。 顯示了如何訪問放置在不同面板上的靜態文本小部件。 首先,我們獲得左右兩個面板的父面板。 該父窗口小部件具有指向右側面板的指針。 右面板上有一個指向靜態文本的指針。 ![Widgets communicate](https://img.kancloud.cn/ce/55/ce55d56555420730bbc82c30f53338cf_292x182.jpg) 圖:小部件通信 在 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>

                              哎呀哎呀视频在线观看