<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # wxWidgets 助手類 > 原文: [http://zetcode.com/gui/wxwidgets/helperclasses/](http://zetcode.com/gui/wxwidgets/helperclasses/) wxWidgets 由一大堆幫助程序類組成,它們可以幫助程序員完成工作。 這些包括用于處理字符串,文件,XML 文件,流,數據庫或網絡的類。 在這里,我們將只顯示整個湖面的一小滴。 wxWidgets 庫可用于創建控制臺和 GUI 應用。 在本章中,我們將說明基于控制臺的應用中的一些幫助程序類。 ## 控制臺 這是一個簡單的控制臺應用。 該應用將一些文本放入控制臺窗口。 `console.cpp` ```cpp #include <wx/string.h> int main(int argc, char **argv) { wxPuts(wxT("A wxWidgets console application")); } ``` ```cpp A wxWidgets console application ``` 這是輸出。 ## `wxString` `wxString`是代表字符串的類。 在下面的示例中,我們定義了三個`wxStrings`。 我們使用加法運算創建一個字符串。 `addition.cpp` ```cpp #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); } ``` ```cpp Linux Operating System ``` 這是輸出。 `Printf()`方法用于格式化字符串。 `formatted.cpp` ```cpp #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); } ``` ```cpp There are 21 red roses. ``` 這是輸出。 下面的示例檢查一個字符串是否包含另一個字符串。 為此,我們有一個`Contains()`方法。 `contains.cpp` ```cpp #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!")); } } ``` ```cpp Contains! Does not contain! ``` 這是輸出。 `Len()`方法返回字符串中的字符數。 `length.cpp` ```cpp #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()); } ``` ```cpp The string has 22 characters ``` 這是輸出。 `MakeLower()`和`MakeUpper()`方法使字符小寫和大寫。 `cases.cpp` ```cpp #include <wx/string.h> int main(int argc, char **argv) { wxString str = wxT("The history of my life"); wxPuts(str.MakeLower()); wxPuts(str.MakeUpper()); } ``` ```cpp the history of my life THE HISTORY OF MY LIFE ``` 這是輸出。 ## 實用函數 wxWidgets 具有幾個方便的工具函數,用于執行進程,獲取主用戶目錄或獲取 OS 名稱。 在下面的示例中,我們執行`ls`命令。 為此,我們具有`wxShell()`函數(僅 Unix)。 `shell.cpp` ```cpp #include <wx/string.h> #include <wx/utils.h> int main(int argc, char **argv) { wxShell(wxT("ls -l")); } ``` ```cpp total 40 -rwxr-xr-x 1 vronskij vronskij 9028 2007-09-06 22:10 basic -rw-r--r-- 1 vronskij vronskij 95 2007-09-06 22:09 basic.cpp -rw-r--r-- 1 vronskij vronskij 430 2007-09-06 00:07 basic.cpp~ -rwxr-xr-x 1 vronskij vronskij 11080 2007-09-05 23:17 console -rw-r--r-- 1 vronskij vronskij 500 2007-09-05 23:17 console.cpp -rw-r--r-- 1 vronskij vronskij 485 2007-09-05 23:16 console.cpp~ ``` 這是輸出。 接下來,我們將獲得主用戶目錄,操作系統名稱,用戶名,主機名和總可用內存。 `system.cpp` ```cpp #include <wx/string.h> #include <wx/utils.h> int main(int argc, char **argv) { wxPuts(wxGetHomeDir()); wxPuts(wxGetOsDescription()); wxPuts(wxGetUserName()); wxPuts(wxGetFullHostName()); long mem = wxGetFreeMemory().ToLong(); wxPrintf(wxT("Memory: %ld\n"), mem); } ``` ```cpp /home/vronskij Linux 2.6.20-16-generic i686 jan bodnar spartan Memory: 741244928 ``` 這是輸出。 ## 時間日期 在 wxWidgets 中,我們有幾個用于處理日期&時間的類。 該示例以各種格式顯示當前日期或時間。 `datetime.cpp` ```cpp #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); } ``` ```cpp Fri Sep 7 21:28:38 2007 21:28:38 09/07/07 ``` 這是輸出。 接下來,我們將顯示不同城市的當前時間。 `datetime2.cpp` ```cpp #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()); } ``` ```cpp 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 ``` 這是輸出。 以下示例顯示了如何將日期范圍添加到日期/時間。 我們將當前時間增加一個月。 `datespan.cpp` ```cpp #include <wx/datetime.h> int main(int argc, char **argv) { wxDateTime now = wxDateTime::Now(); wxString date1 = now.Format(wxT("%B %d %Y")); wxPuts(date1); wxDateSpan span(0, 1); wxDateTime then = now.Add(span); wxString date2 = then.Format(wxT("%B %d %Y")); wxPuts(date2); } ``` ```cpp September 07 2007 October 07 2007 ``` 這是輸出。 ### 文件 wxWidgets 有幾個類可簡化文件的處理。 與使用流相反,這是對文件的低級別訪問。 在下面的示例中,我們使用`wxFile`類創建一個新文件并將數據寫入其中。 我們還將測試文件是否打開。 請注意,當我們創建文件時,它會自動保持打開狀態。 `createfile.cpp` ```cpp #include <wx/file.h> int main(int argc, char **argv) { wxString str = wxT("You make me want to be a better man.\n"); wxFile file; file.Create(wxT("quote"), 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")); } ``` ```cpp $ ls qoute ls: qoute: No such file or directory $ ./createfile the file is opened the file is not opened $ cat quote You make me want to be a better man. ``` 這是輸出。 `wxTextFile`是一個簡單的類,允許逐行處理文本文件。 與`wxFile`類相比,使用此類更容易。 在下一個示例中,我們將打印文件中的行數,第一行和最后一行,最后將讀取并顯示文件的內容。 `readfile.cpp` ```cpp #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(); } ``` ```cpp Number of lines: 8 First line: #include <glib.h> Last line: } ------------------------------------- #include <glib.h> #include <glib/gstdio.h> int main() { g_mkdir("/home/vronskij/test", S_IRWXU); } ``` 這是輸出。 `wxDir`類允許我們枚舉文件和目錄。 在以下示例中,我們將打印當前工作目錄中可用的所有文件和目錄。 `dir.cpp` ```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); } } ``` ```cpp $ ./dir dir temp console basic.cpp basic quote createfile console.cpp basic.cpp~ test.c console.cpp~ ``` 這是輸出。 在本章中,我們介紹了一些 wxWidgets 幫助器類。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看