找回密码
 立即注册
首页 业界区 业界 在Linux下使用wxWidgets进行跨平台GUI开发(二) ...

在Linux下使用wxWidgets进行跨平台GUI开发(二)

甦忻愉 2025-6-11 17:29:23
wxWidgets常见辅助类的应用示例

wxWidgets提供了一系列功能强大的辅助类(Helper Classes),涵盖了字符串处理、文件操作、XML解析、数据流、数据库和网络通信等功能,这些类为跨平台GUI开发提供了基础工具支持,帮助开发者完成各种任务。
wxWidgets库可用于创建控制台和图形界面(GUI)应用程序。本文将通过一些控制台应用程序的示例,阐述部分核心辅助类的使用方法。
Console示例

好,那现在让我们跟着ZetCode教程上的示例。第一个非常简单的控制台程序
一个非常简单的控制台程序:
  1. // console.cpp
  2. #include <wx/wx.h> // 必须得加上这个头文件
  3. #include <wx/string.h>
  4. int main(int argc, char **argv)
  5. {
  6.     wxPuts(wxT("A wxWidgets console application"));
  7. }
复制代码
对应的CMakeLists.txt文件:
  1. cmake_minimum_required(VERSION 3.10)
  2. project(console)
  3. find_package(wxWidgets REQUIRED COMPONENTS core)
  4. include(${wxWidgets_USE_FILE})
  5. add_executable(${PROJECT_NAME} console.cpp)
  6. target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})
  7. set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
复制代码
然后因为现在是VS Code开发,所以需要改一个调试配置文件launch.json,找到
  1. "program": "${workspaceFolder}/hello",
  2. 更改为
  3. "program": "${workspaceFolder}/console",
  4. 现在你应该能顺利构建编译运行,这将在控制台终端输出
  5. A wxWidgets console application
  6. 好的,现在我们将console.cpp和CMakeLists.txt文件复制到Windows平台,是的,我们的目的是要跨平台的,现在我们来做这件事。
  7. 在Windows上找到一个工作空间(注意路径字符规范),还是创建test文件夹,把要复制的两个文件拷贝到里面。
  8. 进入cmd命令行,还是将构建文件放到build下。
  9. cmake -B build
复制代码
然后生成可执行文件大概会出错,需修改CMakeLists.txt文件(确保跨平台):
  1. cmake_minimum_required(VERSION 3.10)
  2. project(console)
  3. if(WIN32)
  4.     find_package(wxWidgets 3.2.4 REQUIRED COMPONENTS core base adv html xml net aui stc xrc gl media qa richtext propgrid ribbon webview)
  5. else()
  6.     find_package(wxWidgets 3.2.4 REQUIRED COMPONENTS core base)
  7. endif()
  8. include(${wxWidgets_USE_FILE})
  9. add_executable(${PROJECT_NAME} console.cpp)
  10. target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})
  11. if(MSVC)
  12.     target_include_directories(${PROJECT_NAME} PUBLIC "${wxWidgets_INCLUDE_DIRS}/msvc")
  13. endif()
  14. set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})
复制代码
现在可以构建并生成可执行文件了,在cmd中键入
cmake --build ./build
这会根据默认构建系统在build文件夹里生成程序。如果你安装了Visual Studio则默认使用msvc工具链,你将会在build下看到一个sln解决方案,你也可以打开解决方案在IDE下生成可执行程序。
wxString类示例

wxString是一个表示字符串的类。在下面的示例中,我们定义了三个wxString对象。我们通过加法运算将这些字符串组合成一个新的字符串。
字符串连接
  1. // addition.cpp
  2. #include <wx/wx.h>
  3. #include <wx/string.h>
  4. int main(int argc, char **argv)
  5. {
  6.     wxString str1 = wxT("Linux");
  7.     wxString str2 = wxT("Operating");
  8.     wxString str3 = wxT("System");
  9.     wxString str = str1 + wxT(" ") + str2 + wxT(" ") + str3;
  10.     wxPuts(str);
  11. }
复制代码
输出:
  1. Linux Operating System
复制代码
你可以练习使用CMake生成这个可执行程序,当然你可以在Linux终端使用
  1. g++ addition.cpp `wx-config --cxxflags --libs` -o addition
复制代码
更简洁地生成可执行文件。
字符串格式化
  1. // formatted.cpp
  2. #include <wx/wx.h>
  3. #include <wx/string.h>
  4. int main(int argc, char **argv)
  5. {
  6.     int flowers = 21;
  7.     wxString str;
  8.     str.Printf(wxT("There are %d red roses."), flowers);
  9.     wxPuts(str);
  10. }
复制代码
输出:
  1. There are 21 red roses.
