1.下载
官网:https://www.wxwidgets.org/downloads/
下载zip文件即可,也可以下载右边的官方文档。解压路径不要有空格和中文!!!我的解压路径是(D:\wxWidgets-3.1.4)
2.添加环境变量
在系统变量里面添加:变量名:WXWIN 值:D:\wxWidgets-3.1.4(就是你的解压路径,文件夹的绝对路径)
3.编译源码
找到这个路径:D:\wxWidgets-3.1.4\build\msw下的 wx_vc16.sln,用vs2019打开。
官方文档中列出的 VC 与 MSVS 对应关系:
VC++ 7, 8, 9, 10, 11, 12, 14, 15 and 16 ( MSVS 2003, 2005, 2008, 2010, 2012, 2013, 2015, 2017 and 2019)
选择Debug,x64,配置类型选择静态库!然后生成解决方案。这样就在D:\wxWidgets-3.1.4\lib路径下生成了vc_x64_lib文件夹。
4.项目配置
创建一个空项目,编辑工程属性,须在【项目属性页】做如下修改:
配置属性 - C/C++ - 常规 - 附加包含目录:添加 $(WXWIN)\include\msvc 和 $(WXWIN)\include
配置属性 - C/C++ - 预处理器 - 预处理器定义:添加 WXMSW 、_UNICODE、NDEBUG,这里若不加_UNICODE,则需要在 配置属性 - 高级 - 高级属性 - 字符集 选择【使用 Unicode 字符集】
配置属性 - 链接器 - 常规 - 附加库目录:添加 $(WXWIN)\lib\vc_x64_lib
配置属性 - 链接器 - 系统 - 子系统:选择【窗口】(默认为【控制台】)
5.测试
官方hello word测试案例:
#include "wx\wxprec.h"
#include "wx\wx.h"
#include <iostream>
#include <string>
using namespace std;
#ifndef WX_PRECOMP
#include "include\wx\wx.h"
#endif
class MyApp : public wxApp
{
public: virtual bool OnInit();
};
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);
// main function this macro is the main function
// create an application instance and starts the program
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",
"Hello string show 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!");
}