wxWidget入門(六)
阿新 • • 發佈:2018-11-11
這一期是為了解決(三)中關於檔案操作的歷史遺留問題。
其實這一期不出也沒什麼關係,我相信如果大家有詳細跟著我的學習筆記一步步敲下來。會看官方文件,應該問題不大。
解題思路:
嘗試:
既然是檔案操作,聯絡老外的命名規則,再怎麼著總有一個類是有帶"file"的類吧,無非是加了一個"商標" —— "wx" 而已
通過搜尋得到一個連結https://docs.wxwidgets.org/trunk/classwx_file.html
包的標頭檔案就是
#include <wx/wx.h>
#include <wx/file.h>
先講一個前提
檔案的根目錄
①New——新建一個檔案
我這裡就簡單示範下,預設是新建"aaa.txt",如果存在就不新建了。
void Openfile::OnNew(wxCommandEvent & event)
{
wxFile *file = new wxFile(wxT("aaa.txt"));
//exists函式是靜態函式
if(wxFile::Exists(wxT("aaa.txt")) == false)
{
file->Create(wxT("aaa.txt"));
}
}
②Open——開啟檔案讀取其內容
void Openfile::OnOpen(wxCommandEvent & event) { //將讀取的檔案字串 顯示到檔案對話方塊內 wxFileDialog *openFileDialog = new wxFileDialog(this); //如果控制元件成功顯示 if(openFileDialog->ShowModal() == wxID_OK) { wxString fileName = openFileDialog->GetPath(); tc->LoadFile(fileName); } }
③Save——儲存字串至檔案中
void Openfile::OnSave(wxCommandEvent & event)
{
wxFileDialog *openFileDialog = new wxFileDialog(this);
if(openFileDialog->ShowModal() == wxID_OK)
{
wxString fileName = openFileDialog->GetPath();
tc->SaveFile(fileName);
}
}
貼出全部程式碼
#include <wx/wx.h> #include <wx/file.h> class Openfile : public wxFrame { public: Openfile(const wxString & title); void OnOpen(wxCommandEvent & event); void OnSave(wxCommandEvent & event); void OnNew(wxCommandEvent & event); wxTextCtrl * tc; }; Openfile::Openfile(const wxString & title) :wxFrame(NULL, -1 , title, wxPoint(-1, -1), wxSize(300, 240)) { wxMenuBar *menubar = new wxMenuBar; wxMenu * file = new wxMenu; wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL); wxBoxSizer *hbox1 = new wxBoxSizer(wxHORIZONTAL); wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL); file->Append(wxID_OPEN, wxT("&Open")); file->Append(wxID_NEW, wxT("&New")); menubar->Append(file, wxT("&File")); SetMenuBar(menubar); Connect(wxID_OPEN, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Openfile::OnOpen)); Connect(wxID_NEW, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Openfile::OnNew)); tc = new wxTextCtrl(this, -1, wxT(""), wxPoint(-1, -1), wxSize(-1,-1), wxTE_MULTILINE); hbox1->Add(tc); wxButton *save = new wxButton(this, wxID_SAVE, wxT("&Save")); wxButton *ok = new wxButton(this, -1, wxT("&Ok")); Connect(wxID_SAVE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Openfile::OnSave)); hbox2->Add(save); hbox2->Add(ok); vbox->Add(hbox1, 1, wxEXPAND); vbox->Add(hbox2, 0, wxALIGN_RIGHT | wxRIGHT | wxBOTTOM, 10); SetSizer(vbox); Center(); } void Openfile::OnNew(wxCommandEvent & event) { wxFile *file = new wxFile(wxT("aaa.txt")); if(wxFile::Exists(wxT("aaa.txt")) == false) { file->Create(wxT("aaa.txt")); } } void Openfile::OnSave(wxCommandEvent & event) { wxFileDialog *openFileDialog = new wxFileDialog(this); if(openFileDialog->ShowModal() == wxID_OK) { wxString fileName = openFileDialog->GetPath(); tc->SaveFile(fileName); } } void Openfile::OnOpen(wxCommandEvent & event) { wxFileDialog *openFileDialog = new wxFileDialog(this); if(openFileDialog->ShowModal() == wxID_OK) { wxString fileName = openFileDialog->GetPath(); tc->LoadFile(fileName); } } class MyApp : public wxApp { public: virtual bool OnInit(); }; IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { Openfile *open = new Openfile(wxT("Openfile")); open->Show(true); return true; }
歡迎大家留言,點評,謝謝。