wxWidgets 辅助类
最后修改于 2023 年 10 月 18 日
wxWidgets 包含大量辅助类,帮助程序员完成工作。这些类包括用于处理字符串、文件、XML 文件、流、数据库或网络的类。在这里,我们只展示了整个湖泊中的一小部分。
wxWidgets 库可用于创建控制台和 GUI 应用程序。在本章中,我们将展示控制台应用程序中一些辅助类的例子。
控制台
这是一个简单的控制台应用程序。该应用程序将一些文本放入控制台窗口。
#include <wx/string.h>
int main(int argc, char **argv)
{
wxPuts(wxT("A wxWidgets console application"));
}
$ g++ console.cpp `wx-config --cxxflags --libs` -o console
我们编译程序。
A wxWidgets console application
这是输出。
wxString
wxString 是一个表示字符串的类。
在下面的例子中,我们定义了三个 wxString。我们使用加法运算创建一个字符串。
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str1 = wxT("Linux");
wxString str2 = wxT("Operating");
wxString str3 = wxT("System");
wxString str = str1 + wxT(" ") + str2 + wxT(" ") + str3;
wxPuts(str);
}
Linux Operating System
这是输出。
Printf 方法用于格式化字符串。
#include <wx/string.h>
int main(int argc, char **argv)
{
int flowers = 21;
wxString str;
str.Printf(wxT("There are %d red roses."), flowers);
wxPuts(str);
}
There are 21 red roses.
这是输出。
下面的例子检查一个字符串是否包含另一个字符串。为此,我们有一个 Contains 方法。
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str = wxT("The history of my life");
if (str.Contains(wxT("history"))) {
wxPuts(wxT("Contains!"));
}
if (!str.Contains(wxT("plain"))) {
wxPuts(wxT("Does not contain!"));
}
}
Contains! Does not contain!
这是输出。
Len 方法返回字符串中的字符数。
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str = wxT("The history of my life");
wxPrintf(wxT("The string has %d characters\n"), str.Len());
}
The string has 22 characters
这是输出。
MakeLower 和 MakeUpper 方法将字符转换为小写和大写。
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str = wxT("The history of my life");
wxPuts(str.MakeLower());
wxPuts(str.MakeUpper());
}
the history of my life THE HISTORY OF MY LIFE
这是输出。
实用程序函数
wxWidgets 有几个方便的实用程序函数,用于执行进程、获取用户主目录或获取操作系统名称。
在下面的例子中,我们执行 ls 命令。为此,我们有 wxShell 函数(仅限 Unix)。
#include <wx/string.h>
#include <wx/utils.h>
int main(int argc, char **argv)
{
wxShell(wxT("ls -l"));
}
total 40 -rwxr-xr-x 1 vronskij vronskij 9028 2007-09-06 22:10 basic -rw-r--r-- 1 vronskij vronskij 95 2007-09-06 22:09 basic.cpp -rw-r--r-- 1 vronskij vronskij 430 2007-09-06 00:07 basic.cpp~ -rwxr-xr-x 1 vronskij vronskij 11080 2007-09-05 23:17 console -rw-r--r-- 1 vronskij vronskij 500 2007-09-05 23:17 console.cpp -rw-r--r-- 1 vronskij vronskij 485 2007-09-05 23:16 console.cpp~
这是输出。
接下来,我们获取用户主目录、操作系统名称、用户名、主机名和总可用内存。
#include <wx/string.h>
#include <wx/utils.h>
int main(int argc, char **argv)
{
wxPuts(wxGetHomeDir());
wxPuts(wxGetOsDescription());
wxPuts(wxGetUserName());
wxPuts(wxGetFullHostName());
long mem = wxGetFreeMemory().ToLong();
wxPrintf(wxT("Memory: %ld\n"), mem);
}
/home/vronskij Linux 2.6.20-16-generic i686 jan bodnar spartan Memory: 741244928
这是输出。
时间和日期
在 wxWidgets 中,我们有几个用于处理日期和时间的类。
该示例显示各种格式的当前日期或时间。
#include <wx/datetime.h>
int main(int argc, char **argv)
{
wxDateTime now = wxDateTime::Now();
wxString date1 = now.Format();
wxString date2 = now.Format(wxT("%X"));
wxString date3 = now.Format(wxT("%x"));
wxPuts(date1);
wxPuts(date2);
wxPuts(date3);
}
Thu Feb 4 11:05:33 2021 11:05:33 02/04/21
这是输出。
接下来,我们显示不同城市中的当前时间。
#include <wx/datetime.h>
int main(int argc, char **argv)
{
wxDateTime now = wxDateTime::Now();
wxPrintf(wxT(" Tokyo: %s\n"), now.Format(wxT("%a %T"),
wxDateTime::GMT9).c_str());
wxPrintf(wxT(" Moscow: %s\n"), now.Format(wxT("%a %T"),
wxDateTime::MSD).c_str());
wxPrintf(wxT("Budapest: %s\n"), now.Format(wxT("%a %T"),
wxDateTime::CEST).c_str());
wxPrintf(wxT(" London: %s\n"), now.Format(wxT("%a %T"),
wxDateTime::WEST).c_str());
wxPrintf(wxT("New York: %s\n"), now.Format(wxT("%a %T"),
wxDateTime::EDT).c_str());
}
Tokyo: Sat 05:42:24 Moscow: Sat 00:42:24 Budapest: Fri 22:42:24 London: Fri 22:42:24 New York: Fri 16:42:24
这是输出。
下面的例子展示了我们如何将日期范围添加到我们的日期/时间中。我们将一个月添加到当前时间。
#include <wx/datetime.h>
int main(int argc, char **argv)
{
wxDateTime now = wxDateTime::Now();
wxString date1 = now.Format(wxT("%B %d %Y"));
wxPuts(date1);
wxDateSpan span(0, 1);
wxDateTime then = now.Add(span);
wxString date2 = then.Format(wxT("%B %d %Y"));
wxPuts(date2);
}
February 04 2021 March 04 2021
这是输出。
文件
wxWidgets 有几个类来方便处理文件。与使用流相比,这是对文件的低级访问。
在下面的例子中,我们使用 wxFile 类创建一个新文件并将数据写入其中。我们还测试文件是否已打开。请注意,当我们创建一个文件时,它会自动保持打开状态。
#include <wx/file.h>
int main(int argc, char **argv)
{
wxString str = wxT("You make me want to be a better man.\n");
wxFile file;
file.Create(wxT("quote"), true);
if (file.IsOpened())
wxPuts(wxT("the file is opened"));
file.Write(str);
file.Close();
if (!file.IsOpened())
wxPuts(wxT("the file is not opened"));
}
$ ls qoute ls: qoute: No such file or directory $ ./createfile the file is opened the file is not opened $ cat quote You make me want to be a better man.
这是输出。
wxTextFile 是一个简单的类,它允许逐行处理文本文件。使用此类比使用 wxFile 类更容易。
在下一个例子中,我们打印文件中的行数、第一行和最后一行,最后我们读取并显示文件的内容。
#include <wx/textfile.h>
int main(int argc, char **argv)
{
wxTextFile file(wxT("test.c"));
file.Open();
wxPrintf(wxT("Number of lines: %d\n"), file.GetLineCount());
wxPrintf(wxT("First line: %s\n"), file.GetFirstLine().c_str());
wxPrintf(wxT("Last line: %s\n"), file.GetLastLine().c_str());
wxPuts(wxT("-------------------------------------"));
wxString s;
for ( s = file.GetFirstLine(); !file.Eof();
s = file.GetNextLine() )
{
wxPuts(s);
}
file.Close();
}
Number of lines: 8
First line: #include <glib.h>
Last line: }
-------------------------------------
#include <glib.h>
#include <glib/gstdio.h>
int main() {
g_mkdir("/home/vronskij/test", S_IRWXU);
}
这是输出。
wxDir 类允许我们枚举文件和目录。
在下面的例子中,我们打印当前工作目录中所有可用的文件和目录。
#include <wx/dir.h>
#include <wx/filefn.h>
int main(int argc, char **argv)
{
wxDir dir(wxGetCwd());
wxString file;
bool cont = dir.GetFirst(&file, wxEmptyString,
wxDIR_FILES | wxDIR_DIRS);
while (cont) {
wxPuts(file);
cont = dir.GetNext(&file);
}
}
$ ./dir dir temp console basic.cpp basic quote createfile console.cpp basic.cpp~ test.c console.cpp~
这是输出。
在本章中,我们介绍了一些 wxWidgets 辅助类。