1,下載wxwidgets包并安裝,
https://www.wxwidgets.org/downloads/
這里下載的是3.1.1
2, 編譯
安裝目錄
安裝目錄
C:\wxWidgets\build\msw\wx.dsw
用VS2017打開wx.dsw,類似如圖所示
VS2017
右擊解決方案,點擊批生成,勾選所有Debug和Release(可根據(jù)需要選擇),點擊生成進行編譯。
編譯
此時,wxWidgets-3.0.0\lib\vc_lib目錄如下所示,已生成所需庫文件,其中u表示Release版本,ud表示Debug版本。
vc_lib
3, 建立一個簡單的窗口
創(chuàng)建一個空項目
空項目
新建
點擊新建項會出現(xiàn)下圖:
image.png
之后出現(xiàn):
image.png
配置(重點):
image.png
1、出現(xiàn)下圖,添加頭文件:C/C++→常規(guī)→附加包含目錄(添加頭文件在硬盤中的位置)
image.png
image.png
3、引用庫文件:鏈接器→輸入→附加依賴項→添加lib文件
image.png
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
comctl32.lib // 以下4個千萬不要忘記添加,否則會報 鏈接錯誤 錯,
rpcrt4.lib
wxzlibd.lib
odbc32.lib
4、鏈接器→系統(tǒng)→子系統(tǒng)→窗口image.png
5 預(yù)處理器定義
配置一項改為Debug
配置屬性->C/C++->預(yù)處理器->預(yù)處理器定義中添加:
__WXMSW__
__WXDEBUG__
Hello world 代碼:
#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!");
}
運行結(jié)果:
image.png