wxWidgets 由一大堆帮助程序类组成,它们可以帮助程序员完成工作。 这些包括用于处理字符串,文件,XML 文件,流,数据库或网络的类。 在这里,我们将只显示整个湖面的一小滴。
wxWidgets 库可用于创建控制台和 GUI 应用。 在本章中,我们将说明基于控制台的应用中的一些帮助程序类。
控制台
这是一个简单的控制台应用。 该应用将一些文本放入控制台窗口。
console.cpp
#include <wx/string.h>int main(int argc, char **argv){wxPuts(wxT("A wxWidgets console application"));}
A wxWidgets console application
这是输出。
wxString
wxString是代表字符串的类。
在下面的示例中,我们定义了三个wxStrings。 我们使用加法运算创建一个字符串。
addition.cpp
#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()方法用于格式化字符串。
formatted.cpp
#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()方法。
contains.cpp
#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()方法返回字符串中的字符数。
length.cpp
#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()方法使字符小写和大写。
cases.cpp
#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 lifeTHE HISTORY OF MY LIFE
这是输出。
实用函数
wxWidgets 具有几个方便的工具函数,用于执行进程,获取主用户目录或获取 OS 名称。
在下面的示例中,我们执行ls命令。 为此,我们具有wxShell()函数(仅 Unix)。
shell.cpp
#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~
这是输出。
接下来,我们将获得主用户目录,操作系统名称,用户名,主机名和总可用内存。
system.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/vronskijLinux 2.6.20-16-generic i686jan bodnarspartanMemory: 741244928
这是输出。
时间日期
在 wxWidgets 中,我们有几个用于处理日期&时间的类。
该示例以各种格式显示当前日期或时间。
datetime.cpp
#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);}
Fri Sep 7 21:28:38 200721:28:3809/07/07
这是输出。
接下来,我们将显示不同城市的当前时间。
datetime2.cpp
#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:24Moscow: Sat 00:42:24Budapest: Fri 22:42:24London: Fri 22:42:24New York: Fri 16:42:24
这是输出。
以下示例显示了如何将日期范围添加到日期/时间。 我们将当前时间增加一个月。
datespan.cpp
#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);}
September 07 2007October 07 2007
这是输出。
文件
wxWidgets 有几个类可简化文件的处理。 与使用流相反,这是对文件的低级别访问。
在下面的示例中,我们使用wxFile类创建一个新文件并将数据写入其中。 我们还将测试文件是否打开。 请注意,当我们创建文件时,它会自动保持打开状态。
createfile.cpp
#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 qoutels: qoute: No such file or directory$ ./createfilethe file is openedthe file is not opened$ cat quoteYou make me want to be a better man.
这是输出。
wxTextFile是一个简单的类,允许逐行处理文本文件。 与wxFile类相比,使用此类更容易。
在下一个示例中,我们将打印文件中的行数,第一行和最后一行,最后将读取并显示文件的内容。
readfile.cpp
#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: 8First line: #include <glib.h>Last line: }-------------------------------------#include <glib.h>#include <glib/gstdio.h>int main() {g_mkdir("/home/vronskij/test", S_IRWXU);}
这是输出。
wxDir类允许我们枚举文件和目录。
在以下示例中,我们将打印当前工作目录中可用的所有文件和目录。
dir.cpp
#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);}}
$ ./dirdirtempconsolebasic.cppbasicquotecreatefileconsole.cppbasic.cpp~test.cconsole.cpp~
这是输出。
在本章中,我们介绍了一些 wxWidgets 帮助器类。