复制代码
以下示例演示了如何检查一个字符串是否包含另一个字符串,为此我们使用了Contains方法。(注:根据编程语言或框架的不同,类似功能可能有不同命名,如includes()、find()、contains()等,但作用相同。)
字符串包含检查
  1. // contains.cpp
  2. #include <wx/wx.h>
  3. #include <wx/string.h>
  4. int main(int argc, char **argv)
  5. {
  6.     wxString str = wxT("The history of my life");
  7.     if (str.Contains(wxT("history"))) {
  8.         wxPuts(wxT("Contains!"));
  9.     }
  10.     if (!str.Contains(wxT("plain"))) {
  11.         wxPuts(wxT("Does not contain!"));
  12.     }
  13. }
复制代码
输出:
  1. Contains!
  2. Does not contain!
复制代码
Len方法返回字符串中的字符数。(补充说明:对于多字节字符(如中文),不同语言处理方式可能不同,有些按字符数统计,有些按字节数统计)
字符串长度
  1. // length.cpp
  2. #include <wx/wx.h>
  3. #include <wx/string.h>
  4. int main(int argc, char **argv)
  5. {
  6.     wxString str = wxT("The history of my life");
  7.     wxPrintf(wxT("The string has %d characters\n"), str.Len());
  8. }
复制代码
输出:
  1. The string has 22 characters
复制代码
大小写转换
  1. // cases.cpp
  2. #include <wx/wx.h>
  3. #include <wx/string.h>
  4. int main(int argc, char **argv)
  5. {
  6.     wxString str = wxT("The history of my life");
  7.     wxPuts(str.MakeLower());
  8.     wxPuts(str.MakeUpper());
  9. }
复制代码
输出:
  1. the history of my life
  2. THE HISTORY OF MY LIFE
复制代码
实用工具函数

(编程概念说明:这类函数通常指不依赖对象状态、独立完成特定任务的辅助函数)
wxWidgets 提供了多个实用的工具函数,用于执行进程、获取用户主目录或获取操作系统名称。
在以下示例中,我们执行了 ls 命令(仅限 Unix 系统),为此我们使用了 wxShell 函数。
执行shell命令
  1. // shell.cpp
  2. #include <wx/wx.h>
  3. #include <wx/string.h>
  4. #include <wx/utils.h>
  5. int main(int argc, char **argv)
  6. {
  7.     wxShell(wxT("ls -l"));
  8. }
复制代码
这将输出当前路径下的文件统计信息。
接下来我们将获取用户主目录、操作系统名称、用户名、主机名以及内存空闲总量。
获取系统信息
  1. // system.cpp
  2. #include <wx/wx.h>
  3. #include <wx/string.h>
  4. #include <wx/utils.h>
  5. int main(int argc, char **argv)
  6. {
  7.     wxPuts(wxGetHomeDir());
  8.     wxPuts(wxGetOsDescription());
  9.     wxPuts(wxGetUserName());
  10.     wxPuts(wxGetFullHostName());
  11.     long mem = wxGetFreeMemory().ToLong();
  12.     wxPrintf(wxT("Memory: %ld\n"), mem);
  13. }
复制代码
这将输出系统及用户等统计信息。
时间和日期处理

在wxWidgets中,我们提供了多个用于处理日期和时间的类。
该示例展示了以不同格式显示的当前日期或时间。
当前日期时间格式化
  1. // datetime.cpp
  2. #include <wx/wx.h>
  3. #include <wx/datetime.h>
  4. int main(int argc, char **argv)
  5. {
  6.     wxDateTime now = wxDateTime::Now();
  7.     wxString date1 = now.Format();
  8.     wxString date2 = now.Format(wxT("%X"));
  9.     wxString date3 = now.Format(wxT("%x"));
  10.     wxPuts(date1);
  11.     wxPuts(date2);
  12.     wxPuts(date3);
  13. }
复制代码
输出示例:
  1. Wed Jun 11 16:18:09 2025
  2. 16:18:09
  3. 06/11/25
复制代码
不同城市时间
  1. // datetime2.cpp
  2. #include <wx/wx.h>
  3. #include <wx/datetime.h>
  4. int main(int argc, char **argv)
  5. {
  6.     wxDateTime now = wxDateTime::Now();
  7.     wxPrintf(wxT(" Tokyo: %s\n"), now.Format(wxT("%a %T"), wxDateTime::GMT9).c_str());
  8.     wxPrintf(wxT(" Moscow: %s\n"), now.Format(wxT("%a %T"), wxDateTime::MSD).c_str());
  9.     wxPrintf(wxT("Budapest: %s\n"), now.Format(wxT("%a %T"), wxDateTime::CEST).c_str());
  10.     wxPrintf(wxT(" London: %s\n"), now.Format(wxT("%a %T"), wxDateTime::WEST).c_str());
  11.     wxPrintf(wxT("New York: %s\n"), now.Format(wxT("%a %T"), wxDateTime::EDT).c_str());
  12. }
