layout: post
title: "wxWidgets初探"
date: 2020-09-03
author: "王玉松"
header-img: ""
categories: C++
tags:
- C++
- wxWidgets
- Debian10
wxWidgets安装
添加软件安装源
# /etc/apt/sources.list
deb https://repos.codelite.org/wx3.1.4/debian/ buster libs
# 添加软件源后执行更新命令有可能出现由于没有公钥,无法验证下列签名
# 直接下载对应的公钥后即可继续执行命令
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys {公钥}
wxWidgets相应软件
参照官网上的指示, 我选择下载基于 GTK+2 的wxWidgets版本
(原因, 首先都是稳定版本, 其次使用目的仅是作为实训项目练习,
对于版本没有特殊的固定要求, 更专注于工具运用, 理解)
下载速度可能比较缓慢
apt-get install libwxbase3.1-0-unofficial \
libwxbase3.1-dev \
libwxgtk3.1-0-unofficial \
libwxgtk3.1-dev \
wx3.1gtk2-headers \
wx-common \
libwxgtk-media3.1-0-unofficial \
libwxgtk-media3.1-dev \
libwxbase3.1-0-unofficial-dbg \
libwxgtk3.1-0-unofficial-dbg \
libwxgtk-media3.1-0-unofficial-dbg \
wx3.1-i18n \
wx3.1-examples
实例程序
// 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
//继承 wxApp 类的用户自定义的应用类型
//其中只有一个public类型的虚函数
//有关虚函数的学习
class MyApp: public wxApp
{
public:
virtual bool OnInit();
};
//继承 wxFrame 类的用户自定义的窗格类型
//其中包含有构造函数和3个私有函数用于绑定事件
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
enum
{
ID_Hello = 1
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Hello, MyFrame::OnHello)
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( "Hello World", wxPoint(50, 50), wxSize(450, 340) );
frame->Show( true );
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, "&File" );
menuBar->Append( menuHelp, "&Help" );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( "Welcome to wxWidgets!" );
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close( true );
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox( "This is a wxWidgets' Hello world sample",
"About Hello World", wxOK | wxICON_INFORMATION );
}
void MyFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
网友评论