wxWidgets Tips: 用 Visual Studio 2015 创建 wxWidgets 应用程序 (2)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/XinYaping/article/details/51614454

上文 中,我们创建了第一个 wxWidgets 应用程序。但是我们这个程序很不理想,所有的东西都堆在一个 main.cpp 里面了,很不“工程化”。接下来我们把这个例子做了一个修改,把代码分散到了下面几个文件中:

MainFrameBaseClass.h

#ifndef MainFrameBaseClass_H
#define MainFrameBaseClass_H

#include <wx/settings.h>
#include <wx/xrc/xmlres.h>
#include <wx/xrc/xh_bmp.h>
#include <wx/frame.h>
#include <wx/iconbndl.h>
#include <wx/artprov.h>
#include <wx/sizer.h>
#include <wx/panel.h>
#include <wx/splitter.h>
#include <wx/menu.h>
#include <wx/toolbar.h>
#include <wx/stattext.h>
#include <wx/button.h>
#include <wx/statusbr.h>
#include <wx/grid.h>
#include <wx/window.h>
#include <wx/treectrl.h>
#include <wx/treelist.h>
#include <wx/treebase.h>

enum
{
    ID_Hello = 1
};

class MainFrameBaseClass : public wxFrame
{
public:
    MainFrameBaseClass(
        wxWindow* parent,
        wxWindowID id = wxID_ANY,
        const wxString& title = _("Simulation Window"),
        const wxPoint& pos = wxDefaultPosition,
        const wxSize& size = wxSize(800, 600),
        long style = wxCAPTION | wxRESIZE_BORDER | wxMAXIMIZE_BOX | wxMINIMIZE_BOX | wxSYSTEM_MENU | wxCLOSE_BOX);

    virtual ~MainFrameBaseClass();

protected:
    virtual void OnExit(wxCommandEvent& event) { event.Skip(); }
    virtual void OnAbout(wxCommandEvent& event) { event.Skip(); }
    virtual void OnHello(wxCommandEvent& event) { event.Skip(); }

private:
    void Initialize();
    void ConnectEvents();
    void DisconnectEvents();
};

#endif // MainFrameBaseClass_H

MainFrameBaseClass.cpp

#include "MainFrameBaseClass.h"

static wxWindowID ID_MenuItem_Hello = 0;
static wxWindowID ID_MenuItem_EXIT = 0;
static wxWindowID ID_MenuItem_About = 0;

static int get_unique_id();

static wxPanel* CreateMainPanel(wxWindow* const parent);
static wxMenuBar* CreateMainMenuBar(wxFrameBase* const parent);

MainFrameBaseClass::MainFrameBaseClass(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style)
    : wxFrame(parent, id, title, pos, size, style)
{
    this->Initialize();
    this->ConnectEvents();
}

MainFrameBaseClass::~MainFrameBaseClass()
{
    this->DisconnectEvents();
}

void MainFrameBaseClass::Initialize()
{
    wxPanel* mainPanel = NULL;
    wxMenuBar* menuBar = NULL;

    mainPanel = CreateMainPanel(this);
    menuBar = CreateMainMenuBar(this);

    CreateStatusBar();
    SetStatusText("Welcome to wxWidgets!");
}

void MainFrameBaseClass::ConnectEvents()
{
    this->Connect(ID_MenuItem_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrameBaseClass::OnExit), NULL, this);
    this->Connect(ID_MenuItem_About, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrameBaseClass::OnAbout), NULL, this);
    this->Connect(ID_MenuItem_Hello, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrameBaseClass::OnHello), NULL, this);
}

void MainFrameBaseClass::DisconnectEvents()
{
    this->Disconnect(ID_MenuItem_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrameBaseClass::OnExit), NULL, this);
    this->Disconnect(ID_MenuItem_About, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrameBaseClass::OnAbout), NULL, this);
    this->Disconnect(ID_MenuItem_Hello, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrameBaseClass::OnHello), NULL, this);
}

static wxPanel* CreateMainPanel(wxWindow* const parent)
{
    wxBoxSizer* container = new wxBoxSizer(wxVERTICAL);
    parent->SetSizer(container);

    wxPanel* panel = new wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(-1, -1), wxTAB_TRAVERSAL);
    panel->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT));

    container->Add(panel, 1, wxEXPAND, 5);
    return panel;
}

static wxMenuBar* CreateMainMenuBar(wxFrameBase* const parent)
{
    wxMenuBar* menuBar = new wxMenuBar(0);
    parent->SetMenuBar(menuBar);

    wxMenu* menu_File = new wxMenu();
    menuBar->Append(menu_File, _("&File"));

    wxMenuItem* menuItem_Hello = new wxMenuItem(menu_File, get_unique_id(), _("&Hello...\tCtrl-H"), _("Help string shown in status bar for this menu item"), wxITEM_NORMAL);
    menu_File->Append(menuItem_Hello);
    ID_MenuItem_Hello = menuItem_Hello->GetId();

    menu_File->AppendSeparator();

    wxMenuItem* menuItem_Exit = new wxMenuItem(menu_File, get_unique_id(), _("Exit\tAlt-X"), _("Quit"), wxITEM_NORMAL);
    menu_File->Append(menuItem_Exit);
    ID_MenuItem_EXIT = menuItem_Exit->GetId();

    wxMenu* menu_Help = new wxMenu();
    menuBar->Append(menu_Help, _("&Help"));

    wxMenuItem* menuItem_About = new wxMenuItem(menu_File, get_unique_id(), _("About\tAlt-A"), _("About"), wxITEM_NORMAL);
    menu_Help->Append(menuItem_About);
    ID_MenuItem_About = menuItem_About->GetId();

    return menuBar;
}

static int get_unique_id()
{
    static int get_unique_id_raw_data = 0;
    return (++get_unique_id_raw_data);
}

MainFrame.h

#ifndef MAINFRAME_H
#define MAINFRAME_H

#include "MainFrameBaseClass.h"

class MainFrame : public MainFrameBaseClass
{
public:
    MainFrame(wxWindow* parent);
    virtual ~MainFrame();

    void OnExit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
    void OnHello(wxCommandEvent& event);
};

#endif // MAINFRAME_H

MainFrame.cpp

#include <wx/wx.h>
#include <wx/aboutdlg.h>
#include "MainFrame.h"

MainFrame::MainFrame(wxWindow* parent)
    : MainFrameBaseClass(parent)
{
}

MainFrame::~MainFrame()
{
}

void MainFrame::OnExit(wxCommandEvent& event)
{
    wxUnusedVar(event);
    Close();
}

void MainFrame::OnAbout(wxCommandEvent& event)
{
    wxMessageBox("This is a wxWidgets' Hello world sample",
        "About Hello World", wxOK | wxICON_INFORMATION);
}

void MainFrame::OnHello(wxCommandEvent & event)
{
    wxLogMessage("Hello world from wxWidgets!");
}

MainApp.cpp

// wxWidgets "Hello world" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include <wx/app.h>
#include <wx/event.h>
#include <wx/image.h>
#include "MainFrame.h"

class MainApp : public wxApp
{
public:
    virtual bool OnInit();
};

bool MainApp::OnInit()
{
    MainFrame *mainFrame = new MainFrame(NULL);
    SetTopWindow(mainFrame);

    //return mainFrame->ShowFullScreen(true, wxFULL_REPAINT_ON_RESIZE);
    return mainFrame->Show(true);
}


wxIMPLEMENT_APP(MainApp);

猜你喜欢

转载自blog.csdn.net/XinYaping/article/details/51614454