原文: http://zetcode.com/gui/wxwidgets/helperclasses/

wxWidgets 由一大堆帮助程序类组成,它们可以帮助程序员完成工作。 这些包括用于处理字符串,文件,XML 文件,流,数据库或网络的类。 在这里,我们将只显示整个湖面的一小滴。

wxWidgets 库可用于创建控制台和 GUI 应用。 在本章中,我们将说明基于控制台的应用中的一些帮助程序类。

控制台

这是一个简单的控制台应用。 该应用将一些文本放入控制台窗口。

console.cpp

  1. #include <wx/string.h>
  2. int main(int argc, char **argv)
  3. {
  4. wxPuts(wxT("A wxWidgets console application"));
  5. }
  1. A wxWidgets console application

这是输出。

wxString

wxString是代表字符串的类。

在下面的示例中,我们定义了三个wxStrings。 我们使用加法运算创建一个字符串。

addition.cpp

  1. #include <wx/string.h>
  2. int main(int argc, char **argv)
  3. {
  4. wxString str1 = wxT("Linux");
  5. wxString str2 = wxT("Operating");
  6. wxString str3 = wxT("System");
  7. wxString str = str1 + wxT(" ") + str2 + wxT(" ") + str3;
  8. wxPuts(str);
  9. }
  1. Linux Operating System

这是输出。

Printf()方法用于格式化字符串。

formatted.cpp

  1. #include <wx/string.h>
  2. int main(int argc, char **argv)
  3. {
  4. int flowers = 21;
  5. wxString str;
  6. str.Printf(wxT("There are %d red roses."), flowers);
  7. wxPuts(str);
  8. }
  1. There are 21 red roses.

这是输出。

下面的示例检查一个字符串是否包含另一个字符串。 为此,我们有一个Contains()方法。

contains.cpp

  1. #include <wx/string.h>
  2. int main(int argc, char **argv)
  3. {
  4. wxString str = wxT("The history of my life");
  5. if (str.Contains(wxT("history"))) {
  6. wxPuts(wxT("Contains!"));
  7. }
  8. if (!str.Contains(wxT("plain"))) {
  9. wxPuts(wxT("Does not contain!"));
  10. }
  11. }
  1. Contains!
  2. Does not contain!

这是输出。

Len()方法返回字符串中的字符数。

length.cpp

  1. #include <wx/string.h>
  2. int main(int argc, char **argv)
  3. {
  4. wxString str = wxT("The history of my life");
  5. wxPrintf(wxT("The string has %d characters\n"), str.Len());
  6. }
  1. The string has 22 characters

这是输出。

MakeLower()MakeUpper()方法使字符小写和大写。

cases.cpp

  1. #include <wx/string.h>
  2. int main(int argc, char **argv)
  3. {
  4. wxString str = wxT("The history of my life");
  5. wxPuts(str.MakeLower());
  6. wxPuts(str.MakeUpper());
  7. }
  1. the history of my life
  2. THE HISTORY OF MY LIFE

这是输出。

实用函数

wxWidgets 具有几个方便的工具函数,用于执行进程,获取主用户目录或获取 OS 名称。

在下面的示例中,我们执行ls命令。 为此,我们具有wxShell()函数(仅 Unix)。

shell.cpp

  1. #include <wx/string.h>
  2. #include <wx/utils.h>
  3. int main(int argc, char **argv)
  4. {
  5. wxShell(wxT("ls -l"));
  6. }
  1. total 40
  2. -rwxr-xr-x 1 vronskij vronskij 9028 2007-09-06 22:10 basic
  3. -rw-r--r-- 1 vronskij vronskij 95 2007-09-06 22:09 basic.cpp
  4. -rw-r--r-- 1 vronskij vronskij 430 2007-09-06 00:07 basic.cpp~
  5. -rwxr-xr-x 1 vronskij vronskij 11080 2007-09-05 23:17 console
  6. -rw-r--r-- 1 vronskij vronskij 500 2007-09-05 23:17 console.cpp
  7. -rw-r--r-- 1 vronskij vronskij 485 2007-09-05 23:16 console.cpp~

这是输出。

接下来,我们将获得主用户目录,操作系统名称,用户名,主机名和总可用内存。

system.cpp

  1. #include <wx/string.h>
  2. #include <wx/utils.h>
  3. int main(int argc, char **argv)
  4. {
  5. wxPuts(wxGetHomeDir());
  6. wxPuts(wxGetOsDescription());
  7. wxPuts(wxGetUserName());
  8. wxPuts(wxGetFullHostName());
  9. long mem = wxGetFreeMemory().ToLong();
  10. wxPrintf(wxT("Memory: %ld\n"), mem);
  11. }
  1. /home/vronskij
  2. Linux 2.6.20-16-generic i686
  3. jan bodnar
  4. spartan
  5. Memory: 741244928

这是输出。

时间日期

在 wxWidgets 中,我们有几个用于处理日期&时间的类。

该示例以各种格式显示当前日期或时间。

