Codeblocks + Widgets 创建窗口代码分析

整理了一下在linux下,codeblocks中用widgets来创建窗口的大致步骤,直接看图片。

 1.wx01Main.cpp

/***************************************************************
 * Name:      wx01Main.cpp
 * Purpose:   Code for Application Frame
 * Author:    Gary (garyao@126.com)
 * Created:   2022-07-30
 * Copyright: Gary ()
 * License:
 **************************************************************/
//预编译
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "wx01Main.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;
}



wx01Dialog::wx01Dialog(wxDialog *dlg)
    : GUIDialog(dlg)
{
}

wx01Dialog::~wx01Dialog()
{
}
//8. 绑定的事件
void wx01Dialog::OnClose(wxCloseEvent &event)
{
    Destroy();
}

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

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

2. wxMain.h

/***************************************************************
 * Name:      wx01Main.h
 * Purpose:   Defines Application Frame
 * Author:    Gary (garyao@126.com)
 * Created:   2022-07-30
 * Copyright: Gary ()
 * License:
 **************************************************************/

#ifndef WX01MAIN_H
#define WX01MAIN_H



#include "wx01App.h"



#include "GUIDialog.h"

class wx01Dialog: public GUIDialog
{
    public:
        wx01Dialog(wxDialog *dlg);
        ~wx01Dialog();
    private:
        //7. 虚拟事件,在主程序里重写
        virtual void OnClose(wxCloseEvent& event);
        virtual void OnQuit(wxCommandEvent& event);
        virtual void OnAbout(wxCommandEvent& event);
};
#endif // WX01MAIN_H

3. wx01App.h

/***************************************************************
 * Name:      wx01App.h
 * Purpose:   Defines Application Class
 * Author:    Gary (garyao@126.com)
 * Created:   2022-07-30
 * Copyright: Gary ()
 * License:
 **************************************************************/

#ifndef WX01APP_H
#define WX01APP_H
//1.首先,需要继承wxApp
#include <wx/app.h>

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

#endif // WX01APP_H

4. wx01App.cpp

/***************************************************************
 * Name:      wx01App.cpp
 * Purpose:   Code for Application Class
 * Author:    Gary (garyao@126.com)
 * Created:   2022-07-30
 * Copyright: Gary ()
 * License:
 **************************************************************/

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

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "wx01App.h"
#include "wx01Main.h"
//3. 主函数表明程序的执行
IMPLEMENT_APP(wx01App);
//4. 程序初始化
bool wx01App::OnInit()
{

    wx01Dialog* dlg = new wx01Dialog(0L);

    dlg->Show();
    return true;
}

5. GUIDialog.h

///
// C++ code generated with wxFormBuilder (version Feb 17 2007)
// http://www.wxformbuilder.org/
//
// PLEASE DO "NOT" EDIT THIS FILE!
///

#ifndef __GUIDialog__
#define __GUIDialog__

// Define WX_GCH in order to support precompiled headers with GCC compiler.
// You have to create the header "wx_pch.h" and include all files needed
// for compile your gui inside it.
// Then, compile it and place the file "wx_pch.h.gch" into the same
// directory that "wx_pch.h".
#ifdef WX_GCH
#include <wx_pch.h>
#else
#include <wx/wx.h>
#endif

#include <wx/button.h>
#include <wx/statline.h>

//2.其次,需要一个继承与wxDialog或wxFrame的类
///

///
/// Class GUIDialog
///
class GUIDialog : public wxDialog
{
    DECLARE_EVENT_TABLE()
    private:
        //6. 事件绑定
        // Private event handlers
        void _wxFB_OnClose( wxCloseEvent& event ){ OnClose( event ); }
        void _wxFB_OnAbout( wxCommandEvent& event ){ OnAbout( event ); }
        void _wxFB_OnQuit( wxCommandEvent& event ){ OnQuit( event ); }


    protected:
        enum
        {
            idBtnAbout = 1000,
            idBtnQuit,
        };

        wxStaticText* m_staticText1;
        wxButton* BtnAbout;
        wxStaticLine* m_staticline1;
        wxButton* BtnQuit;

        // Virtual event handlers, overide them in your derived class
        virtual void OnClose( wxCloseEvent& event ){ event.Skip(); }
        virtual void OnAbout( wxCommandEvent& event ){ event.Skip(); }
        virtual void OnQuit( wxCommandEvent& event ){ event.Skip(); }


