⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesysdlg.cpp

📁 用java写的操作系统实验 也包括实验报告 用java写的操作系统实验 也包括实验报告
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		{
			CString strPath="C:\\temp\\"+dlg.m_strName+".txt";
			HANDLE handle=CreateFile(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);
			m_file->Open(strPath,CFile::modeReadWrite,NULL);
			CString strText="",strTemp1="",strTemp2="";
			GetDlgItemText(IDC_EDIT_FILE,strText);
 			while(strText.GetLength()!=0)                                  //保
			{                                                              //存
				strTemp1=strText.Left(200);                                //打
				m_file->Write(strTemp1,200);                               //开
				strTemp2=strText.Right(strText.GetLength()-200);            //文    
				strText=strTemp2;                                           //见
			}
			m_file->Close();
		}		
	}
}

CString CFileSysDlg::GetPath(HTREEITEM childItem)  //获取路径名
{
	if(childItem==NULL)
		return "C:\\temp";
	HTREEITEM parent;
	HTREEITEM child=childItem;
	parent=m_listFile.GetParentItem(child);  //获取childItem子项的父节点
	CString strName,strDir;
	strName=m_listFile.GetItemText(child);   //m_listFile.GetItemText获取childItem子项的
	                                           //文本名
	while(parent!=NULL)
	{
		strDir=m_listFile.GetItemText(parent);//m_listFile.GetItemText获取childItem子项的
	                                           //文本名
		strDir=strDir+"\\"+strName;              //路径名+"\\"+strName(子项的文本名)
		strName=strDir;
		child=parent;
		parent=m_listFile.GetParentItem(child);//m_listFile.GetParentItem获取child节点
		                                          //的父节点
	}
	strDir="C:\\temp\\"+strName;   //路径名
	return strDir;
}

	void CFileSysDlg::OnMenu(LPARAM lParam,WPARAM wParam)  //响应右键菜单消息
{
	switch(lParam)
	{
	case ID_FILE_EDIT:           //编辑
		{
			HTREEITEM itemChild=m_listFile.GetSelectedItem();
			int image,image1;
			m_listFile.GetItemImage(itemChild,image,image1);
			if(image==1)
				{
				if(m_listFile.GetChildItem(itemChild)==NULL)
				{
					m_strPath=GetPath(itemChild);
					m_file->Open(m_strPath,CFile::modeReadWrite,NULL);
					DWORD cur,end,begin;
					m_file->SeekToBegin();
					cur=begin=m_file->GetPosition();
					m_file->SeekToEnd();
					end=m_file->GetPosition();
					m_file->SeekToBegin();
					char buff[201];
					CString strText;
					SetDlgItemText(IDC_EDIT_FILE,"");
					int nRead;
					while(cur<end)                                          //读
					{                                                        //取
						nRead=m_file->Read(buff,200);                        //
						buff[nRead]='\0';                                   //
						GetDlgItemText(IDC_EDIT_FILE,strText);              //
						strText+=buff;                                      //文
						SetDlgItemText(IDC_EDIT_FILE,strText);              //件
						cur=m_file->GetPosition();
						memset(buff,' ',201);
					}
					m_file->Close();
				}
				else
				{
					AfxMessageBox("请选择文件",MB_OK);
				}
			}
		}
		break;
	case ID_FILE_NEW_FILE: //新建文件
		{
			if(m_strPath!="")
			{
				m_file->Open(m_strPath,CFile::modeReadWrite,NULL);
				CString strText="",strTemp1="",strTemp2="";
				GetDlgItemText(IDC_EDIT_FILE,strText);
 				while(strText.GetLength()!=0)                                  //保
				{                                                              //存
					strTemp1=strText.Left(200);                                //打
					m_file->Write(strTemp1,200);                               //开
					strTemp2=strText.Right(strText.GetLength()-200);            //文    
					strText=strTemp2;                                           //见
				}
				m_file->Close();
			}
			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);
				}
				break;
			}
			itemParent=m_listFile.GetParentItem(itemChild);
			CEnterNameDlg dlg;                              //输入文件名对话框
			if(dlg.DoModal()==IDOK)
			{
				int image,image1;
				m_listFile.GetItemImage(itemChild,image,image1);
				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);
					}
				}
			}
		}
		break;

	case ID_FILE_CHANGE:  //更名
		{
			HTREEITEM itemChild,itemParent;
			itemChild=m_listFile.GetSelectedItem();
			if(itemChild==NULL)
			{
				AfxMessageBox("请选择文件或文件夹");
				break;
			}
			itemParent=m_listFile.GetParentItem(itemChild);
			CString strOldPath=GetPath(itemChild);
			CString strPath=GetPath(itemParent);
			int image,image1;
			m_listFile.GetItemImage(itemChild,image,image1);
			CEnterNameDlg dlg;              //输入新文件名对话框
			if(dlg.DoModal()==IDOK)
			{
				if(image==1)//是文件
				{
					CString strTempName=m_listFile.GetItemText(itemChild);
					int index=strTempName.Find(".",0);
					CString strTemp=strTempName.Right(strTempName.GetLength()-index);
					CString strTemp1=strTempName.Left(index);
					if(strTemp1==dlg.m_strName)
					{
						AfxMessageBox("文件名相同,改名失败");
						break;
					}
					strPath=strPath+"\\"+dlg.m_strName+strTemp;
					try
					{
						CFile::Rename(strOldPath,strPath); //文件更名
					}
					catch(CFileException e)
					{
						AfxMessageBox(e.m_cause);
					}
					m_listFile.SetItemText(itemChild,dlg.m_strName+strTemp);
				}
				else //文件夹
				{
					if(m_listFile.GetItemText(itemChild)==(dlg.m_strName))
					{
						AfxMessageBox("文件夹名相同,改名失败");
					}
					strPath=strPath+"\\"+dlg.m_strName;
					RenameDir(strOldPath,strPath);          //修改文件夹名
					m_listFile.SetItemText(itemChild,dlg.m_strName); //更改对应的树状控件
					                                                 //子项
				}
			}
		}
		break;
	case ID_FILE_NEW_CATALOG:         //新建文件夹
		{
			HTREEITEM itemParent,itemChild;
			itemChild=m_listFile.GetSelectedItem();
			itemParent=m_listFile.GetParentItem(itemChild);
			CEnterNameDlg dlg;
			if(dlg.DoModal()==IDOK)
			{
				int image,image1;
				m_listFile.GetItemImage(itemChild,image,image1);
				if(image==0)
				{
					CFileAttributeDlg dlg1;
					if(dlg1.DoModal()==IDOK)
					{
						CString tempPath=GetPath(itemChild)+"\\"+dlg.m_strName;
						if(!CreateDirectory(tempPath,NULL))
						{
							AfxMessageBox("创建子文件夹失败");
						}
						else
						{
							m_listFile.InsertItem(dlg.m_strName,0,0,itemChild);
						}
					}
					else
					{
						CString tempPath=GetPath(itemParent)+"\\"+dlg.m_strName;
						if(!CreateDirectory(tempPath,NULL))
						{
							AfxMessageBox("创建文件夹失败");
						}
						else
						{
							m_listFile.InsertItem(dlg.m_strName,0,0,itemParent);
						}
					}
				}
				else
				{
						CString tempPath=GetPath(itemParent)+"\\"+dlg.m_strName;
						if(!CreateDirectory(tempPath,NULL))
						{
							AfxMessageBox("创建文件夹失败");
						}
						else
						{
							m_listFile.InsertItem(dlg.m_strName,0,0,itemParent);
						}
				}
	
			}
		}
		break;
	case ID_FILE_DELETE:  //删除文件文件夹
		{
			HTREEITEM itemChild;
			itemChild=m_listFile.GetSelectedItem();
			if(itemChild==NULL)
			{
				AfxMessageBox("请选择文件或文件夹");
				break;
			}
			CString strPath=GetPath(itemChild);
			int image,image1;
			m_listFile.GetItemImage(itemChild,image,image1);
			if(image==0)//是文件夹
			{
				DeleteDir(strPath);//删除文件夹
			}
			else
			{
				DeleteFile(strPath);  //删除文件
				SetDlgItemText(IDC_EDIT_FILE,"");
			}
			m_listFile.DeleteItem(itemChild);//删除树状控件对应子项
			m_strPath="";

		}
		break;
	}
}