复制代码
输出示例:
  1. Tokyo: Wed 17:23:43
  2. Moscow: Wed 12:23:43
  3. Budapest: Wed 10:23:43
  4. London: Wed 09:23:43
  5. New York: Wed 04:23:43
复制代码
日期跨度

以下示例展示了如何向日期/时间添加日期跨度。我们将当前时间增加一个月。
  1. // datespan.cpp
  2. #include <wx/wx.h>
  3. #include <wx/datetime.h>
  4. int main(int argc, char **argv)
  5. {
  6.     wxDateTime now = wxDateTime::Now();
  7.     wxString date1 = now.Format(wxT("%B %d %Y"));
  8.     wxPuts(date1);
  9.     wxDateSpan span(0, 1);
  10.     wxDateTime then = now.Add(span);
  11.     wxString date2 = then.Format(wxT("%B %d %Y"));
  12.     wxPuts(date2);
  13. }
复制代码
输出示例:
  1. June 11 2025
  2. July 11 2025
复制代码
文件操作

wxWidgets 提供了多个类来简化文件操作。这是对文件的底层访问,与流式操作不同。
在以下示例中,我们使用 wxFile 类创建新文件并写入数据,同时会检测文件是否已打开。需注意:创建文件时会自动保持打开状态。
创建和写入文件
  1. // createfile.cpp
  2. #include <wx/wx.h>
  3. #include <wx/file.h>
  4. int main(int argc, char **argv)
  5. {
  6.     wxString str = wxT("You make me want to be a better man.\n");
  7.     wxFile file;
  8.     file.Create(wxT("quote"), true);
  9.     if (file.IsOpened())
  10.         wxPuts(wxT("the file is opened"));
  11.     file.Write(str);
  12.     file.Close();
  13.     if (!file.IsOpened())
  14.         wxPuts(wxT("the file is not opened"));
  15. }
复制代码
终端交互示例:
  1. $ ls quote
  2. ls: quote: No such file or directory
  3. $ ./createfile
  4. the file is opened
  5. the file is not opened
  6. $ cat quote
  7. You make me want to be a better man.
复制代码
wxTextFile 是一个简易的文本文件操作类,支持以行为单位处理文本文件。相比 wxFile 类,使用这个类会更加便捷。
在接下来的示例中,我们将输出文件的行数、首行和末行内容,最终读取并显示文件的全部内容。
文本文件操作
  1. // readfile.cpp
  2. #include <wx/wx.h>
  3. #include <wx/textfile.h>
  4. int main(int argc, char **argv)
  5. {
  6.     wxTextFile file(wxT("test.c"));
  7.     file.Open();
  8.     wxPrintf(wxT("Number of lines: %d\n"), file.GetLineCount());
  9.     wxPrintf(wxT("First line: %s\n"), file.GetFirstLine().c_str());
  10.     wxPrintf(wxT("Last line: %s\n"), file.GetLastLine().c_str());
  11.     wxPuts(wxT("----------------------------------------"));
  12.     wxString s;
  13.     for ( s = file.GetFirstLine(); !file.Eof();
  14.         s = file.GetNextLine() )
  15.     {
  16.         wxPuts(s);
  17.     }
  18.     file.Close();
  19. }
复制代码
test.c内容:
  1. #include <glib.h>
  2. #include <glib/gstdio.h>
  3. int main() {
  4.     g_mkdir("/home/vronskij/test", S_IRWXU);
  5. }
复制代码
输出:
  1. Number of lines: 8First line: #include Last line: }----------------------------------------#include <glib.h>
  2. #include <glib/gstdio.h>
  3. int main() {
  4.     g_mkdir("/home/vronskij/test", S_IRWXU);
  5. }
复制代码
目录枚举

在以下示例中,我们将打印当前工作目录下的所有文件和目录。
  1. // dir.cpp
  2. #include <wx/wx.h>
  3. #include <wx/dir.h>
  4. #include <wx/filefn.h>
  5. int main(int argc, char **argv)
  6. {
  7.     wxDir dir(wxGetCwd());
  8.     wxString file;
  9.     bool cont = dir.GetFirst(&file, wxEmptyString, wxDIR_FILES | wxDIR_DIRS);
  10.     while (cont) {
  11.         wxPuts(file);
  12.         cont = dir.GetNext(&file);
  13.     }
  14. }
复制代码
输出示例:
  1. dir
  2. dir.cpp
  3. temp
  4. console
  5. basic.cpp
  6. basic
  7. quote
  8. createfile
  9. console.cpp
  10. basic.cpp~
  11. test.c
  12. console.cpp~
  13. ......
复制代码
辅助类的应用示例暂且介绍到这里了,后续进行正式的wxWidgets开发说明。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册