• 2、wxWidgets介绍--菜单栏、状态栏、图标简介


    wxWidgets

    wxWidgets是一个用来编写C++程序的GUI(图形用户界面)工具包。它是一个开源的、成熟的、跨平台的工具包。wxWidgets应用程序能在所有主流的操作系统上运行,Windows,Unix,Mac。这个项目由Julian Smart在1992年启动。wxWidgets提供各种各样的C++类来处理数据流、数据库、多线程、在线帮助、应用程序设置。wxWidgets由大量的窗口小部件组成。

    接下来我们一步步的完成一个Hello World的窗口程序:

    首先,搭建一个非常简单的框架:

     1 #include <wx/wx.h>
     2 
     3 class MyFrame : public wxFrame
     4 {
     5 public:
     6     MyFrame(const wxString& title): wxFrame(NULL, wxID_ANY, title) {}
     7 
     8 };
     9 
    10 class MyApp : public wxApp
    11 {
    12 public:
    13     virtual bool OnInit()
    14     {
    15         MyFrame * pframe = new MyFrame(wxT("Hello World"));
    16         pframe->Show(true);
    17         return true;
    18     }
    19 
    20 };
    21 IMPLEMENT_APP(MyApp)

    执行结果:

    特点:

    1. 没有main()函数,实际上是包含在wxWidgets框架中看不到。
    2. 一个应用程序类App,一个框架类Frame
    3. FrameAppOnInit()函数中实例化。
      注意:这里Frame没有销毁,可能会内存泄露(通常这么处理也不会有太大问题,因为窗口关闭时,OS会收回所有资源)。

    然后给窗口添加状态栏

     1 #include <wx/wx.h>
     2 
     3 //框架类wxFrame
     4 class MyFrame : public wxFrame
     5 {
     6 public:
     7     MyFrame(const wxString& title): wxFrame(NULL, wxID_ANY, title)
     8     {
     9         //添加状态栏
    10         CreateStatusBar();
    11         //将状态栏分为两栏
    12         //CreateStatusBar(2);
    13         SetStatusText(wxT("Welcome to wxWidgets!"));
    14     }
    15 
    16 };
    17 
    18 //应用程序类APP
    19 class MyApp : public wxApp
    20 {
    21 public:
    22     //Frame的实例化
    23     virtual bool OnInit()
    24     {
    25         MyFrame * pframe = new MyFrame(wxT("Hello World"));
    26         pframe->Show(true);
    27         return true;
    28     }
    29 
    30 };
    31 //声明应用程序
    32 IMPLEMENT_APP(MyApp)

    添加菜单

     1 #include <wx/wx.h>
     2 
     3 //框架类wxFrame
     4 class MyFrame : public wxFrame
     5 {
     6 public:
     7     MyFrame(const wxString& title): wxFrame(NULL, wxID_ANY, title)
     8     {
     9         //定义菜单
    10         wxMenu *menuFile = new wxMenu;
    11         menuFile->Append(wxID_EXIT, wxT("Exit ... 	Alt+X"), wxT("Quit this program"));
    12 
    13         wxMenu *menuHelp = new wxMenu;
    14         menuHelp->Append(wxID_ABORT, wxT("&About ... 	F1"), wxT("Show about frame"));
    15 
    16         //定义菜单栏
    17         wxMenuBar *menuBar = new wxMenuBar;
    18         
    19         //向菜单栏添加菜单
    20         menuBar->Append(menuFile, wxT("&File"));
    21         menuBar->Append(menuHelp, wxT("&Help"));
    22         
    23         //将菜单栏添加到wxFrame中
    24         SetMenuBar(menuBar);
    25 
    26         //添加状态栏
    27         CreateStatusBar();
    28         //将状态栏分为两栏
    29         //CreateStatusBar(2);
    30         SetStatusText(wxT("Welcome to wxWidgets!"));
    31     }
    32 
    33 };
    34 
    35 //应用程序类APP
    36 class MyApp : public wxApp
    37 {
    38 public:
    39     //Frame的实例化
    40     virtual bool OnInit()
    41     {
    42         MyFrame * pframe = new MyFrame(wxT("Hello World"));
    43         pframe->Show(true);
    44         return true;
    45     }
    46 
    47 };
    48 //声明应用程序
    49 IMPLEMENT_APP(MyApp)

    添加菜单事件、添加应用显示图标

     1 #include <wx/wx.h>
     2 #include "icon.xpm"
     3 
     4 //框架类wxFrame
     5 class MyFrame : public wxFrame
     6 {
     7 public:
     8     MyFrame(const wxString& title): wxFrame(NULL, wxID_ANY, title)
     9     {
    10         //定义菜单
    11         wxMenu *menuFile = new wxMenu;
    12         menuFile->Append(wxID_EXIT, wxT("Exit ... 	Alt+X"), wxT("Quit this program"));
    13 
    14         wxMenu *menuHelp = new wxMenu;
    15         menuHelp->Append(wxID_ABOUT, wxT("&About ... 	F1"), wxT("Show about frame"));
    16 
    17         //定义菜单栏
    18         wxMenuBar *menuBar = new wxMenuBar;
    19 
    20         //向菜单栏添加菜单
    21         menuBar->Append(menuFile, wxT("&File"));
    22         menuBar->Append(menuHelp, wxT("&Help"));
    23 
    24         //将菜单栏添加到wxFrame中
    25         SetMenuBar(menuBar);
    26 
    27         //添加状态栏
    28         CreateStatusBar();
    29         //将状态栏分为两栏
    30         //CreateStatusBar(2);
    31         SetStatusText(wxT("Welcome to wxWidgets!"));
    32 
    33         //设置应用显示图标
    34         SetIcon(wxIcon(icon_xpm));
    35 
    36     }
    37 
    38     //定义事件处理函数
    39     void OnQuit(wxCommandEvent& event);
    40     void OnAbout(wxCommandEvent& event);
    41 private:
    42     //声明事件表
    43     DECLARE_EVENT_TABLE()
    44 };
    45 
    46 //应用程序类APP
    47 class MyApp : public wxApp
    48 {
    49 public:
    50     //Frame的实例化
    51     virtual bool OnInit()
    52     {
    53         MyFrame * pframe = new MyFrame(wxT("Hello World"));
    54         pframe->Show(true);
    55         return true;
    56     }
    57 
    58 };
    59 //声明应用程序
    60 IMPLEMENT_APP(MyApp)
    61 
    62 //定义事件表,完成事件和处理函数的映射
    63 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    64     EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
    65     EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
    66 END_EVENT_TABLE()
    67 
    68 //事件处理函数的实现
    69 void MyFrame::OnAbout(wxCommandEvent& event)
    70 {
    71     wxString msg;
    72     msg.Printf(wxT("About hello wxWidgets"), wxVERSION_STRING);
    73 
    74     wxMessageBox(msg, wxT("About wxWidgets"), wxOK | wxICON_INFORMATION, this);
    75 }
    76 
    77 void MyFrame::OnQuit(wxCommandEvent& event)
    78 {
    79     Close();
    80 }

    icon.xpm

     1 /* XPM */
     2 static const char * icon_xpm[] = {
     3 /* columns rows colors chars-per-pixel */
     4 "32 32 6 1",
     5 "  c black",
     6 ". c navy",
     7 "X c red",
     8 "o c yellow",
     9 "O c gray100",
    10 "+ c None",
    11 /* pixels */
    12 "++++++++++++++++++++++++++++++++",
    13 "++++++++++++++++++++++++++++++++",
    14 "++++++++++++++++++++++++++++++++",
    15 "++++++++++++++++++++++++++++++++",
    16 "++++++++++++++++++++++++++++++++",
    17 "++++++++              ++++++++++",
    18 "++++++++ ............ ++++++++++",
    19 "++++++++ ............ ++++++++++",
    20 "++++++++ .OO......... ++++++++++",
    21 "++++++++ .OO......... ++++++++++",
    22 "++++++++ .OO......... ++++++++++",
    23 "++++++++ .OO......              ",
    24 "++++++++ .OO...... oooooooooooo ",
    25 "         .OO...... oooooooooooo ",
    26 " XXXXXXX .OO...... oOOooooooooo ",
    27 " XXXXXXX .OO...... oOOooooooooo ",
    28 " XOOXXXX ......... oOOooooooooo ",
    29 " XOOXXXX ......... oOOooooooooo ",
    30 " XOOXXXX           oOOooooooooo ",
    31 " XOOXXXXXXXXX ++++ oOOooooooooo ",
    32 " XOOXXXXXXXXX ++++ oOOooooooooo ",
    33 " XOOXXXXXXXXX ++++ oOOooooooooo ",
    34 " XOOXXXXXXXXX ++++ oooooooooooo ",
    35 " XOOXXXXXXXXX ++++ oooooooooooo ",
    36 " XXXXXXXXXXXX ++++              ",
    37 " XXXXXXXXXXXX ++++++++++++++++++",
    38 "              ++++++++++++++++++",
    39 "++++++++++++++++++++++++++++++++",
    40 "++++++++++++++++++++++++++++++++",
    41 "++++++++++++++++++++++++++++++++",
    42 "++++++++++++++++++++++++++++++++",
    43 "++++++++++++++++++++++++++++++++"
    44 };
  • 相关阅读:
    NSString拼接字符串
    2020/4/26
    2020/4/25
    2020/4/24
    2020/4/22
    2020/4/22
    2020/4/20
    2020/4/19
    2020/4/18
    2020/4/17
  • 原文地址:https://www.cnblogs.com/Long-w/p/9580316.html
Copyright © 2020-2023  润新知