wxWidgets helper classes
- wxWidgets库可以创建控制台( console )和界面( gui )程序,下面将在控制台模式下介绍一些helper class。
Console
这是一个简单得控制台程序,在控制台窗口里输出一些信息。
#include <wx/string.h>
int main(int argc, char **argv)
{
wxPuts(wxT("A wxWidgets console application"));
}
Output:A wxWidgets console application
#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);
}
wxString
1. 这大概是一个很有用的类,是用来描述字符串。在下面的例子里,定义了3个wxStrings. 然后用它们相加生成了一个新的字符串。












Output:Linux Operating System
2. Printf() 方法用来格式化字符串
2. Printf() 方法用来格式化字符串













Output:There are 21 red roses.
3. 下面这个例子用来检查一个字符串是否包含另外一个字符串。这次我们使用Contains()方法
3. 下面这个例子用来检查一个字符串是否包含另外一个字符串。这次我们使用Contains()方法



















Output:
Contains!
Does not contain!
4. Len()用来返回一个字符串的长度
Does not contain!
4. Len()用来返回一个字符串的长度








Output:The string has 22 characters.
5. 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());
}
Output:the history of my lifeTHE HISTORY OF MY LIFE
Time & date
1. 下面的例子用不用的格式显示当前的时间















Output Fri Sep 7 21:28:38 2007 21:28:38 09/07/07
2. 下面显示不同国家(时区)的当前时间
#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());
}
Output
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
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
Files
1. 下面的例子里,用wxFile创建一个文件并向其写入数据。另外测试此文件是否被打开。


















OutPut:
the file is opened
the file is not opened
test文件内:You make me want to be a better man.
2. 下面的例子将用wxTextFile输出文件的行数,第一行与最后一行的内容。最后,将显示文件的内容。


























Output
Number of lines: 8
First line: #include
Last line: }
-------------------------------------
#include <glib.h>
#include <glib/gstdio.h>
int main() {
g_mkdir("/home/vronskij/test", S_IRWXU);
}