# 下載 https://www.wxwidgets.org/downloads/ 下載壓縮包即可 ![image](https://img2023.cnblogs.com/blog/916065/202306/916065-20230614040303993-2082032985.png) # 編 ...
下載
https://www.wxwidgets.org/downloads/
下載壓縮包即可
編譯
打開 build\msw
目錄下的 sln
文件
vs發佈版本與vc版本對應關係: vs發佈版本與vc版本對應關係
vs發佈包版本 | vc版本 |
---|---|
Visual Studio 2003 | VC7 |
Visual Studio 2005 | VC8 |
Visual Studio 2008 | VC9 |
Visual Studio 2010 | VC10 |
Visual Studio 2012 | VC11 |
Visual Studio 2013 | VC12 |
Visual Studio 2015 | VC14 |
Visual Studio 2017 | VC15 |
Visual Studio 2019 | VC16 |
Visual Studio 2022 | VC17 |
生成->批生成
全選->重新生成
大約需要 30 分鐘
新建 hello world 前的準備
新建一個目錄, 將 include 目錄和lib目錄複製過去
配置環境變數 wx_win
, 值為include
與lib
所在目錄: E:\program\wx_wdigets_3.2.2.1
hello world
editor config
[*]
guidelines = 120
[*.{cpp,c,h}]
charset = charset = utf-16le
indent_style = space
indent_size = 4
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
tab_width = 4
代碼
// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#define WXUSINGDLL
// #define __WXMSW__
// #define _UNICODE
#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, "第一個視窗")
{
wxMenu* menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&你好...\tCtrl-H",
"此菜單的提示文本顯示在狀態欄");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu* menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar* menuBar = new wxMenuBar;
menuBar->Append(menuFile, "文件(&F)");
menuBar->Append(menuHelp, "幫助(&H)");
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!");
}
項目屬性設置
修改輸出目錄與中間目錄
所有配置, 所有平臺
$(SolutionDir)$(ProjectName)\$(Platform)\$(Configuration)\
添加頭文件目錄
所有配置, 所有平臺
$(wx_win)\include\msvc
$(wx_win)\include
添加庫目錄
註意, win32與x64 不同
win32
$(wx_win)\lib\vc_dll
x64
$(wx_win)\lib\vc_x64_dll
修改子系統
dll 文件的複製
因為使用的時動態庫形式, 所以運行時需要動態庫文件, 添加生成事件
所有配置, 所有平臺
用於 MSBuild 命令和屬性的常用巨集
python $(SolutionDir)$(ProjectName)\copylib.py $(Platform) $(Configuration) $(SolutionDir)$(ProjectName)
python 腳本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
from shutil import copyfile
wx_env_name='wx_win'
dll_dict={
# 'wxbase322u':'net',
'wxbase322u':'',
# 'wxbase322u':'xml',
# 'wxmsw322u':'adv',
# 'wxmsw322u':'aui',
'wxmsw322u':'core',
# 'wxmsw322u':'gl',
# 'wxmsw322u':'html',
# 'wxmsw322u':'media',
# 'wxmsw322u':'propgrid',
# 'wxmsw322u':'qa',
# 'wxmsw322u':'ribbon',
# 'wxmsw322u':'richtext',
# 'wxmsw322u':'stc',
# 'wxmsw322u':'webview',
# 'wxmsw322u':'xrc'
}
if __name__ == "__main__":
platform = sys.argv[1]
configuration = sys.argv[2]
proj_dir = sys.argv[3]
wx_path = os.getenv(wx_env_name)
# print('wx_path:',wx_path)
# print('platform:', platform)
# print('configuration:',configuration)
# print('proj_dir:',proj_dir)
for key,value in dll_dict.items():
dll_name = key
if 'Debug' == configuration:
dll_name = dll_name + 'd'
if 0 == len(value):
dll_name = dll_name + '_vc_'
else:
dll_name = dll_name + '_' + value + '_vc_'
if 'x64' == platform:
dll_name = dll_name + 'x64_'
dll_name = dll_name + 'custom.dll'
# print('dll_name:',dll_name)
source_dll = wx_path + os.path.sep + 'lib'
if 'x64' == platform:
source_dll = source_dll + os.path.sep + 'vc_x64_dll' + os.path.sep + dll_name
else:
source_dll = source_dll + os.path.sep + 'vc_dll' + os.path.sep + dll_name
# print('source_dll',source_dll)
target_dll = proj_dir + os.path.sep + platform + os.path.sep + configuration + os.path.sep + dll_name
# print('target_dll',target_dll)
print(source_dll + ' => ' + target_dll)
if not os.path.exists(target_dll):
copyfile(source_dll, target_dll)
測試效果
- 刪除與解決方案
.sln
同級的構建目錄, 比如x64
等, 保持目錄清晰乾凈
分別運行 Debug x86
, Debug x64
, Release x86
, Release x64
驗證效果
項目模板
將 .editorconfig
與copylib.py
添加到項目
添加->現有項
註意:
- 生成的模板文件位於:
C:\Users\用戶名\Documents\Visual Studio 2022\My Exported Templates
- 實際使用的模板文件在:
C:\Users\用戶名\Documents\Visual Studio 2022\Templates\ProjectTemplates
- 可能編輯模板之後不生效, 需要刪除緩存:
C:\Users\用戶名\AppData\Local\Microsoft\VisualStudio\vs版本號\ComponentModelCache\Microsoft.VisualStudio.Default.cache
效果
本文來自博客園,作者:laolang2016,轉載請註明原文鏈接:https://www.cnblogs.com/khlbat/p/17479111.html