这一期是为了解决(三)中关于文件操作的历史遗留问题。
其实这一期不出也没什么关系,我相信如果大家有详细跟着我的学习笔记一步步敲下来。会看官方文档,应该问题不大。
解题思路:
尝试:
既然是文件操作,联系老外的命名规则,再怎么着总有一个类是有带"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;
}
欢迎大家留言,点评,谢谢。