一、下载
二、静态编译wx3.1
打开源码目录,在build -》msw中找到wx_vc14.sln,用vs2015打开
将每一个项目的运行库都改成静态编译,debug为MTD,release为MT
点击菜单栏的《生成》-》批量生成,只选择每一项的debug和release的win32版本,然后生成
等待10分钟左右,就能编译好了。
提取目录下的include与lib文件夹,这2个是我们需要的,其他可以删除。
三、环境部署
新建一个win32项目,选择windows应用程序(空项目),取消勾选“安全开发应用……”(这里很重要)
include目录中包含《wxWidgets-3.1.0\include》,还需要包含下面:
若为debug 则包含《wxWidgets-3.1.0\lib\vc_lib\mswud》
若为release 则包含《wxWidgets-3.1.0\lib\vc_lib\mswu》
lib目录包含《wxWidgets-3.1.0\lib\vc_lib》
预编译器-》__WXMSW__
运行库-》debug选择MTD,release选择MT
如果要兼容xp系统,则将平台工具集改成v140_xp
附加依赖项如下:
若为debug,则
wxbase31ud.lib wxbase31ud_net.lib wxbase31ud_xml.lib wxexpatd.lib wxjpegd.lib wxmsw31ud_adv.lib wxmsw31ud_aui.lib wxmsw31ud_core.lib wxmsw31ud_gl.lib wxmsw31ud_html.lib wxmsw31ud_media.lib wxmsw31ud_propgrid.lib wxmsw31ud_qa.lib wxmsw31ud_ribbon.lib wxmsw31ud_richtext.lib wxmsw31ud_stc.lib wxmsw31ud_webview.lib wxmsw31ud_xrc.lib wxpngd.lib wxregexud.lib wxscintillad.lib wxtiffd.lib wxzlibd.lib //*****以下是特殊包含 comctl32.lib rpcrt4.lib wsock32.lib odbc32.lib若为release,则wxbase31u.lib wxbase31u_net.lib wxbase31u_xml.lib wxexpat.lib wxjpeg.lib wxmsw31u_adv.lib wxmsw31u_aui.lib wxmsw31u_core.lib wxmsw31u_gl.lib wxmsw31u_html.lib wxmsw31u_media.lib wxmsw31u_propgrid.lib wxmsw31u_qa.lib wxmsw31u_ribbon.lib wxmsw31u_richtext.lib wxmsw31u_stc.lib wxmsw31u_webview.lib wxmsw31u_xrc.lib wxpng.lib wxregexu.lib wxscintilla.lib wxtiff.lib wxzlib.lib //*****以下是特殊包含 comctl32.lib rpcrt4.lib
四、测试代码(来自官方helloworld示例)
新建一个main.cpp,内容如下,全部拷贝进去
// wxWidgets "Hello world" Program // For compilers that support precompilation, includes "wx/wx.h". #include <wx/wxprec.h> #include <wx/wx.h> 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); 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!"); }
五、制作模版,方便以后新建项目
新建2个类,一个叫MyWxApp,一个叫MyWxFrame,代码内容如下
MyWxApp.h
#ifndef MYWXAPP_H #define MYWXAPP_H #include <wx/app.h> class MyWxApp : public wxApp { public: virtual bool OnInit(); }; #endif // MYWXAPP_H
MyWxApp.cpp#include "MyWxApp.h" #include "MyWxFrame.h" #include <wx/image.h> IMPLEMENT_APP(MyWxApp); bool MyWxApp::OnInit() { bool wxsOK = true; wxInitAllImageHandlers(); if (wxsOK) { MyWxFrame* Frame = new MyWxFrame(0); Frame->Show(); SetTopWindow(Frame); } return wxsOK; }
MyWxFrame.h#ifndef MYWXFRAME_H #define MYWXFRAME_H #include <wx/artprov.h> #include <wx/xrc/xmlres.h> #include <wx/string.h> #include <wx/button.h> #include <wx/gdicmn.h> #include <wx/font.h> #include <wx/colour.h> #include <wx/settings.h> #include <wx/stattext.h> #include <wx/combobox.h> #include <wx/sizer.h> #include <wx/frame.h> #include <wx/msgdlg.h> class MyWxFrame : public wxFrame { private: protected: wxButton* m_button1; public: MyWxFrame(wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize(500, 300), long style = wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL); ~MyWxFrame(); void OnTips(wxCommandEvent& event); }; #endif // MYWXFRAME_H
MyWxFrame.cpp#include "MyWxFrame.h" MyWxFrame::MyWxFrame(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxFrame(parent, id, title, pos, size, style) { this->SetSizeHints(wxDefaultSize, wxDefaultSize); this->SetTitle(wxT("测试")); SetIcon(wxICON(app)); wxBoxSizer* bSizer2; bSizer2 = new wxBoxSizer(wxVERTICAL); m_button1 = new wxButton(this, wxID_ANY, wxT("提示"), wxDefaultPosition, wxDefaultSize, 0); bSizer2->Add(m_button1, 0, wxALL, 5); this->SetSizer(bSizer2); this->Layout(); this->Centre(wxBOTH); Connect(m_button1->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&MyWxFrame::OnTips); } MyWxFrame::~MyWxFrame() { } void MyWxFrame::OnTips(wxCommandEvent& event) { wxString msg = wxT("Hello World"); wxMessageBox(msg,wxT("提示"),MB_OK); }
新建一个资源文件MyWxFrame.rc#include "wx/msw/wx.rc" //mainIcon ICON "res/YmChat.ico"
(关于资源文件说明,新建完资源文件后,如果附带生成了resource.h,要删掉,没有用。另外MyWxFrame.rc中的内容要清空,把上面的2行代码复制进去,第2行的代码,把注释去掉,后面的ico换成你的图标路径即可,程序中可以使用SetIcon(wxIcon(mainIcon))来设置图标.)选择《文件》-》导出模版。即可。