void CFileSysDlg::DeleteDir(CString strPath)  //删除文件,文件夹
{
	CFileFind file;       //文件查找类
	CString tempPath=strPath+"\\*.*";
	int nRet=file.FindFile(tempPath);  //初始化,成功返回非0值
	while(nRet)
	{
		nRet=file.FindNextFile();  //查找下一个文件,不到结尾返回非0值
		if(!file.IsDots())    
		{
			if(file.IsDirectory()) //是文件夹
			{
				CString tempPath1=strPath+"\\"+file.GetFileName();
				DeleteDir(tempPath1);   //递归调用
			
			}
			else  //是文件
			{
				CString tempFile=strPath+"\\"+file.GetFileName();
				DeleteFile(tempFile); //删除文件
			}
		}
	}
	file.Close();
	RemoveDirectory(strPath);  //删除文件夹
}



void CFileSysDlg::RenameDir(CString strOldDir, CString strNewDir)   //更改文件夹名
{
	CFileFind find;          //文件查找类
	CString strTempDir=strOldDir+"\\*.*";
	if(strNewDir==strOldDir)
	{
		AfxMessageBox("文件夹同名,更改失败 ");
		return ;
	}
	int nRet=find.FindFile(strTempDir);        //初始化,成功返回非0值
	CreateDirectory(strNewDir,NULL);             //创建新文件夹
	while(nRet)
	{
		nRet=find.FindNextFile();           //查找下一个文件,不到结尾返回非0值
		if(!find.IsDots())
		{
			if(find.IsDirectory())  //是文件夹
			{
				CString strNewDir1=strNewDir+"\\"+find.GetFileName();
				CString strOldDir1=strOldDir+"\\"+find.GetFileName();
				RenameDir(strOldDir1,strNewDir1); //递归调用
			}
			else  //是文件
			{
				CString strNewDir1=strNewDir+"\\"+find.GetFileName();
				CString strOldDir1=strOldDir+"\\"+find.GetFileName();
				try
				{
					CFile::Rename(strOldDir1,strNewDir1);  //重命名文件
				}
				catch(CFileException e)
				{
					AfxMessageBox(e.m_cause);
				}
			}
		}
	}
	find.Close();
	RemoveDirectory(strOldDir);   //删除文件夹
}

void CFileSysDlg::OnContextMenu(CWnd* pWnd, CPoint point) //响应WM_CONTEXTMENU消息
                                                             //在树状控件的右键控件消息中发出
															 //的消息
{
	// TODO: Add your message handler code here
	CMenu menu;
	menu.LoadMenu(IDR_MENU_FILE);
	m_menu=menu.GetSubMenu(0);

	m_menu->TrackPopupMenu(TPM_LEFTALIGN |TPM_LEFTBUTTON,//弹出菜单
		point.x,point.y,AfxGetApp()->GetMainWnd());
	m_menu->DestroyMenu();	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -