C++ GUI库学习

wxWidgets入门:VS2019配置wxWidgets库

系统环境:Win10专业版

VStudio版本:2019

wxWidgets版本:3.2.2.1

wxWidgets历史

WxWidgets 是一个开源的C++框架,允许用C++和其他语言编写具有本地外观的跨平台GUI应用程序。

与其他类似的库相比,wxWidgets具有以下优点:

  1. 唯一一个通过包装原生GUI小部件构建的C++ GUI库,在每个平台上都能产生最佳的用户体验。
  2. 只使用标准C++编写,不依赖任何自定义扩展或预处理。
  3. 开源并且可以免费在商业项目中使用。

了解了这些后。Let’s Go!首先去下载wxWidgets的源码,这里我选择的是7z格式。

编译wxWidgets源代码

找到wx_vc17.sln,然后使用VS2019打开。

image-20230411233638500

选择批生成,全选,然后点击生成按钮:

image-20230411233926968

生成结束后新建一个Windows桌面应用程序。

image-20230412001701450

删除其中的文件夹,然后新建一个类。添加头文件目录D:\gitRep\wxWidgets\include和D:\gitRep\wxWidgets\include\msvc;然后添加lib库目录D:\gitRep\wxWidgets\lib\vc_lib即可。

创建一个Hello World界面!代码如下:

#include <wx/wxprec.h>
#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
    MyFrame();
private:
    void OnHello(wxCommandEvent& event);
    void OnExit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
};
enum
{
    ID_Hello = 1
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame();
    frame->Show(true);
    return true;
}
MyFrame::MyFrame()
    : wxFrame(NULL, wxID_ANY, "Hello World")
{
    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!");
    Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
    Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}
void MyFrame::OnExit(wxCommandEvent& event)
{
    Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
    wxMessageBox("This is a wxWidgets Hello World example",
                 "About Hello World", wxOK | wxICON_INFORMATION);
}
void MyFrame::OnHello(wxCommandEvent& event)
{
    wxLogMessage("Hello world from wxWidgets!");
}

运行成功后如下:

image-20230412002217028

FLTK踩坑之路:CodeBlocks配置FLTK库

FLTK库

系统环境:Win7旗舰版

CodeBlocks版本:20.3

FLTK版本:1.3.8

Mingw版本:4.2.1(CodeBlocks自带的编译器,无需额外配置编译器)