概述
【题目40】简易文本编辑器。
设计一个简易的文本编辑器。
设计要求:
(1) 具有图形菜单界面;
(2) 查找,替换(等长,不等长),插入(插串,文本块的插入)、文本块移动(行块,列块移动),删除;
(3) 可正确存盘、取盘;
(4) 正确显示总行数。
这是题目.................. 使用基于对话框的MFC 我想到的感觉有问题的点! 查找替换 算法不了解 最烦人的时文本块移动 百度好多业找不到!
文本块移动使用RICHEDIT控件 这个是自带文本块移动 存取盘问题可以读写文件(这里可能会遇到编码问题中文乱码!!!)代码我会附加的 链接 https://www.lanzous.com/i1u3ojc (VS2013编译通过) github不会用原谅我
截图
先说说那个richedit 需要初始化 不清楚的话取百度一下用法 否者会没法运行(闪退) 在你的InitInstance()下 添加 AfxInitRichEdit2();//初始化编辑框
然后是那个查找替换 需要注册消息应该是这样的C+MFC就学了一点这还没学
在你的DLG.CPP头文件中添加 (为什么别的(不是dlg.cpp)文件没发调用我就不知道了)
曾经出现这个事了
static const UINT MsgFindReplace = ::RegisterWindowMessage(FINDMSGSTRING);
这一段缺那个补那个
BEGIN_MESSAGE_MAP(CJIANDANBIANJIQIDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_SIZE()
ON_EN_UPDATE(IDC_RICHEDIT21, &CJIANDANBIANJIQIDlg::OnEnUpdateRichedit21)
ON_REGISTERED_MESSAGE(MsgFindReplace, OnFindReplace)
ON_COMMAND(ID_tihuanchazhao, &CJIANDANBIANJIQIDlg::Ontihuanchazhao)
ON_COMMAND(ID_dakai, &CJIANDANBIANJIQIDlg::Ondakai)
ON_COMMAND(ID_lingcunwei, &CJIANDANBIANJIQIDlg::Onlingcunwei)
END_MESSAGE_MAP()
复写 OnFindReplace(WPARAM wParam, LPARAM lParam) 方法 下面是代码 网上好多不全百度很多不知道是那家 如果此人看到了此代码联系补引用链接
这个函数记得在头文件定义一下
long CJIANDANBIANJIQIDlg::OnFindReplace(WPARAM wParam, LPARAM lParam)
{
int curpos = 0;
int pos = 0;
if (pFindReplaceDlg->IsTerminating()){ //判断对话框是否被关闭
pFindReplaceDlg = NULL;
}
//获取当前查找对话框的指针
CString strText, repText;
CFindReplaceDialog * pDlg = CFindReplaceDialog::GetNotifier(lParam);
strText = pDlg->GetFindString(); // 要查找的字符串
CString str; // RICHEDIT控件中的总的字符串m_RichEdit.GetWindowText(str);
bianjikuang1.GetWindowText(str);
int len = strText.GetLength(); // 要查找的字符串长度
long index = 0, end_index = 0; // 索引的起始位置 及 结束位置, 从0开始if (find) // 查找对话框
if (pDlg->SearchDown())
{
bianjikuang1.GetSel(index, end_index);
index = str.Find(strText, end_index); // 从上次查询的结束位置处继续往下查询
if (index!=-1) //往下搜索时,找到所要查询的子字符串
{
bianjikuang1.SetSel(index, index + len); // 为找到的子字符串 设置选择标记
bianjikuang1.SetFocus(); // 在找到的子字符串上,设置焦点
}
else
{
index = str.Find(strText, 0); //从文件开始处,重新查找, 可以实现当搜索到文件末尾时,点查找下一个按钮时,可以从头重新查,从而实现循环功能
if (index == -1)
{
MessageBox(L"以查找完毕,无所查内容"); //若从开始处查找,仍然找不到,则说明无此查找内容
return 0;
}
bianjikuang1.SetSel(index, index + len); // 若从开始处查找,查询到所查内容,则为其做标记
bianjikuang1.SetFocus();
}}
else
{
bianjikuang1.GetSel(index, end_index); // 得到当前选择文本的始末位置
CString strReverse = str.MakeReverse(); // 对控件中的文字进行反转
CString strTextReverse = strText.MakeReverse(); // 对查找串的文字进行反转
index = strReverse.Find(strTextReverse, str.GetLength() - index); 从反串中往下查找 查找的起点位置索引为 ID=STR.Length-index-1的前一位,即从源串开始位置的前一位搜索 ,不能从源串开始位置处搜索,因为若仅单个字符时,总是查询到这个位置,不会再往前查询了
if (index != -1)
{
end_index = str.GetLength() - index - 1; // 源串与反串index 的关
}
else
{
index = strReverse.Find(strTextReverse, 0);
if (index == -1){
MessageBox(L"以查找完毕,无所查内容");
}
bianjikuang1.SetSel(index, index + len);
bianjikuang1.SetFocus();
}
}
if (pDlg->ReplaceCurrent())//替换
{
if (index == -1)
{
MessageBox(L"以查找完毕,无所查内容");
}
else
{
bianjikuang1.GetSel(index, end_index);
CString sub_left, sub_mid,
sub_ringht; sub_left = str.Left(index);
sub_mid = str.Mid(index, end_index - index);
if (sub_mid != strText) // 判断当前选择文本 是否为查找文本
{
MessageBox(L"请重新搜索");
return 0;
}
sub_ringht = str.Right(str.GetLength() - 1 - end_index + 1); // 右边串包含end_index这个位置的字符
repText = pDlg->GetReplaceString();
str = sub_left + repText + sub_ringht;
bianjikuang1.SetWindowText(str);
}
}
if (pDlg->ReplaceAll())
{
int num = 0;
CString mssag;
repText = pDlg->GetReplaceString();
len = repText.GetLength();
num = str.Replace(strText, repText);
bianjikuang1.SetWindowText(str);
mssag.Format(L"已经完成所有替换,共替换%d处", num);
MessageBox(mssag);
}
}
接下来问题是打开保存文件遇到了 乱码问题 百度了不zhi一家
打开文件
CFileDialog dlg(true, _T("txt"), _T("*.txt"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("ALL Files(*.TXT)|*.TXT||"), NULL);
CString strPath, strText;
if (dlg.DoModal() == IDOK)
{
strPath = dlg.GetPathName();
//m_OP.SetWindowText(strPath);
CFile file(strPath, CFile::modeRead);
TCHAR* temp = new TCHAR[file.GetLength() / 2 + 1];
file.Read(temp, file.GetLength());
temp[file.GetLength() / 2] = 0;
file.Close();
bianjikuang1.SetWindowTextW(temp);
}
另存文件
CFileDialog dlg(FALSE);
CString strPath, strText;
char write[10000];
if (dlg.DoModal() == IDOK)
{
strPath = dlg.GetPathName();
if (strPath.Right(4) != ".TXT")
strPath += ".TXT";
//SetDlgItemText(IDC, strPath);
CFile file(strPath, CFile::modeCreate | CFile::modeWrite);
GetDlgItemText(IDC_RICHEDIT21, strText);
CStringA strA(strText.GetBuffer(0));
strText.ReleaseBuffer();
std::string s = strA.GetBuffer(0);
const char* tempya = s.c_str();
strcpy_s(write, tempya);
WORD unicode = 0xFEFF; //这句重要,注意这里是F E FF,中间有个E
file.Write(&unicode, 2); //这句重要
file.Write(strText, wcslen(strText)*sizeof(wchar_t));
file.Close();
}
代码链接已经附上了!!!状态栏我就不说了 不懂的具体百度!
转载于:https://www.cnblogs.com/xuexidememeda/p/9612918.html
最后
以上就是犹豫蜜蜂为你收集整理的C++ mfc 简易文本编辑器 遇到的一些问题的全部内容,希望文章能够帮你解决C++ mfc 简易文本编辑器 遇到的一些问题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复