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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # wxWidgets 中的對話框 > 原文: [http://zetcode.com/gui/wxwidgets/dialogs/](http://zetcode.com/gui/wxwidgets/dialogs/) 對話框窗口或對話框是大多數現代 GUI 應用必不可少的部分。 對話被定義為兩個或更多人之間的對話。 在計算機應用中,對話框是一個窗口,用于與應用“對話”。 對話框用于輸入數據,修改數據,更改應用設置等。對話框是用戶與計算機程序之間進行通信的重要手段。 對話框本質上有兩種:預定義對話框和自定義對話框。 ## 預定義對話框 預定義對話框是 wxWidgets 工具包中可用的對話框。 這些是用于常見編程任務的對話框,例如顯示文本,接收輸入,加載和保存文件等。它們可以節省程序員的時間,并通過使用某些標準行為來增強功能。 ## `MessageDialog` 消息對話框用于向用戶顯示消息。 它們是可定制的。 我們可以更改將在對話框中顯示的圖標和按鈕。 `Messages.h` ```cpp #include <wx/wx.h> class Messages : public wxFrame { public: Messages(const wxString& title); void ShowMessage1(wxCommandEvent & event); void ShowMessage2(wxCommandEvent & event); void ShowMessage3(wxCommandEvent & event); void ShowMessage4(wxCommandEvent & event); }; const int ID_INFO = 1; const int ID_ERROR = 2; const int ID_QUESTION = 3; const int ID_ALERT = 4; ``` `Messages.cpp` ```cpp #include "Messages.h" Messages::Messages(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(210, 110)) { wxPanel *panel = new wxPanel(this, wxID_ANY); wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL); wxGridSizer *gs = new wxGridSizer(2, 2, 2, 2); wxButton *btn1 = new wxButton(panel, ID_INFO, wxT("Info")); wxButton *btn2 = new wxButton(panel, ID_ERROR, wxT("Error")); wxButton *btn3 = new wxButton(panel, ID_QUESTION, wxT("Question")); wxButton *btn4 = new wxButton(panel, ID_ALERT, wxT("Alert")); Connect(ID_INFO, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Messages::ShowMessage1)); Connect(ID_ERROR, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Messages::ShowMessage2)); Connect(ID_QUESTION, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Messages::ShowMessage3)); Connect(ID_ALERT, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Messages::ShowMessage4)); gs->Add(btn1, 1, wxEXPAND); gs->Add(btn2, 1); gs->Add(btn3, 1); gs->Add(btn4, 1); hbox->Add(gs, 0, wxALL, 15); panel->SetSizer(hbox); Center(); } void Messages::ShowMessage1(wxCommandEvent& event) { wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Download completed"), wxT("Info"), wxOK); dial->ShowModal(); } void Messages::ShowMessage2(wxCommandEvent& event) { wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Error loading file"), wxT("Error"), wxOK | wxICON_ERROR); dial->ShowModal(); } void Messages::ShowMessage3(wxCommandEvent& event) { wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Are you sure to quit?"), wxT("Question"), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION); dial->ShowModal(); } void Messages::ShowMessage4(wxCommandEvent& event) { wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Unallowed operation"), wxT("Exclamation"), wxOK | wxICON_EXCLAMATION); dial->ShowModal(); } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "Messages.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Messages *msgs = new Messages(wxT("Messages")); msgs->Show(true); return true; } ``` 在我們的示例中,我們創建了四個按鈕并將它們放入網格大小調整器中。 這些按鈕將顯示四個不同的對話框窗口。 我們通過指定不同的樣式標志來創建它們。 ```cpp wxMessageDialog *dial = new wxMessageDialog(NULL, wxT("Error loading file"), wxT("Error"), wxOK | wxICON_ERROR); dial->ShowModal(); ``` 消息對話框的創建很簡單。 通過提供`NULL`作為父級,我們將對話框設置為頂級窗口。 這兩個字符串提供了消息文本和對話框標題。 通過指定`wxOK`和`wxICON_ERROR`標志,我們顯示一個 OK 按鈕和一個錯誤圖標。 為了在屏幕上顯示對話框,我們調用`ShowModal()`方法。 ![Info dialog](https://img.kancloud.cn/2c/87/2c87e8ae5103807a9ea96ced2ec4d6ee_258x191.jpg) ![Question dialog](https://img.kancloud.cn/eb/b7/ebb78b39f87a6824e60d32a7cae3c577_256x191.jpg) ![Alert dialog](https://img.kancloud.cn/ee/12/ee128aaa47dd2258b89d2a30ec3afbaa_254x191.jpg) ![Error dialog](https://img.kancloud.cn/94/f8/94f89258558f129f10eb2b2900d229e9_227x191.jpg) ## `wxFileDialog` 這是打開和保存文件的常用對話框。 `openfile.h` ```cpp #include <wx/wx.h> class Openfile : public wxFrame { public: Openfile(const wxString& title); void OnOpen(wxCommandEvent& event); wxTextCtrl *tc; }; ``` `openfile.cpp` ```cpp #include "openfile.h" Openfile::Openfile(const wxString & title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200)) { wxMenuBar *menubar = new wxMenuBar; wxMenu *file = new wxMenu; file->Append(wxID_OPEN, wxT("&Open")); menubar->Append(file, wxT("&File")); SetMenuBar(menubar); Connect(wxID_OPEN, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Openfile::OnOpen)); tc = new wxTextCtrl(this, -1, wxT(""), wxPoint(-1, -1), wxSize(-1, -1), wxTE_MULTILINE); Center(); } void Openfile::OnOpen(wxCommandEvent& event) { wxFileDialog * openFileDialog = new wxFileDialog(this); if (openFileDialog->ShowModal() == wxID_OK){ wxString fileName = openFileDialog->GetPath(); tc->LoadFile(fileName); } } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "openfile.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Openfile *open = new Openfile(wxT("Openfile")); open->Show(true); return true; } ``` 在我們的示例中,我們顯示一個打開文件菜單項和一個簡單的多行文本控件。 如果單擊打開文件菜單項,則會顯示`wxFileDialog`。 我們可以將一些簡單的文本文件加載到文本控件中。 ```cpp tc = new wxTextCtrl(this, -1, wxT(""), wxPoint(-1, -1), wxSize(-1, -1), wxTE_MULTILINE); ``` 我們將文本文件加載到此文本控件中。 ```cpp wxFileDialog * openFileDialog = new wxFileDialog(this); ``` 在這里,我們創建一個`wxFileDialog`。 我們使用默認參數。 (打開文件對話框是默認對話框。) ```cpp if (openFileDialog->ShowModal() == wxID_OK){ wxString fileName = openFileDialog->GetPath(); tc->LoadFile(fileName); } ``` 在這里,我們顯示對話框。 我們獲得選定的文件名,并將文件加載到文本控件中。 ![wxFileDialog](https://img.kancloud.cn/14/9e/149ee77c6d6b61722c97aa21b0ac1058_521x450.jpg) 圖:Linux 上的`wxFileDialog` ## `wxFontDialog` 這是選擇字體的常用對話框。 `fontdialog.h` ```cpp #include <wx/wx.h> class ChangeFont : public wxFrame { public: ChangeFont(const wxString& title); void OnOpen(wxCommandEvent& event); wxStaticText *st; }; const int ID_FONTDIALOG = 1; ``` `fontdialog.cpp` ```cpp #include <wx/fontdlg.h> #include "fontdialog.h" ChangeFont::ChangeFont(const wxString & title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200)) { wxPanel *panel = new wxPanel(this, -1); wxMenuBar *menubar = new wxMenuBar; wxMenu *file = new wxMenu; file->Append(ID_FONTDIALOG, wxT("&Change font")); menubar->Append(file, wxT("&File")); SetMenuBar(menubar); Connect(ID_FONTDIALOG, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(ChangeFont::OnOpen)); st = new wxStaticText(panel, wxID_ANY, wxT("The Agoge"), wxPoint(20, 20)); Center(); } void ChangeFont::OnOpen(wxCommandEvent& WXUNUSED(event)) { wxFontDialog *fontDialog = new wxFontDialog(this); if (fontDialog->ShowModal() == wxID_OK) { st->SetFont(fontDialog->GetFontData().GetChosenFont()); } } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "fontdialog.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { ChangeFont *change = new ChangeFont(wxT("Change font")); change->Show(true); return true; } ``` 在此示例中,我們將更改靜態文本示例的字體。 ```cpp st = new wxStaticText(panel, wxID_ANY, wxT("The Agoge"), wxPoint(20, 20)); ``` 在這里,我們在面板上顯示靜態文本。 我們將使用`wxFontDialog`更改其字體。 ```cpp wxFontDialog *fontDialog = new wxFontDialog(this); if (fontDialog->ShowModal() == wxID_OK) { st->SetFont(fontDialog->GetFontData().GetChosenFont()); } ``` 在這些代碼行中,我們顯示字體對話框。 然后我們得到選擇的字體。 最后,我們更改之前創建的靜態文本的字體。 ![Font dialog](https://img.kancloud.cn/90/08/90084d05676e5436012867e69507512d_489x402.jpg) 圖:字體對話框 ## 自定義對話框 在下一個示例中,我們創建一個自定義對話框。 圖像編輯應用可以更改圖片的顏色深度。 為了提供這種功能,我們可以創建一個合適的自定義對話框。 `customdialog.h` ```cpp #include <wx/wx.h> class CustomDialog : public wxDialog { public: CustomDialog(const wxString& title); }; ``` `customdialog.cpp` ```cpp #include "customdialog.h" CustomDialog::CustomDialog(const wxString & title) : wxDialog(NULL, -1, title, wxDefaultPosition, wxSize(250, 230)) { wxPanel *panel = new wxPanel(this, -1); wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL); wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL); wxStaticBox *st = new wxStaticBox(panel, -1, wxT("Colors"), wxPoint(5, 5), wxSize(240, 150)); wxRadioButton *rb = new wxRadioButton(panel, -1, wxT("256 Colors"), wxPoint(15, 30), wxDefaultSize, wxRB_GROUP); wxRadioButton *rb1 = new wxRadioButton(panel, -1, wxT("16 Colors"), wxPoint(15, 55)); wxRadioButton *rb2 = new wxRadioButton(panel, -1, wxT("2 Colors"), wxPoint(15, 80)); wxRadioButton *rb3 = new wxRadioButton(panel, -1, wxT("Custom"), wxPoint(15, 105)); wxTextCtrl *tc = new wxTextCtrl(panel, -1, wxT(""), wxPoint(95, 105)); wxButton *okButton = new wxButton(this, -1, wxT("Ok"), wxDefaultPosition, wxSize(70, 30)); wxButton *closeButton = new wxButton(this, -1, wxT("Close"), wxDefaultPosition, wxSize(70, 30)); hbox->Add(okButton, 1); hbox->Add(closeButton, 1, wxLEFT, 5); vbox->Add(panel, 1); vbox->Add(hbox, 0, wxALIGN_CENTER | wxTOP | wxBOTTOM, 10); SetSizer(vbox); Centre(); ShowModal(); Destroy(); } ``` `main.h` ```cpp #include <wx/wx.h> class MyApp : public wxApp { public: virtual bool OnInit(); }; ``` `main.cpp` ```cpp #include "main.h" #include "customdialog.h" IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { CustomDialog *custom = new CustomDialog(wxT("CustomDialog")); custom->Show(true); return true; } ``` 本示例是基于對話框的應用。 我們說明了如何創建自定義對話框。 ```cpp class CustomDialog : public wxDialog ``` 自定義對話框基于`wxDialog`類。 ```cpp wxStaticBox *st = new wxStaticBox(panel, -1, wxT("Colors"), wxPoint(5, 5), wxSize(240, 150)); wxRadioButton *rb = new wxRadioButton(panel, -1, wxT("256 Colors"), wxPoint(15, 30), wxDefaultSize, wxRB_GROUP); ``` 請注意,必須先創建`wxStaticBox`小部件,然后再包含該小部件,并且這些小部件應該是靜態框的同級,而不是子級。 ```cpp ShowModal(); Destroy(); ``` 為了在屏幕上顯示對話框,我們調用`ShowModal()`方法。 要從內存中清除對話框,我們調用`Destroy()`方法。 ![Custom dialog](https://img.kancloud.cn/3a/af/3aaf262f8679a85e7ae3af6007133ccf_290x289.jpg) 圖:自定義對話框 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>

                              哎呀哎呀视频在线观看