[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的輔助類


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM