[ZETCODE]wxWidgets教程二:辅助类

本教程原文链接:http://zetcode.com/gui/wxwidgets/helperclasses/

翻译:瓶哥

日期:2013年11月27日星期三

邮箱:414236069@qq.com

主页:http://www.cnblogs.com/pingge/

若有翻译错误或者歧义请联系我!

 

wxWidgets包含着一组丰富的辅助类,这有助于程序员完成他们的工作。这些辅助类与字符串、文件系统、XML文件、数据流、数据库、网络一同工作。在这里我们将只展示其中的小小的一部分。

wxWidgets能创建控制台或者GUI程序。在这一章,我们将会举例说明一些基于控制台程序的辅助类。

Console

这是一个简单的控制台应用程序,这个程序在控制台窗口上显示一些 文字。

// console.cpp

#include <wx/string.h>

int main(int argc, char ** argv)

{

wxPuts(wxT("A wxWidgets console application"));

wxPuts(_T("A wxWidgets console application"));

return 0;

}
View Code

 

wxString

wxString是一个字符串类。

在下一个例子中,我们定义了三个wxStrings,我们使用加法运算符通过这几个字符串创建了另一个字符串。

// addition.cpp

#include <wx/string.h>

int main(int argc, char ** argv)

{

wxString str1 = _T("Linux");

wxString str2 = _T("Operating");

wxString str3 = _T("System");

wxString str = str1 + _T(" ") + str2 + _T(" ") + str3;

wxPuts(str);

return 0;

}
View Code

 

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

// formatted.cpp

#include <wx/string.h>

int main(int argc, char ** argv)

{

int flowers = 21;

wxString str;

str.Printf(_T("There are %d red roses."), flowers);

wxPuts(str);

return 0;

}
View Code

 

接下来的这个例子,检查了一个字符串是否包含另一个字符串(子串),这里我们有一个Contains()方法。

// contains.cpp

#include <wx/string.h>

int main(int argc, char ** argv)

{

wxString str = _T("The history of my life");

if(str.Contains(_T("history")))

{

wxPuts(_T("Contains!"));

}

if(!str.Contains(_T("plain")))

{

wxPuts(_T("Does not contain!"));

}

return 0;

}
View Code

 

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

// length.cpp

#include <wx/string.h>

int main(int argc, char ** argv)

{

wxString str = _T("The history of my life");

wxPrintf(_T("The string has %d characters\n"), str.Len());

return 0;

}
View Code

 

方法MakeLower()和MakeUpper()使得字符小写或者大写

// cases.cpp

#include <wx/string.h>

int main(int argc, char ** argv)

{

wxString str = _T("The history of my life");

wxPuts(str.MakeLower());

wxPuts(str.MakeUpper());

return 0;

}
View Code

 

公用函数

wxWidgets有几个方便的公用函数,用于执行一些命令,例如获得用户主文件夹目录或者获得操作系统名称。

在接下来的几个例子中,我们执行ls命名,这里我们有wxShell()函数。

// shell.cpp

#include <wx/string.h>

#include <wx/utils.h>

 

int main(int argc, char ** argv)

{

wxShell(_T("dir")); // Windows

// wxShell(_T("ls -l")); // Unix

 

return 0;

}
View Code


 

 

接下来我们将获得用户的主文件夹目录、操作系统名、用户名、主机名、全部的空闲内存。

// system.cpp

#include <wx/string.h>

#include <wx/utils.h>

int main(int argc, char ** argv)

{

wxPuts(wxGetHomeDir());wxPuts(_T(""));

wxPuts(wxGetOsDescription());

// wxPuts(wxGetUserName()); // Unix

wxPuts(wxGetFullHostName());

long mem = wxGetFreeMemory().ToLong();

wxPrintf(_T("Memory : %ld\n"), mem);

return 0;

}
View Code

 

 

时间和日期

在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(_T("%X"));

wxString date3 = now.Format(_T("%x"));

wxPuts(date1);

wxPuts(date2);

wxPuts(date3);

return 0;

}
View Code

 

 

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

// datetime2.cpp

#include <wx/datetime.h>

int main(int argc, char ** argv)

{

wxDateTime now = wxDateTime::Now();

wxPrintf(_T(" Tokyo: %s\n"), now.Format(_T("%X"), wxDateTime::GMT9).c_str());

wxPrintf(_T(" Moscow: %s\n"), now.Format(_T("%X"), wxDateTime::MSD).c_str());

wxPrintf(_T("Budapest: %s\n"), now.Format(_T("%X"), wxDateTime::CEST).c_str());

wxPrintf(_T(" London: %s\n"), now.Format(_T("%X"), wxDateTime::WEST).c_str());

wxPrintf(_T("New York: %s\n"), now.Format(_T("%X"), wxDateTime::EDT).c_str());

return 0;

}
View Code

 

 

文件操作

wxWidgets有几个类辅助文件操作,这是对于文件的底层访问而不是处理文件流。

在接下来的这个例子中,我们使用wxFile类来新建一个文件并且写入一些数据,我们也测试这个文件是否是打开状态。注意:当我们新建一个文件的时候,这个文件会自动的处于打开状态。

// createfile.cpp

#include <wx/file.h>

int main(int argc, char ** argv)

{

wxString str = _T("You make me want to be a better man.\n");

wxFile file;

file.Create(_T("quote"), true);

if(file.IsOpened())

{

wxPuts(_T("The file is opened"));

}

file.Write(str);

file.Close();

if(!file.IsOpened())

{

wxPuts(_T("The file is not opened"));

}

return 0;

}
View Code

 

wxTextFile是一个允许你逐行处理文件的简单的类,这个类比wxFile类更容易使用。

在接下来这个例子中,我们将输出文件的行数、第一行、最后一行内容,最后我们将读取文件的全部内容并输出。

// readfile.cpp

#include <wx/textfile.h>

 

int main(int argc, char ** argv)

{

wxTextFile file(_T("readfile.cpp"));

file.Open();

 

wxPrintf(_T("Number of lines: %d\n"), file.GetLineCount());

wxPrintf(_T("First line: %s\n"), file.GetFirstLine().c_str());

wxPrintf(_T("Lase line: %s\n"), file.GetLastLine().c_str());

 

wxPuts(_T("-------------------------------------------"));

 

wxString s;

for(s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine())

{

wxPuts(s);

}

file.Close();

 

return 0;

}
View Code

 

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);

}

 

return 0;

}
View Code

 

在本章中我们介绍了一些wxWidgets的辅助类

posted @ 2013-11-27 13:55  瓶哥  Views(3549)  Comments(1Edit  收藏  举报