天天看點

wxWidgets communicate

/*
*程式的版權和版本聲明部分:
*Copyright(c)2014,煙台大學計算機學院學生
*All rights reserved.
*檔案名稱:
*作者:田成琳
*完成日期:2014 年 5 月 30 日
*版本号:v1.0
*對任務及求解方法的描述部分:
*輸入描述: -
*問題描述:初體驗wxWidgets
*程式輸出:一個簡單的小視窗,含有兩個按鈕,能計數
*問題分析:
*算法設計:
*/
#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;
};
void LeftPanel::Onplus(wxCommandEvent &WXUNUSED(event))
{
    count++;
    Communicate *com =(Communicate *)m_parent->GetParent();
    com->m_rp->m_text->SetLabel(wxString::Format(wxT("%d"),count));
}
void LeftPanel::OnMinus(wxCommandEvent &WXUNUSED(event))
{
    count--;
    Communicate *com = (Communicate *) m_parent->GetParent();
    com->m_rp->m_text->SetLabel(wxString::Format(wxT("%d"),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;
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));
}
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));
}
class Communicate : public wxFrame//界面函數
{
public:
    Communicate(const wxString &title);
    LeftPanel *m_lp;
    RightPanel *m_rp;
    wxPanel *m_parent;
};
Communicate::Communicate(const wxString &title)
    :wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(290,150))
{
    m_parent = new wxPanel(this,wxID_ANY);
    wxBoxSizer *box = new wxBoxSizer(wxHORIZONTAL);
    m_lp = new LeftPanel(m_parent);
    m_rp = new RightPanel(m_parent);
    box->Add(m_lp, 1, wxEXPAND | wxALL, 5);
    box->Add(m_rp, 1, wxEXPAND | wxALL, 5);
    m_parent->SetSizer(box);
    this->Centre();
};
class MyApp:public wxApp
{
public:
    virtual bool OnInit();
};
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
    Communicate *communicate = new Communicate(wxT("Widgets communicate"));
    communicate->Show(true);
    return true;
}
           

運作結果:

wxWidgets communicate

心得體會:很多不懂的地方,低級的模仿,還有一個問題,為啥多個檔案編譯成功,一個檔案就是編譯不通過呢。