当前位置:网站首页>WxWidgets learning notes (I): Interpretation of code:: blocks template project source code
WxWidgets learning notes (I): Interpretation of code:: blocks template project source code
2022-04-22 15:10:00 【Charming Sun】
One 、 development environment
- operating system :Fedora 23
- Integrated development environment :Code::Blocks 16.01
- G++ edition :5.3.1
- GTK+ edition :2.24.30( Use command sudo dnf install gtk2 gtk2-devel gtk2-devel-docs install )
- wxWidgets edition :3.1.0( After unpacking the source package, enter the top-level directory and enter ./configure、make、sudo make install Three orders )
Two 、 newly build wxWidgets Formwork works
1、 Create a new one wxWidgets Formwork works :

2、 Choose good wxWidgets Name the new project after version , And choose the path to save the project :

3、 After entering the copyright information, select the type of template project :

4、 Configure compiler :

5、 To configure wxWidgets engineering , And complete the establishment of Formwork Engineering :

3、 ... and 、 Interpretation of template project source code
1、 To make a wxWidgets The application runs , First, you should define a wxApp Subclasses of classes , And overload wxApp::OnInit function . because wxWidgets The application has no main function , Instead, it's in wxApp Member functions defined in class “wxApp::OnInit”, and wxApp Member functions in a class can use argc and argv Two command line arguments . Use wxApp Class needs to contain header files wx/app.h, from wxApp The subclass derived from class is in TemplateApp.h The document defines , This subclass also declares a virtual function that returns a Boolean value OnInit(), If the Boolean value is true, Indicates successful initialization .
/***************************************************************
* Name: TemplateApp.h
* Purpose: Defines Application Class
* Author: charmingsun ([email protected])
* Created: 2016-06-08
* Copyright: charmingsun (NULL)
* License:
**************************************************************/
#ifndef TEMPLATEAPP_H
#define TEMPLATEAPP_H
#include <wx/app.h>
class TemplateApp : public wxApp
{
public:
virtual bool OnInit();
};
#endif // TEMPLATEAPP_H
2、wxApp In subclass OnInit Function is defined in TemplateApp.cpp In file , So this file contains the header file TemplateApp.h.TemplateApp.cpp The file first uses IMPLEMENT_APP(TemplateApp) perhaps wxIMPLEMENT_APP(TemplateApp); Macro to dynamically create and start the above wxApp Instances of subclasses ( The macro must be followed by a semicolon ).
Then the file defines wxApp A subclass OnInit function . Because in wxApp A subclass OnInit The function must contain a top-level wxFrame Class or wxDialog Class window , So the OnInit The first statement in the function defines a named frame Of TemplateFrame Class ,TemplateFrame Class is wxFrame Subclasses of classes , This subclass will be in TemplateMain.h The document defines , So the TemplateApp.cpp The file should contain TemplateMain.h This header file . This statement to frame The constructor of the instance passes two parameters , The first is long Type value 0, That is, the first parameter is empty NULL, The second parameter is _(“wxWidgets Application Template”) The return value of the function ,_() Macro and wxGetTranslation() Function equivalence , Will input const char * The string of type is converted to wxString String output of type .
The OnInit The second statement in the function explicitly calls TemplateFrame Class frame Example of Show() function , The Show() Function inherits TemplateFrame Class wxWindow The base class Show() function ,Show() The function should be called immediately after the window class is created , Make the window just created appear ,Show() The default input parameter of the function is true, This is the display window ,false Hide window for . In the end OnInit The last statement of the function returns true value , Indicates successful initialization .
* notes : About this TemplateApp.cpp Description of the two precompiled instructions at the beginning of the file : The first paragraph tells the compiler if the platform supports precompiled header files , It contains wx_pch.h This header file ; The second paragraph tells the compiler if Borland The company's C++ Builder Of IDE, It indicates that all header files contained before the precompiled instruction use precompiled header files to save compilation time .
/***************************************************************
* Name: TemplateApp.cpp
* Purpose: Code for Application Class
* Author: charmingsun ([email protected])
* Created: 2016-06-08
* Copyright: charmingsun (NULL)
* License:
**************************************************************/
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "TemplateApp.h"
#include "TemplateMain.h"
IMPLEMENT_APP(TemplateApp);
bool TemplateApp::OnInit()
{
TemplateFrame* frame = new TemplateFrame(0L, _("wxWidgets Application Template"));
frame->Show();
return true;
}
3、 The next in TemplateMain.h It's defined in the file wxFrame Subclasses of classes ——TemplateFrame class , May not include TemplateApp.h The header file .TemplateFrame Class first declares its constructor and destructor, two public member functions . Then the enumeration type is used to define idMenuQuit and idMenuAbout These two private member constants , These two constants are the identifiers of the menu items that generate the command , Will be in TemplateFrame The event table of the window class and the constructor are used .
And then it defines a name called OnClose Event handler for , This function inputs a wxCloseEvent Event classes , This class contains some information about closing windows and session events . And then it defines a name called OnQuit And a man named OnAbout Event handler for , Both functions enter a wxCommandEvent Event classes , This class contains some information about command Events . The information returned by these event handling functions is passed by input parameters , And it is usually not called by external classes , So the return value is empty , And it is usually declared as a private member function . Event handler functions are usually in the form of OnSomething(); Format naming , But there is no rule that it must be named according to this format .
Last , stay TemplateFrame A named... Is inserted at the end of the class DECLARE_EVENT_TABLE() The macro ( This macro is equivalent to wxDECLARE_EVENT_TABLE(); macro ), This macro declares a static event table for this class to handle events . This macro can be placed anywhere in the class that handles the event , But it is customary to put it at the end of the class . Because this macro will change the access type internally , So it's safest to put it at the end of the class . The class that handles the event must be by wxEvtHandler Class derived from , and wxWindow Class is from wxEvtHandler Class , So anything by wxWindow Class can receive and process events , such as wxFrame Classes and wxDialog class .
/***************************************************************
* Name: TemplateMain.h
* Purpose: Defines Application Frame
* Author: charmingsun ([email protected])
* Created: 2016-06-08
* Copyright: charmingsun (NULL)
* License:
**************************************************************/
#ifndef TEMPLATEMAIN_H
#define TEMPLATEMAIN_H
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "TemplateApp.h"
class TemplateFrame: public wxFrame
{
public:
TemplateFrame(wxFrame *frame, const wxString& title);
~TemplateFrame();
private:
enum
{
idMenuQuit = 1000,
idMenuAbout
};
void OnClose(wxCloseEvent& event);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
#endif // TEMPLATEMAIN_H
4、TemplateMain.cpp The document defines TemplateFrame Event table and event handler function in class . The file first defines a file named wxbuildinfo Help functions for , This function is used for TemplateFrame Class and a constructor named OnAbout Event handler for . The wxbuildinfo The function inputs a file named wxbuildinfoformat The value of the enumeration type , If the input is short_f, Then the function takes wxString Type return wxWidgets Version number of , If the input is long_f, Then the function takes wxString Type return wxWidgets Version number of 、 The operating system and platform are used Unicode Code or ANSI code .
Then the file defines TemplateFrame Class , The event table tells wxWidgets How the event is mapped to the function responsible for handling the event . The event table consists of wxBEGIN_EVENT_TABLE() and wxEND_EVENT_TABLE() These two macros and the relationship between them EVT_xxx Macro composition .wxBEGIN_EVENT_TABLE(theClass,baseClass) The first parameter of the macro is the class that needs to capture events , The second parameter is the base class of the class that needs to capture events ,wxEND_EVENT_TABLE() Macro has no arguments ,EVT_xxx The macro redirects the event to the member function in the parameter .EVT_CLOSE(TemplateFrame::OnClose) Macro belongs to wxCloseEvent class , This macro will command events wxEVT_CLOSE_WINDOW Event redirected to TemplateFrame Class OnClose Member functions .EVT_MENU(id, func) Macro belongs to wxCommandEvent class , The first parameter is to generate wxEVT_MENU The identifier of the menu item of the command , The second parameter is processing wxEVT_MENU Member function of command .
Then the file defines TemplateFrame Class constructor , The two parameters in the total parameter table of the constructor will be passed to TemplateFrame Class's base class constructor wxFrame::wxFrame(wxWindow * parent , wxWindowID id , const wxString & title , const wxPoint & pos = wxDefaultPosition , const wxSize & size = wxDefaultSize , long style = wxDEFAULT_FRAME_STYLE , const wxString & name = wxFrameNameStr),wxFrame The first parameter of the class is the parent class of the window , The second parameter is the identifier of the window ( The default value is -1), The third parameter is the title of the window .
The TemplateFrame The first precompiled instruction in the constructor of the class contains code that creates a menu bar . Click on the class name to read wxMenu class and wxMenuBar class Online reference manual for , Please understand the source code according to the reference manual . among _T() The macro will enter const char * The string of type is converted to Unicode Encoded string output , For details, please click the name to read the online reference manual of the macro . among SetMenuBar(mbar); Statement called wxFrame Class SetMenuBar() function , This function tells wxWidgets The frame displays the menu bar passed in by the parameter .
The TemplateFrame The code contained in the second precompiled instruction in the constructor of the class creates a status bar . among CreateStatusBar(2); Statement creates a status bar with two partitions ,SetStatusText(_(“Hello Code::Blocks user!”),0); Statement sets the text in the first section of the status bar and redraws the status bar ,SetStatusText(wxbuildinfo(short_f), 1); Statement sets the text in the second section of the status bar and redraws the status bar . For more information, please click on the class name to read wxFrame class Online reference manual for .
Finally, the file defines TemplateFrame Destructor of class 、OnClose Event handler 、OnQuit Event handling functions and OnAbout Event handler . Use virtual bool wxWindow::Destroy() Function to log out of the window safely . Use wxMessageBox(msg, _(“Welcome to…”)); Statement to create a statement titled “Welcome to…” The content is “wxWidgets 3.1.0-Linux-Unicode build” The dialog . of wxMessageBox function For more information, please click on the function name to read the online reference manual .
/*************************************************************** * Name: TemplateMain.cpp * Purpose: Code for Application Frame * Author: charmingsun ([email protected]) * Created: 2016-06-08 * Copyright: charmingsun (NULL) * License: **************************************************************/
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "TemplateMain.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(TemplateFrame, wxFrame)
EVT_CLOSE(TemplateFrame::OnClose)
EVT_MENU(idMenuQuit, TemplateFrame::OnQuit)
EVT_MENU(idMenuAbout, TemplateFrame::OnAbout)
END_EVENT_TABLE()
TemplateFrame::TemplateFrame(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
}
TemplateFrame::~TemplateFrame()
{
}
void TemplateFrame::OnClose(wxCloseEvent &event)
{
Destroy();
}
void TemplateFrame::OnQuit(wxCommandEvent &event)
{
Destroy();
}
void TemplateFrame::OnAbout(wxCommandEvent &event)
{
wxString msg = wxbuildinfo(long_f);
wxMessageBox(msg, _("Welcome to..."));
}
Four 、 Further study
wxWidgets: Hello World Example
wxWidgets: A Quick Guide to Writing Applications
wxWidgets: Programming Guides
wxWidgets tutorial
版权声明
本文为[Charming Sun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221504282619.html
边栏推荐
- C语言的基本练习(001-1)
- Database operation
- [Mysql] CHAR_LENGTH函数
- View your own system activation type
- PlatoFarm推出正式版游戏经济模型的特点分析
- When our son was 18 years old and went to college, we were already 60. What financial products are the most reliable for him to buy?
- Face recognition (5) face detection based on mctnn (pytoch)
- PAT甲级1012:The Best Rank (25)
- Leetcode interview question 17.16 Masseur (simple)
- [*CTF2022]oh_my_lotto_revenge
猜你喜欢
随机推荐
【网络】传输层-UDP协议
[*CTF2022]oh_my_lotto_revenge
带你了解极具弹性的Spark架构的原理
腾讯云IM集成(so easy)
Web automation testing framework realizes the operation of web elements through JS
如何通过云效Projex项目协同提高团队更高效的协作能力
Workplace PUA, five sins of managers
Bootsrap 模态框文字超出处理
为什么别人做自媒体月入过万?你每天才几毛几块?有哪些变现方式?
TcaplusDB君 · 行业新闻汇编(三)
ROS通信机制三---参数服务器
【深入理解TcaplusDB技术】将数据插入到列表指定位置接口说明——[List表]
[C language] learn file operation in 10 minutes
Redis gets all the elements in the list
如何用示波器和电流探头来测量小电流
[circle of friends server architecture design]
Rip introduction
When our son was 18 years old and went to college, we were already 60. What financial products are the most reliable for him to buy?
Android的UI---ZoomControls放大缩小图片,android面试简历模板
Love station network keyword mining query tool - batch website keyword mining export software free download









