📄 filesysdlg.cpp
字号:
// FileSysDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FileSys.h"
#include "FileSysDlg.h"
#include "FileAttributeDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileSysDlg dialog
CFileSysDlg::CFileSysDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileSysDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFileSysDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFileSysDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileSysDlg)
DDX_Control(pDX, IDC_EDIT_FILE, m_editFile);
DDX_Control(pDX, IDC_TREE_FILE, m_listFile);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFileSysDlg, CDialog)
//{{AFX_MSG_MAP(CFileSysDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(NM_DBLCLK, IDC_TREE_FILE, OnDblclkTreeFile)
ON_NOTIFY(NM_RCLICK, IDC_TREE_FILE, OnRclickTreeFile)
ON_BN_CLICKED(IDB_CREATE, OnCreate)
ON_BN_CLICKED(IDB_SAVE, OnSave)
ON_WM_KEYDOWN()
ON_WM_CONTEXTMENU()
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(ID_FILE_EDIT,ID_FILE_NEW_CATALOG,OnMenu)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileSysDlg message handlers
BOOL CFileSysDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_menu=new CMenu; //创建菜单指针
m_strPath=""; //初始化打文件指针
m_file=new CFile; //创建文件指针
m_imageList.Create(16,16,ILC_COLOR4,0,4); //创建图象列表
m_bit1.LoadBitmap(IDB_CATALOG); //加载位图IDB_CATALOG
m_bit2.LoadBitmap(IDB_FILE); //加载位图IDB_FILE
m_imageList.Add(&m_bit1,RGB(0,0,0)); //图象列表加载位图
m_imageList.Add(&m_bit2,RGB(0,0,0)); //图象列表加载位图
m_listFile.SetImageList(&m_imageList,TVSIL_NORMAL); //把图象列表加入树状控件
CreateDirectory("C:\\temp",NULL); //创建C:\\temp文件夹
CreateList((HTREEITEM )TVI_ROOT,"C:\\temp"); //以根节点为父节点,C:\\temp为路径名
//初始化树状控件
return TRUE; // return TRUE unless you set the focus to a control
}
void CFileSysDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CFileSysDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CFileSysDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFileSysDlg::CreateList(HTREEITEM parent,char pszDirName[])//根据给定的树控件
{ //和文件或文件夹名
CFileFind file; //文件查找类 变量 //初始化树状控件
BOOL bRet;
char tempFileName[200];
sprintf(tempFileName,"%s\\*.*",pszDirName); //初始化查找目录pszDirName+"\\*.*"
bRet=file.FindFile(tempFileName); //初始化文件查找类file.FindFile(tempFileName)
//,查找tempFileName
//指定的目录,成功返回1,否则返回0
while (bRet) //循环查找
{
char tempFileName1[200];
bRet=file.FindNextFile(); //查找下一个文件
if(!file.IsDots()) // 查找到的不是 .
{
sprintf(tempFileName1,"%s",file.GetFileName()); //查找到的文件或文件夹名
//file.GetFileName()
if(file.IsDirectory()) //查找到的是目录
{
char tempDirName[200];
sprintf(tempDirName,"%s\\%s",pszDirName,tempFileName1); //初始化子查找目录
//当前查找目录pszDirName
//+查找到的文件夹名
//pszDirName+ "\\"+tempFileName1
HTREEITEM itemParent=m_listFile.InsertItem(file.GetFileName(),0,0,parent);
//树状控件加入子项
//m_listFile.InsertItem
//m_listFile是树状控件
//成员变量
CreateList(itemParent,tempDirName); //以新加入的子项,当前文件夹路径
//为路径名递归调用
}
else
{
m_listFile.InsertItem(file.GetFileName(),1,1,parent);//查找到的是文件
//树状控件加入子项
//m_listFile.InsertItem
}
}
}
file.Close(); //关闭文件查找类
}
void CFileSysDlg::OnDblclkTreeFile(NMHDR* pNMHDR, LRESULT* pResult) //响应鼠标双击树状控件
//子项事件的函数
{
// TODO: Add your control notification handler code here
HTREEITEM itemChild=m_listFile.GetSelectedItem(); //获取当前选中树状控件子项
//
int image,image1;
m_listFile.GetItemImage(itemChild,image,image1); //获取当前选种树状控件
//的图标 1为文件,0为文件夹
if(image==1) //如果当前选中的是文件
{
CString str;
str=GetPath(itemChild); //自定义函数,根据树状控件子项获取文件名
m_strPath=str;
m_file->Open(str,CFile::modeReadWrite,NULL); //m_file,CFile 类指针,成员函数
//m_file->Open,打开文件
DWORD cur,end; //表示文件的当前(cur)和终止(end)位置,
m_file->SeekToBegin(); //使m_file指向文件头
cur=m_file->GetPosition(); //m_file->GetPosition()获取当前文件位置
m_file->SeekToEnd(); //使m_file指向文件尾
end=m_file->GetPosition(); //m_file->GetPosition()获取当前文件位置
m_file->SeekToBegin(); //使m_file指向文件头
char buff[201];
CString strText;
SetDlgItemText(IDC_EDIT_FILE,""); //清空编辑框
int nRead;
while(cur<end) //循环读取文件 cur<end
{
nRead=m_file->Read(buff,200); //m_file->Read读取文件,返回实际读取的字节数
//文件指针读取200字节到buff中
buff[nRead]='\0';
GetDlgItemText(IDC_EDIT_FILE,strText); //获得编辑框控件的文本字符串
strText+=buff; //获得的编辑框控件的文本字符串+从文件读出的
//字符串
SetDlgItemText(IDC_EDIT_FILE,strText); //设置编辑框文本 内容为新的strText
cur=m_file->GetPosition(); //把当前文件指针赋值给cur
memset(buff,' ',201); //把字符串buff清空
}
m_file->Close(); //关闭文件指针
}
*pResult = 0;
}
void CFileSysDlg::OnRclickTreeFile(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
SendMessage(WM_CONTEXTMENU,(WPARAM)AfxGetApp()->GetMainWnd(),GetMessagePos());
*pResult = 0;
}
void CFileSysDlg::OnCreate() //创建文件
{
// TODO: Add your control notification handler code here
HTREEITEM itemChild,itemParent;
itemChild=m_listFile.GetSelectedItem(); //获取当前选取子项
if(itemChild==NULL) //当前选取子项为空
{
CEnterNameDlg dlg; //输入文件名对话框
if(dlg.DoModal()==IDOK)
{
CString m_strPath="C:\\temp\\"+dlg.m_strName+".txt"; //设置保存文件路径 名
HANDLE handle=CreateFile(m_strPath,GENERIC_READ|GENERIC_WRITE, //创建文件
FILE_SHARE_READ|
FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,
NULL);
CloseHandle(handle); //关闭文件句柄
m_listFile.InsertItem(dlg.m_strName+".txt",1,1); //树状控件加入文件对应的子项
}
return;
}
itemParent=m_listFile.GetParentItem(itemChild); //获取当前选取子项的父节点
CEnterNameDlg dlg; //输入文件名对话框
if(dlg.DoModal()==IDOK)
{
int image,image1;
m_listFile.GetItemImage(itemChild,image,image1); //获取当前选取子项对应的图象,1为文件
//0为文件夹
if(image==1)
{
m_strPath=GetPath(itemParent); //根据父节点获取路径名(文件夹)路径
m_strPath=m_strPath+"\\"+dlg.m_strName+".txt"; //当前文件路经
m_listFile.InsertItem(dlg.m_strName+".txt",1,1,itemParent); //树状控件加入文件对应的
//子项
HANDLE handle=CreateFile(m_strPath,GENERIC_READ|GENERIC_WRITE, //创建文件
FILE_SHARE_READ|
FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,
NULL);
SetDlgItemText(IDC_EDIT_FILE,""); //清空编辑框
CloseHandle(handle); //关闭文件句柄
}
else
{
CFileAttributeDlg dlg1; //是否创建子文件或子文件 夹
if(dlg1.DoModal()==IDOK) //创建子文件或子文件夹
{
m_strPath=GetPath(itemChild); //获取文件夹路径名
m_strPath=m_strPath+"\\"+dlg.m_strName+".txt";
m_listFile.InsertItem(dlg.m_strName+".txt",1,1,itemChild);//加入子文件/文件夹
//对应的树状控件
//子项
HANDLE handle=CreateFile(m_strPath,GENERIC_READ|GENERIC_WRITE,//创建文件
FILE_SHARE_READ|
FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,
NULL);
SetDlgItemText(IDC_EDIT_FILE,""); //清空编辑控件
CloseHandle(handle); //关闭文件句柄
}
else //不创建子文件或子文件夹
{
m_strPath=GetPath(itemParent); //获取文件夹路径名
m_strPath=m_strPath+"\\"+dlg.m_strName+".txt";
m_listFile.InsertItem(dlg.m_strName+".txt",1,1,itemParent); //加入子文件/文件夹
//对应的树状控件
//子项
HANDLE handle=CreateFile(m_strPath,GENERIC_READ|GENERIC_WRITE,//创建文件
FILE_SHARE_READ|
FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,
NULL);
SetDlgItemText(IDC_EDIT_FILE,"");//清空编辑控件
CloseHandle(handle);//关闭文件句柄
}
}
}
}
void CFileSysDlg::OnSave() //保存文件
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_strPath!="") //当前打开文件、名为空
{
m_file->Open(m_strPath,CFile::modeReadWrite,NULL); //打开文件
m_file->SetLength(0); //清空文件
CString strText="",strTemp1="",strTemp2="";
m_editFile.GetWindowText(strText); //获取编辑框字符串
m_file->SeekToBegin(); //文件指针指向文件头
while(strText.GetLength()!=0) //循环保存,至strText为空
{
strTemp1=strText.Left(200); //获取strText的左200个字符
m_file->Write(strTemp1,strTemp1.GetLength()); //把获取的字符串写入文件
strTemp2=strText.Right(strText.GetLength()-200); //获取strText200个字符以右的字符
strText=strTemp2;
}
m_file->Close(); //关闭文件
}
else
{
CEnterNameDlg dlg;
if(dlg.DoModal()==IDOK)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -