栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

#ifndef NOTEPADAPP_H
#define NOTEPADAPP_H

#include         //调用wxwidgets的框架应用的头文件

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

#endif // NOTEPADAPP_H

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

#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中的内容:

#ifndef NOTEPADMAIN_H
#define NOTEPADMAIN_H

#ifndef WX_PRECOMP
    #include
#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++基础的很好理解。

#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, _("&QuittAlt-F4"), _("Quit the application"));
    mbar->Append(fileMenu, _("&File"));

    wxMenu* helpMenu = new wxMenu(_T(""));
    helpMenu->Append(idMenuabout, _("&abouttF1"), _("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..."));
}
 

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/784549.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号