用VC++实现一个文本文件阅读器
2016-03-23 16:40
465 查看
用VC++实现一个文本文件阅读器,用户选定目录,列出该目录下的所有.txt,单击某个txt,在右侧显示内容;
首先新建一个MFC exe,基于对话框工程;
设计界面如下;
添加按钮单击函数;
为对话框类添加成员函数;
为列表框添加一个成员变量;
添加列表框单击函数;(选中条目后执行的函数;)
对话框类头文件手动添加
private:
TCHAR pszPath[MAX_PATH];
对话框类实现文件中新增函数代码
void CTxtreaderDlg::OnOK()
{
// TODO: Add extra validation here
BROWSEINFO bi;
bi.hwndOwner = this->GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = NULL;
bi.lpszTitle = TEXT("请选择文件夹");
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl == NULL)
{
return;
}
if (SHGetPathFromIDList(pidl, pszPath))
{
//AfxMessageBox(pszPath);
SetDlgItemText(IDC_STATIC, pszPath);
FindTxtFile(pszPath);
}
//CDialog::OnOK();
}
void CTxtreaderDlg::FindTxtFile(CString strFoldername)
{
CFileFind fileFind;
int i = 0;
CString strdir = strFoldername + _T("\\*.txt");
m_list1.ResetContent();
if(fileFind.FindFile(strdir,0))
{
BOOL bRet = TRUE;
while(bRet)
{
bRet = fileFind.FindNextFile();
if(fileFind.IsDots())
{
continue;
}
if(fileFind.IsDirectory())
{
//str.push_back(fileFind.GetFilePath());
//strDir[i++] = fileFind.GetFilePath();
}
else
{
m_list1.AddString(fileFind.GetFileName());
}
}
fileFind.Close();
}
return;
}
void CTxtreaderDlg::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
SetDlgItemText(IDC_EDIT1,"");
int n=m_list1.GetCurSel();
CString content;
TCHAR filename[32];
CString szLine="";
m_list1.GetText(n,filename);
CStdioFile file;
// file.Open(pszPath+_T("\\")+filename,CFile::modeRead); error C2110: cannot add two pointers
strcat(pszPath,_T("\\"));
strcat(pszPath,filename);
//pszPath+=_T("\\");
//pszPath+=filename;
file.Open(pszPath,CFile::modeRead);
//逐行读取字符串
while(file.ReadString(szLine))
{
content+=szLine;
content+="\r\n";
}
file.Close();
SetDlgItemText(IDC_EDIT1,content);
}
效果:
首先新建一个MFC exe,基于对话框工程;
设计界面如下;
添加按钮单击函数;
为对话框类添加成员函数;
为列表框添加一个成员变量;
添加列表框单击函数;(选中条目后执行的函数;)
对话框类头文件手动添加
private:
TCHAR pszPath[MAX_PATH];
对话框类实现文件中新增函数代码
void CTxtreaderDlg::OnOK()
{
// TODO: Add extra validation here
BROWSEINFO bi;
bi.hwndOwner = this->GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = NULL;
bi.lpszTitle = TEXT("请选择文件夹");
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl == NULL)
{
return;
}
if (SHGetPathFromIDList(pidl, pszPath))
{
//AfxMessageBox(pszPath);
SetDlgItemText(IDC_STATIC, pszPath);
FindTxtFile(pszPath);
}
//CDialog::OnOK();
}
void CTxtreaderDlg::FindTxtFile(CString strFoldername)
{
CFileFind fileFind;
int i = 0;
CString strdir = strFoldername + _T("\\*.txt");
m_list1.ResetContent();
if(fileFind.FindFile(strdir,0))
{
BOOL bRet = TRUE;
while(bRet)
{
bRet = fileFind.FindNextFile();
if(fileFind.IsDots())
{
continue;
}
if(fileFind.IsDirectory())
{
//str.push_back(fileFind.GetFilePath());
//strDir[i++] = fileFind.GetFilePath();
}
else
{
m_list1.AddString(fileFind.GetFileName());
}
}
fileFind.Close();
}
return;
}
void CTxtreaderDlg::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
SetDlgItemText(IDC_EDIT1,"");
int n=m_list1.GetCurSel();
CString content;
TCHAR filename[32];
CString szLine="";
m_list1.GetText(n,filename);
CStdioFile file;
// file.Open(pszPath+_T("\\")+filename,CFile::modeRead); error C2110: cannot add two pointers
strcat(pszPath,_T("\\"));
strcat(pszPath,filename);
//pszPath+=_T("\\");
//pszPath+=filename;
file.Open(pszPath,CFile::modeRead);
//逐行读取字符串
while(file.ReadString(szLine))
{
content+=szLine;
content+="\r\n";
}
file.Close();
SetDlgItemText(IDC_EDIT1,content);
}
效果:
相关文章推荐
- MooBox 基于Mootools的对话框插件
- 基于jQuery实现带动画效果超炫酷的弹出对话框(附源码下载)
- C#实现在前端网页弹出警告对话框(alert)的方法
- Android开发必知 九种对话框的实现方法
- Android列表对话框用法实例分析
- VC中CDC、HDC、pDC区别与联系及相互转换
- VC实现获取当前正在运行的进程
- vc中SendMessage自定义消息函数用法实例
- C#使用Word中的内置对话框实例
- VC实现让关闭按钮成灰色不可用的方法
- VC++中的字体设置方法详解
- VC程序设计中CreateProcess用法注意事项
- VC实现批量删除指定文件的方法
- MFC对话框中添加状态栏的方法
- vc中使用SendMessage自定义消息函数
- vc提示unexpected end of file found的原因分析
- vc获取计算机名和ip地址的方法
- VC6.0实现读取Excel数据的方法
- VC下通过系统快照实现进程管理的方法
- PHP+Mysql+jQuery查询和列表框选择操作实例讲解