datetime.cpp

  1. #include <wx/datetime.h>
  2. int main(int argc, char **argv)
  3. {
  4. wxDateTime now = wxDateTime::Now();
  5. wxString date1 = now.Format();
  6. wxString date2 = now.Format(wxT("%X"));
  7. wxString date3 = now.Format(wxT("%x"));
  8. wxPuts(date1);
  9. wxPuts(date2);
  10. wxPuts(date3);
  11. }
  1. Fri Sep 7 21:28:38 2007
  2. 21:28:38
  3. 09/07/07

这是输出。

接下来,我们将显示不同城市的当前时间。

datetime2.cpp

  1. #include <wx/datetime.h>
  2. int main(int argc, char **argv)
  3. {
  4. wxDateTime now = wxDateTime::Now();
  5. wxPrintf(wxT(" Tokyo: %s\n"), now.Format(wxT("%a %T"),
  6. wxDateTime::GMT9).c_str());
  7. wxPrintf(wxT(" Moscow: %s\n"), now.Format(wxT("%a %T"),
  8. wxDateTime::MSD).c_str());
  9. wxPrintf(wxT("Budapest: %s\n"), now.Format(wxT("%a %T"),
  10. wxDateTime::CEST).c_str());
  11. wxPrintf(wxT(" London: %s\n"), now.Format(wxT("%a %T"),
  12. wxDateTime::WEST).c_str());
  13. wxPrintf(wxT("New York: %s\n"), now.Format(wxT("%a %T"),
  14. wxDateTime::EDT).c_str());
  15. }
  1. Tokyo: Sat 05:42:24
  2. Moscow: Sat 00:42:24
  3. Budapest: Fri 22:42:24
  4. London: Fri 22:42:24
  5. New York: Fri 16:42:24

这是输出。

以下示例显示了如何将日期范围添加到日期/时间。 我们将当前时间增加一个月。

datespan.cpp

  1. #include <wx/datetime.h>
  2. int main(int argc, char **argv)
  3. {
  4. wxDateTime now = wxDateTime::Now();
  5. wxString date1 = now.Format(wxT("%B %d %Y"));
  6. wxPuts(date1);
  7. wxDateSpan span(0, 1);
  8. wxDateTime then = now.Add(span);
  9. wxString date2 = then.Format(wxT("%B %d %Y"));
  10. wxPuts(date2);
  11. }
  1. September 07 2007
  2. October 07 2007

这是输出。

文件

wxWidgets 有几个类可简化文件的处理。 与使用流相反,这是对文件的低级别访问。

在下面的示例中,我们使用wxFile类创建一个新文件并将数据写入其中。 我们还将测试文件是否打开。 请注意,当我们创建文件时,它会自动保持打开状态。

createfile.cpp

  1. #include <wx/file.h>
  2. int main(int argc, char **argv)
  3. {
  4. wxString str = wxT("You make me want to be a better man.\n");
  5. wxFile file;
  6. file.Create(wxT("quote"), true);
  7. if (file.IsOpened())
  8. wxPuts(wxT("the file is opened"));
  9. file.Write(str);
  10. file.Close();
  11. if (!file.IsOpened())
  12. wxPuts(wxT("the file is not opened"));
  13. }
  1. $ ls qoute
  2. ls: qoute: 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类相比,使用此类更容易。

在下一个示例中,我们将打印文件中的行数,第一行和最后一行,最后将读取并显示文件的内容。

readfile.cpp

  1. #include <wx/textfile.h>
  2. int main(int argc, char **argv)
  3. {
  4. wxTextFile file(wxT("test.c"));
  5. file.Open();
  6. wxPrintf(wxT("Number of lines: %d\n"), file.GetLineCount());
  7. wxPrintf(wxT("First line: %s\n"), file.GetFirstLine().c_str());
  8. wxPrintf(wxT("Last line: %s\n"), file.GetLastLine().c_str());
  9. wxPuts(wxT("-------------------------------------"));
  10. wxString s;
  11. for ( s = file.GetFirstLine(); !file.Eof();
  12. s = file.GetNextLine() )
  13. {
  14. wxPuts(s);
  15. }
  16. file.Close();
  17. }
  1. Number of lines: 8
  2. First line: #include <glib.h>
  3. Last line: }
  4. -------------------------------------
  5. #include <glib.h>
  6. #include <glib/gstdio.h>
  7. int main() {
  8. g_mkdir("/home/vronskij/test", S_IRWXU);
  9. }

这是输出。

wxDir类允许我们枚举文件和目录。

在以下示例中,我们将打印当前工作目录中可用的所有文件和目录。

dir.cpp

  1. #include <wx/dir.h>
  2. #include <wx/filefn.h>
  3. int main(int argc, char **argv)
  4. {
  5. wxDir dir(wxGetCwd());
  6. wxString file;
  7. bool cont = dir.GetFirst(&file, wxEmptyString,
  8. wxDIR_FILES | wxDIR_DIRS);
  9. while (cont) {
  10. wxPuts(file);
  11. cont = dir.GetNext(&file);
  12. }
  13. }
  1. $ ./dir
  2. dir
  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~

这是输出。

在本章中,我们介绍了一些 wxWidgets 帮助器类。