    public:
        GUIDialog( wxWindow* parent, int id = wxID_ANY, wxString title = wxT("wxWidgets Application Template"), wxPoint pos = wxDefaultPosition, wxSize size = wxDefaultSize, int style = wxDEFAULT_DIALOG_STYLE );

};

#endif //__GUIDialog__

6. GUIDialog.cpp

///
// C++ code generated with wxFormBuilder (version Feb 17 2007)
// http://www.wxformbuilder.org/
//
// PLEASE DO "NOT" EDIT THIS FILE!
///

#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

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

#include "GUIDialog.h"

///
BEGIN_EVENT_TABLE( GUIDialog, wxDialog )
    EVT_CLOSE( GUIDialog::_wxFB_OnClose )
    EVT_BUTTON( idBtnAbout, GUIDialog::_wxFB_OnAbout )
    EVT_BUTTON( idBtnQuit, GUIDialog::_wxFB_OnQuit )
END_EVENT_TABLE()
//5. 构建窗口
GUIDialog::GUIDialog( wxWindow* parent, int id, wxString title, wxPoint pos, wxSize size, int style ) : wxDialog( parent, id, title, pos, size, style )
{
    this->SetSizeHints( wxDefaultSize, wxDefaultSize );

    wxBoxSizer* bSizer1;
    bSizer1 = new wxBoxSizer( wxHORIZONTAL );

    m_staticText1 = new wxStaticText( this, wxID_ANY, wxT("Welcome To\nwxWidgets"), wxDefaultPosition, wxDefaultSize, 0 );
    m_staticText1->SetFont( wxFont( 20, 74, 90, 90, false, wxT("Arial") ) );

    bSizer1->Add( m_staticText1, 0, wxALL|wxEXPAND, 5 );

    wxBoxSizer* bSizer2;
    bSizer2 = new wxBoxSizer( wxVERTICAL );

    BtnAbout = new wxButton( this, idBtnAbout, wxT("&About"), wxDefaultPosition, wxDefaultSize, 0 );
    bSizer2->Add( BtnAbout, 0, wxALL, 5 );

    m_staticline1 = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
    bSizer2->Add( m_staticline1, 0, wxALL|wxEXPAND, 5 );

    BtnQuit = new wxButton( this, idBtnQuit, wxT("&Quit"), wxDefaultPosition, wxDefaultSize, 0 );
    bSizer2->Add( BtnQuit, 0, wxALL, 5 );

    bSizer1->Add( bSizer2, 1, wxEXPAND, 5 );

    this->SetSizer( bSizer1 );
    this->Layout();
    bSizer1->Fit( this );
}

