zoukankan      html  css  js  c++  java
  • 3、wxWidgets按钮及组件间通信

    一、按钮

    在接下来的这个例子中,我们在框架上创建了一个按钮。我们将会看到,如何建立一个简单的事件处理程序。在这个实例中,为按钮绑定处理事件,使用的是静态时间表的方法

    1 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    2    EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
    3    EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
    4    EVT_BUTTON(wxID_EXIT, MyFrame::OnQuit)
    5 END_EVENT_TABLE()

    main.h

     1 #include <wx/wx.h>
     2 //定义主窗口类
     3 class MyFrame : public wxFrame
     4 {
     5 public:
     6     MyFrame(const wxString& title);
     7 
     8     //定义事件处理函数
     9     void OnQuit(wxCommandEvent& event);
    10     void OnAbout(wxCommandEvent& event);
    11 private:
    12     //声明事件表
    13     DECLARE_EVENT_TABLE()
    14 
    15 };
    16 //定义应用程序类
    17 class MyApp : public wxApp
    18 {
    19   public:
    20     virtual bool OnInit();
    21 };

    main.cpp

     1 #include "main.h"
     2 #include "icon.xpm"
     3 
     4 MyFrame::MyFrame(const wxString& title)
     5        : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150))
     6 {
     7     //定义菜单
     8     wxMenu *menuFile = new wxMenu;
     9     menuFile->Append(wxID_EXIT, wxT("Exit ... 	Alt+X"), wxT("Quit this program"));
    10 
    11     wxMenu *menuHelp = new wxMenu;
    12     menuHelp->Append(wxID_ABOUT, wxT("&About ... 	F1"), wxT("Show about frame"));
    13 
    14     //定义菜单栏
    15     wxMenuBar *menuBar = new wxMenuBar;
    16 
    17     //向菜单栏添加菜单
    18     menuBar->Append(menuFile, wxT("&File"));
    19     menuBar->Append(menuHelp, wxT("&Help"));
    20 
    21     //将菜单栏添加到wxFrame中
    22     SetMenuBar(menuBar);
    23 
    24     //添加状态栏
    25     CreateStatusBar();
    26     //将状态栏分为两栏
    27     //CreateStatusBar(2);
    28     //添加状态栏显示内容
    29     SetStatusText(wxT("Welcome to wxWidgets!"));
    30 
    31     //设置应用显示图标
    32     SetIcon(wxIcon(icon_xpm));
    33 
    34     //在wxFrame组件中定义了一个Panel容器,用于放置Button按钮
    35     wxPanel * panel = new wxPanel(this, wxID_ANY);
    36     //添加一个按钮
    37     wxButton * button = new wxButton(panel, wxID_EXIT, wxT("Quit"), wxPoint(20, 20));
    38     button->SetFocus();//按钮自动获取焦点
    39 
    40 
    41     Centre();
    42 }
    43 //声明应用程序
    44 IMPLEMENT_APP(MyApp)
    45 
    46 //初始化应用程序
    47 bool MyApp::OnInit()
    48 {
    49     MyFrame *myframe = new MyFrame(wxT("MyFrame"));
    50     myframe->Show(true);
    51 
    52     return true;
    53 }
    54 
    55 //定义事件表,完成事件和处理函数的映射
    56 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    57     EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
    58     EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
    59     EVT_BUTTON(wxID_EXIT, MyFrame::OnQuit)
    60 END_EVENT_TABLE()
    61 
    62 //事件处理函数的实现
    63 void MyFrame::OnAbout(wxCommandEvent& event)
    64 {
    65     wxString msg;
    66     //设置msg的内容
    67     msg.Printf(wxT("About hello wxWidgets"), wxVERSION_STRING);
    68     //定义弹出框的内容和标题
    69     wxMessageBox(msg, wxT("About wxWidgets"), wxOK | wxICON_INFORMATION, this);
    70 }
    71 
    72 void MyFrame::OnQuit(wxCommandEvent& event)
    73 {
    74     Close(true);
    75 }

    窗口界面如下:

    二、组件之间通信

      下面是一个组件之间相互通信的例子,在这个实例中,由于每个按钮需要多次点击,因此,为按钮绑定事件处理函数,使用的动态关联时间处理函数的方法。

      静态与动态事件处理函数关联方法的区别,我们会在后面讲事件处理机制中具体讲到。

    main.h

     1 #include <wx/wx.h>
     2 #include <wx/panel.h>
     3 #include <wx/wxprec.h>
     4 #include <wx/stattext.h>
     5 
     6 const int ID_PLUS = 101;
     7 const int ID_MINUS =  102;
     8 
     9 //定义左侧面板
    10 class LeftPanel : public wxPanel
    11 {
    12 public:
    13     LeftPanel(wxPanel * parent);
    14 
    15     void OnPlus(wxCommandEvent & event);
    16     void OnMinus(wxCommandEvent & event);
    17 
    18     wxButton * m_plus;
    19     wxButton * m_minus;
    20     wxPanel * m_parent;
    21     int count;
    22 };
    23 //定义右侧面板
    24 class RightPanel : public wxPanel
    25 {
    26 public:
    27     RightPanel(wxPanel * parent);
    28 
    29     void OnSetText(wxCommandEvent & event);
    30     //静态文本控件
    31     wxStaticText * m_text;
    32 };
    33 
    34 //定义主窗口类
    35 class MyFrame : public wxFrame
    36 {
    37 public:
    38     MyFrame(const wxString& title);
    39     //定义成员变量
    40     LeftPanel * m_lp;
    41     RightPanel * m_rp;
    42 
    43     wxPanel * m_parent;
    44 
    45     //定义事件处理函数
    46     void OnQuit(wxCommandEvent& event);
    47     void OnAbout(wxCommandEvent& event);
    48 private:
    49     //声明静态事件表
    50     DECLARE_EVENT_TABLE()
    51 
    52 };
    53 //定义应用程序类
    54 class MyApp : public wxApp
    55 {
    56   public:
    57     virtual bool OnInit();
    58 };

    main.cpp

      1 #include "main.h"
      2 #include "icon.xpm"
      3 
      4 LeftPanel::LeftPanel(wxPanel * parent)
      5          : wxPanel(parent, wxID_ANY, wxPoint(-1, -1), wxSize(-1, -1), wxBORDER_SUNKEN)
      6 {
      7     //定义一个计数变量
      8     count = 0;
      9     //定义父panel
     10     m_parent = parent;
     11     //定义两个按钮
     12     m_plus = new wxButton(this, ID_PLUS, _T("+"), wxPoint(10, 10));
     13     m_minus = new wxButton(this, ID_MINUS, _T("-"), wxPoint(10, 60));
     14 
     15     //定义动态关联事件处理函数
     16     Connect(ID_PLUS, wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(LeftPanel::OnPlus));
     17     Connect(ID_MINUS, wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(LeftPanel::OnMinus));
     18 
     19 }
     20 
     21 void LeftPanel::OnPlus(wxCommandEvent & WXUNUSED(event))
     22 {
     23     //更新计数变量
     24     count++;
     25     //获取父panel指针
     26     MyFrame * comm = (MyFrame *)(m_parent->GetParent());
     27     //设置静态文本框的值
     28     comm->m_rp->m_text->SetLabel(wxString::Format(_T("%d"), count));
     29 }
     30 
     31 void LeftPanel::OnMinus(wxCommandEvent & WXUNUSED(event))
     32 {
     33     //更新计数变量
     34     count--;
     35     //获取父panel指针
     36     MyFrame * comm = (MyFrame *)(m_parent->GetParent());
     37     //设置静态文本框的值
     38     comm->m_rp->m_text->SetLabel(wxString::Format(_T("%d"), count));
     39 }
     40 
     41 //定义右侧panel
     42 RightPanel::RightPanel(wxPanel * parent)
     43           : wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(270, 150), wxBORDER_SUNKEN)
     44 {
     45     //定义一个静态文本框
     46     m_text = new wxStaticText(this, -1, _T("0"), wxPoint(40, 60));
     47 }
     48 
     49 //实现主窗口类
     50 MyFrame::MyFrame(const wxString& title)
     51        : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150))
     52 {
     53     //定义菜单
     54     wxMenu *menuFile = new wxMenu;
     55     menuFile->Append(wxID_EXIT, wxT("Exit ... 	Alt+X"), wxT("Quit this program"));
     56 
     57     wxMenu *menuHelp = new wxMenu;
     58     menuHelp->Append(wxID_ABOUT, wxT("&About ... 	F1"), wxT("Show about frame"));
     59 
     60     //定义菜单栏
     61     wxMenuBar *menuBar = new wxMenuBar;
     62 
     63     //向菜单栏添加菜单
     64     menuBar->Append(menuFile, wxT("&File"));
     65     menuBar->Append(menuHelp, wxT("&Help"));
     66 
     67     //将菜单栏添加到wxFrame中
     68     SetMenuBar(menuBar);
     69 
     70     //添加状态栏
     71     CreateStatusBar();
     72     //将状态栏分为两栏
     73     //CreateStatusBar(2);
     74     //添加状态栏显示内容
     75     SetStatusText(wxT("Welcome to wxWidgets!"));
     76 
     77     //设置应用显示图标
     78     SetIcon(wxIcon(icon_xpm));
     79 
     80     //定义父panel
     81     m_parent = new wxPanel(this, wxID_ANY);
     82     //定义一个wxBoxSizer
     83     wxBoxSizer * hbox = new wxBoxSizer(wxHORIZONTAL);
     84     //定义左右panel
     85     m_lp = new LeftPanel(m_parent);
     86     m_rp = new RightPanel(m_parent);
     87     //将左右panel加入到wxBoxSizer
     88     hbox->Add(m_lp, 1, wxEXPAND | wxALL, 5);
     89     hbox->Add(m_rp, 1, wxEXPAND | wxALL, 5);
     90     //将wxBoxSizer加载到父panel
     91     m_parent->SetSizer(hbox);
     92 
     93     //使整个wxFrame框架位于屏幕中间
     94     Centre();
     95 }
     96 
     97 //声明应用程序
     98 IMPLEMENT_APP(MyApp)
     99 
    100 //初始化应用程序
    101 bool MyApp::OnInit()
    102 {
    103     MyFrame *myframe = new MyFrame(wxT("MyFrame"));
    104     myframe->Show(true);
    105 
    106     return true;
    107 }
    108 
    109 //定义静态事件表,完成事件和处理函数的映射
    110 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    111     EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
    112     EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
    113 END_EVENT_TABLE()
    114 
    115 //事件处理函数的实现
    116 void MyFrame::OnAbout(wxCommandEvent& event)
    117 {
    118     wxString msg;
    119     //设置msg的内容
    120     msg.Printf(wxT("About hello wxWidgets"), wxVERSION_STRING);
    121     //定义弹出框的内容和标题
    122     wxMessageBox(msg, wxT("About wxWidgets"), wxOK | wxICON_INFORMATION, this);
    123 }
    124 
    125 void MyFrame::OnQuit(wxCommandEvent& event)
    126 {
    127     Close();
    128 }

    窗口界面如下:

    点击左侧的加减按钮,右侧的数据会同步更新。

  • 相关阅读:
    ORACLE错误笔记
    SQL-考试各科目的成绩以及各科目的参与考试次数
    MySql有几条更新sql不能执行时的解决方案
    面试中常遇到的算法面试题
    MySql绿色版安装教程
    Javaweb
    关于JavaWeb不使用框架上传文件的简单实现
    SSM框架
    数据库
    J2SE
  • 原文地址:https://www.cnblogs.com/Long-w/p/9580340.html
Copyright © 2011-2022 走看看