学习wxWidgets第2篇:wxWidgets框架程序简析

用Codeblocks生成的项目结构如下,2个头文件,2个实现文件,1个资源文件

1、头文件notepadApp.h内容如下:我加了一些自已的理解注释。

/***************************************************************
 * Name:      notepadApp.h
 * Purpose:   Defines Application Class
 * Author:    whxie (xwh121@sina.com)
 * Created:   2022-03-27
 * Copyright: whxie ()
 * License:
 **************************************************************/

#ifndef NOTEPADAPP_H
#define NOTEPADAPP_H

#include <wx/app.h>        //调用wxwidgets的框架应用的头文件

class notepadApp : public wxApp        //创建一个类,继承自wxApp
{
    public:
        virtual bool OnInit();        // 必需要有一个虚函数OnInit来初始化
};

#endif // NOTEPADAPP_H

2.实现文件notepadApp.cpp的内容如下:前面的WX_PRECOMP大概是有预编译才用到,_BORLANDC_可能和编译器有关。

/***************************************************************
 * Name:      notepadApp.cpp
 * Purpose:   Code for Application Class
 * Author:    whxie (xwh121@sina.com)
 * Created:   2022-03-27
 * Copyright: whxie ()
 * License:
 **************************************************************/

#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "notepadApp.h"
#include "notepadMain.h"

IMPLEMENT_APP(notepadApp);        //一个宏,其作用是创建一个新的应用程序对象

bool notepadApp::OnInit()  //创建一个MyApp继承自wxApp。文档上说,必须重写OnInit方法。
{
 //创建一个框架对象,这个框架是notepadMain.h中定义的类。
    notepadFrame* frame = new notepadFrame(0L, _("wxWidgets Application Template"));  
    frame->SetIcon(wxICON(aaaa)); // To Set App Icon,设置应用程序图标
    frame->Show();
    
    return true;
}

3、文件notepadMain.h中的内容:

/***************************************************************
 * Name:      notepadMain.h
 * Purpose:   Defines Application Frame
 * Author:    whxie (xwh121@sina.com)
 * Created:   2022-03-27
 * Copyright: whxie ()
 * License:
 **************************************************************/

#ifndef NOTEPADMAIN_H
#define NOTEPADMAIN_H

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include "notepadApp.h"

//创建一个类,继承自wxFrame

class notepadFrame: public wxFrame
{
    public:
        notepadFrame(wxFrame *frame, const wxString& title);
        ~notepadFrame();
    private:
        enum
        {
            idMenuQuit = 1000,
            idMenuAbout
        };

//功能函数
        void OnClose(wxCloseEvent& event);
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);

//事件表
        DECLARE_EVENT_TABLE()
};


#endif // NOTEPADMAIN_H

4、文件notepadMain.cpp中的内容:这个有一定c或c++基础的很好理解。

/***************************************************************
 * Name:      notepadMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    whxie (xwh121@sina.com)
 * Created:   2022-03-27
 * Copyright: whxie ()
 * License:
 **************************************************************/

#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "notepadMain.h"

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__WXMAC__)
        wxbuild << _T("-Mac");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//需要用到的一些事件

BEGIN_EVENT_TABLE(notepadFrame, wxFrame)
    EVT_CLOSE(notepadFrame::OnClose)
    EVT_MENU(idMenuQuit, notepadFrame::OnQuit)
    EVT_MENU(idMenuAbout, notepadFrame::OnAbout)
END_EVENT_TABLE()

notepadFrame::notepadFrame(wxFrame *frame, const wxString& title)
    : wxFrame(frame, -1, title)
{
#if wxUSE_MENUS
    // create a menu bar
    wxMenuBar* mbar = new wxMenuBar();
    wxMenu* fileMenu = new wxMenu(_T(""));
    fileMenu->Append(idMenuQuit, _("&Quit\tAlt-F4"), _("Quit the application"));
    mbar->Append(fileMenu, _("&File"));

    wxMenu* helpMenu = new wxMenu(_T(""));
    helpMenu->Append(idMenuAbout, _("&About\tF1"), _("Show info about this application"));
    mbar->Append(helpMenu, _("&Help"));

    SetMenuBar(mbar);
#endif // wxUSE_MENUS

#if wxUSE_STATUSBAR
    // create a status bar with some information about the used wxWidgets version
    CreateStatusBar(2);
    SetStatusText(_("Hello Code::Blocks user!"),0);
    SetStatusText(wxbuildinfo(short_f), 1);
#endif // wxUSE_STATUSBAR

}


notepadFrame::~notepadFrame()
{
}

void notepadFrame::OnClose(wxCloseEvent &event)
{
    Destroy();
}

void notepadFrame::OnQuit(wxCommandEvent &event)
{
    Destroy();
}

void notepadFrame::OnAbout(wxCommandEvent &event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}
 

以上就是一个简单程序或称为项目的框架简析。码字很累,年纪大了,眼睛吃不消。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值