7. 最后还有一个文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<wxFormBuilder_Project>
    <FileVersion major="1" minor="5" />
    <object class="Project" expanded="1">
        <property name="bitmaps"></property>
        <property name="code_generation">C++</property>
        <property name="encoding">UTF-8</property>
        <property name="file">GUIDialog</property>
        <property name="first_id">1000</property>
        <property name="icons"></property>
        <property name="internationalize">0</property>
        <property name="name">MyProject</property>
        <property name="path">.</property>
        <property name="precompiled_header">wx/wxprec.h</property>
        <property name="relative_path">1</property>
        <property name="use_enum">1</property>
        <property name="use_microsoft_bom">0</property>
        <object class="Dialog" expanded="1">
            <property name="bg"></property>
            <property name="center"></property>
            <property name="enabled">1</property>
            <property name="extra_style"></property>
            <property name="fg"></property>
            <property name="font"></property>
            <property name="hidden">0</property>
            <property name="id">wxID_ANY</property>
            <property name="maximum_size"></property>
            <property name="minimum_size"></property>
            <property name="name">GUIDialog</property>
            <property name="pos"></property>
            <property name="size"></property>
            <property name="style">wxDEFAULT_DIALOG_STYLE</property>
            <property name="subclass"></property>
            <property name="title">wxWidgets Application Template</property>
            <property name="tooltip"></property>
            <property name="window_extra_style"></property>
            <property name="window_style"></property>
            <event name="OnClose">OnClose</event>
            <event name="OnSize"></event>
            <object class="wxBoxSizer" expanded="1">
                <property name="minimum_size"></property>
                <property name="name">bSizer1</property>
                <property name="orient">wxHORIZONTAL</property>
                <object class="sizeritem" expanded="1">
                    <property name="border">5</property>
                    <property name="flag">wxALL|wxEXPAND</property>
                    <property name="proportion">0</property>
                    <object class="wxStaticText" expanded="1">
                        <property name="bg"></property>
                        <property name="enabled">1</property>
                        <property name="fg"></property>
                        <property name="font">Arial,90,90,20</property>
                        <property name="hidden">0</property>
                        <property name="id">wxID_ANY</property>
                        <property name="label">Welcome To&#x0A;wxWidgets</property>
                        <property name="maximum_size"></property>
                        <property name="minimum_size"></property>
                        <property name="name">m_staticText1</property>
                        <property name="permission">protected</property>
                        <property name="pos"></property>
                        <property name="size"></property>
                        <property name="style"></property>
                        <property name="subclass"></property>
                        <property name="tooltip"></property>
                        <property name="window_extra_style"></property>
                        <property name="window_style"></property>
                    </object>
                </object>
                <object class="sizeritem" expanded="1">
                    <property name="border">5</property>
                    <property name="flag">wxEXPAND</property>
                    <property name="proportion">1</property>
                    <object class="wxBoxSizer" expanded="1">
                        <property name="minimum_size"></property>
                        <property name="name">bSizer2</property>
                        <property name="orient">wxVERTICAL</property>
                        <object class="sizeritem" expanded="1">
                            <property name="border">5</property>
                            <property name="flag">wxALL</property>
                            <property name="proportion">0</property>
                            <object class="wxButton" expanded="1">
                                <property name="bg"></property>
                                <property name="enabled">1</property>
                                <property name="fg"></property>
                                <property name="font"></property>
                                <property name="hidden">0</property>
                                <property name="id">idBtnAbout</property>
                                <property name="label">&amp;About</property>
                                <property name="maximum_size"></property>
                                <property name="minimum_size"></property>
                                <property name="name">BtnAbout</property>
                                <property name="permission">protected</property>
                                <property name="pos"></property>
                                <property name="size"></property>
                                <property name="style"></property>
                                <property name="subclass"></property>
                                <property name="tooltip"></property>
                                <property name="window_extra_style"></property>
                                <property name="window_style"></property>
                                <event name="OnButtonClick">OnAbout</event>
                            </object>
                        </object>
                        <object class="sizeritem" expanded="1">
                            <property name="border">5</property>
                            <property name="flag">wxALL|wxEXPAND</property>
                            <property name="proportion">0</property>
                            <object class="wxStaticLine" expanded="1">
                                <property name="bg"></property>
                                <property name="enabled">1</property>
                                <property name="fg"></property>
                                <property name="font"></property>
                                <property name="hidden">0</property>
                                <property name="id">wxID_ANY</property>
                                <property name="maximum_size"></property>
                                <property name="minimum_size"></property>
                                <property name="name">m_staticline1</property>
                                <property name="permission">protected</property>
                                <property name="pos"></property>
                                <property name="size"></property>
                                <property name="style">wxLI_HORIZONTAL</property>
                                <property name="subclass"></property>
                                <property name="tooltip"></property>
                                <property name="window_extra_style"></property>
                                <property name="window_style"></property>
                            </object>
                        </object>
                        <object class="sizeritem" expanded="1">
                            <property name="border">5</property>
                            <property name="flag">wxALL</property>
                            <property name="proportion">0</property>
                            <object class="wxButton" expanded="1">
                                <property name="bg"></property>
                                <property name="enabled">1</property>
                                <property name="fg"></property>
                                <property name="font"></property>
                                <property name="hidden">0</property>
                                <property name="id">idBtnQuit</property>
                                <property name="label">&amp;Quit</property>
                                <property name="maximum_size"></property>
                                <property name="minimum_size"></property>
                                <property name="name">BtnQuit</property>
                                <property name="permission">protected</property>
                                <property name="pos"></property>
                                <property name="size"></property>
                                <property name="style"></property>
                                <property name="subclass"></property>
                                <property name="tooltip"></property>
                                <property name="window_extra_style"></property>
                                <property name="window_style"></property>
                                <event name="OnButtonClick">OnQuit</event>
                            </object>
                        </object>
                    </object>
                </object>
            </object>
        </object>
    </object>
</wxFormBuilder_Project>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水滴与鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值