2009年4月8日星期三

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

wxString

1. 这大概是一个很有用的类,是用来描述字符串。在下面的例子里,定义了3个wxStrings. 然后用它们相加生成了一个新的字符串。
#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);
}

Output:Linux Operating System

2. 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);
}


Output:There are 21 red roses.

3. 下面这个例子用来检查一个字符串是否包含另外一个字符串。这次我们使用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!"));
}


}


Output:
Contains!
Does not contain!

4. 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());
}

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 life

THE HISTORY OF MY LIFE

Time & date

1. 下面的例子用不用的格式显示当前的时间

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

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

Files

1. 下面的例子里,用wxFile创建一个文件并向其写入数据。另外测试此文件是否被打开。

int main(int argc, char **argv)
{

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

wxFile file;
file.Create(wxT(
"test"), 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"));
}

OutPut:
the file is opened
the file is not opened
test文件内:You make me want to be a better man.

2. 下面的例子将用wxTextFile输出文件的行数,第一行与最后一行的内容。最后,将显示文件的内容。

#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();
}

Output
Number of lines: 8
First line: #include
Last line: }
-------------------------------------
#include
#include

int main() {

g_mkdir("/home/vronskij/test", S_IRWXU);

}

没有评论: