基于MFC如何在视图中打开一个文本?

zhangxiaohan 2005-11-03 03:55:02
各位大虾,VC我实在太菜了,恳请指教!

在MFC的下拉菜单中添了一个新项OPENTEXT,里面用CFileDialog实现了一个对话框,就想通过这个对话框把文本文件在视图中显示出来,视图的基类用的是CEditView好象能容易些吧!整了好几天了就是整不出来,恳请帮忙!

void CMainFrame::OnOpenText()
{
CString StrFilter="打开文本(***.txt)|***.txt|所有文件(*.*)|*.*||";
CFileDialog OT(TRUE,NULL,NULL,NULL,StrFilter,this);
OT.m_ofn.lpstrTitle="打开文本";
OT.m_ofn.lpstrInitialDir="C:\\DataFile";
if(OT.DoModal()==IDOK)
{

.....//应该在这里添代码吧

}

}
...全文
145 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangxiaohan 2005-11-03
  • 打赏
  • 举报
回复
鹦鹉大哥~~问题解决了!一时脑袋转不过来弯~~感谢,感谢!
快乐鹦鹉 2005-11-03
  • 打赏
  • 举报
回复
晕死。你连把消息映射建立到view都不知道啊。那你怎么映射到mainframe的呢?
zhangxiaohan 2005-11-03
  • 打赏
  • 举报
回复
还是报错啊~
----另外,你不要把这个消息映射用MainFrame响应,直接用视图响应就行了!

这句话什么意思?麻烦鹦鹉大哥解释详细一些啊~~MFC的整个框架我还是不太明白,不在MainFrame里响应,在视图里怎么响应?感谢~~感谢~~

Compiling...
MainFrm.cpp
D:\Project\SYSTEM\MainFrm.cpp(191) : error C2065: 'GetEditCtrl' : undeclared identifier
D:\Project\SYSTEM\MainFrm.cpp(191) : error C2440: 'initializing' : cannot convert from 'int *' to 'class CEdit'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\Project\SYSTEM\MainFrm.cpp(192) : error C2819: type 'CEdit' does not have an overloaded member 'operator ->'
d:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2974) : see declaration of 'CEdit'
D:\Project\SYSTEM\MainFrm.cpp(192) : error C2227: left of '->SetWindowTextA' must point to class/struct/union
Error executing cl.exe.

MainFrm.obj - 4 error(s), 0 warning(s)
快乐鹦鹉 2005-11-03
  • 打赏
  • 举报
回复
对了,最后忘了调用file.Close()关闭文件。
快乐鹦鹉 2005-11-03
  • 打赏
  • 举报
回复
if(OT.DoModal()==IDOK)
{

CString sFileName = OT.GetPathName();
CFile file;
file.Open(sFileName,CFile::modeRead);
int nLen = file.GetLength();
char* pInfo = new char[nLen];
file.ReadHuge(pInfo,nLen);
CEdit pEdit = &GetEditCtrl();
pEdit->SetWindowText(pInfo);
delete[] pInfo;

}
另外,你不要把这个消息映射用MainFrame响应,直接用视图响应就行了!
zhangxiaohan 2005-11-03
  • 打赏
  • 举报
回复
报了三个错~~

MainFrm.cpp
D:\Project\SYSTEM\MainFrm.cpp(185) : error C2065: 'GetPathName' : undeclared identifier
D:\Project\SYSTEM\MainFrm.cpp(191) : error C2065: 'GetEditCtrl' : undeclared identifier
D:\Project\SYSTEM\MainFrm.cpp(191) : error C2440: 'initializing' : cannot convert from 'int' to 'class CEdit &'
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.
bobob 2005-11-03
  • 打赏
  • 举报
回复
得到文本文件名字,然后用CStdioFile打开,使用ReadString一行一行的读,用dc的textout或drawtext显示
快乐鹦鹉 2005-11-03
  • 打赏
  • 举报
回复
if(OT.DoModal()==IDOK)
{

CString sFileName = GetPathName();
CFile file;
file.Open(sFileName,CFile::modeRead);
int nLen = file.GetLength();
char* pInfo = new char[nLen];
file.ReadHuge(pInfo,nLen);
CEdit &edit = GetEditCtrl();
edit.SetWindowText(pInfo);
delete[] pInfo;

}

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