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

📄 mainform.~cpp

📁 文件管理系统
💻 ~CPP
📖 第 1 页 / 共 4 页
字号:

void __fastcall TForm2::ListViewClick(TObject *Sender)
{

  TListItem *Item;
  String name;
  Item=ListView1->Selected;
  if(Item&&Item->ImageIndex==0){  //双击目录
  name=Item->Caption;
  CurrDir=LocateDir(CurrDir,name);
  ShowDir(CurrDir);
  FilePath+=CurrDir->name+"\\";
  ComboBox1->Text=FilePath;
  CurrNode->Expand(false);
  int i=0;
  while(CurrNode->Item[i]->Text!=name)i++;
  CurrNode=CurrNode->Item[i];
  CurrNode->Selected=true;
  }
  else if(Item&&Item->ImageIndex==1){    //双击文件
  name=Item->Caption;
  FILENODE file=LocateFile(CurrDir,name);
  ////////////////////////////////////////////////////
  Form6->Sname->Caption=file->name;
  Form6->Stype->Caption=file->name.SubString(file->name.Length()-2,3)+"文件";
  Form6->Slen->Caption=IntToStr(file->length)+"KB";
  if(!file->property)Form6->Spro->Caption="只读";//属性
          else Form6->Spro->Caption="允许读写";
  if(!file->share)Form6->Ssh->Caption="禁止共享";     //共享
         else Form6->Ssh->Caption="允许共享";
  Form6->Suser->Caption=file->user;
  Form6->Sdate->Caption=file->date;
   Form6->Spath->Caption=FilePath+file->name;
  Form6->ShowModal();
  //////////////////////////////////////////////////////
  }
}
//---------------------------------------------------------------------------
DIRECTORY TForm2::LocateDir(DIRECTORY dir, String name)        //定位目录
{
     DIRECTORY tempdir=dir->dirpoint;
     while (tempdir){
     if(tempdir->del==0&&tempdir->name==name)break;
     tempdir=tempdir->next;
     }
     return tempdir;
}
void __fastcall TForm2::FreeForm(TObject *Sender, TCloseAction &Action)
{
Form1->Show() ;
}
//---------------------------------------------------------------------------
FILENODE TForm2::LocateFile(DIRECTORY dir, String name)        //定位文件
{
     FILENODE file=dir->filenode;
     while (file){
     if(file->del==0&&file->name==name)break;
     file=file->next;
     }
     return file;
}

void __fastcall TForm2::LogffUserClick(TObject *Sender)
{                  //注销按钮
if(MessageDlg("确定注销:"+CurrUser+" 吗?",mtInformation,TMsgDlgButtons()
      <<mbYes<<mbNo, 0)==mrYes){
SaveDir(Dfp);
Close();
Form1->Show();
}
}
//---------------------------------------------------------------------------


void __fastcall TForm2::UpDirClick(TObject *Sender)
{                   //向上按钮
if(CurrDir->father){
CurrDir=CurrDir->father;
ShowDir(CurrDir);
CurrNode=CurrNode->Parent;
CurrNode->Selected=true;
OutPath(CurrNode);
}
}
//---------------------------------------------------------------------------

void __fastcall TForm2::JumpToClick(TObject *Sender)
{       //转到指定路径
TTreeNode *tree=TreeView1->TopItem;
int i=2,j,t,k,f=0;
String Path=ComboBox1->Text,Str;
if(Path==""){ShowMessage("请输入路径");return;}
i=Path.Pos("\\");
if(i==0){ShowMessage("缺少符号'\\'");return;}
Str=Path.SubString(1,i-1);Path=Path.Delete(1,i);
if(Str!="Root"){ShowMessage("根目录错误");return;}
while(Path!=""){
i=Path.Pos("\\");
if(i==0){ShowMessage("缺少符号'\\'");return;}
Str=Path.SubString(1,i-1);Path=Path.Delete(1,i);
t=tree->Count;
for(k=0;k<t;k++)
if(tree->Item[k]->Text==Str){
tree=tree->Item[k];f=1;break;}
if(!f){ShowMessage("路径不存在");return;}
i++;
}
tree->Selected=true;
tree->Expand(false);
CurrNode=tree;
OutPath(tree);
CurrDir=SearchDir();
ShowDir(CurrDir);
//查询过的路径不重复的加入ComboBox
if(ComboBox1->Items->IndexOf(FilePath)==-1)ComboBox1->Items->Add(FilePath);
}
//---------------------------------------------------------------------------

void __fastcall TForm2::N4Click(TObject *Sender)
{       //新建文件
if(CurrUser!="root"&&CurrUser!=CurrDir->user&&CurrDir->property==0){
ShowMessage("你无限权在此目录下创建文件");return;}
Form3->Tag=0;
Form3->EditName->Clear();
Form3->EditLength->Clear();
Form3->TypeBox->ItemIndex=0;
Form3->TypeBox->Enabled=true;
Form3->EditName->Enabled=true;
Form3->EditLength->Enabled=true;
Form3->Caption="新建文件夹";
Form3->ShowModal();
 String  type,name;
  int share=0;
  int length;

  if(Form3->Tag)
     {
       name=Form3->EditName->Text;
       length=Form3->EditLength->Text.ToInt();
       switch (Form3->TypeBox->ItemIndex)
       {case 0:type=".txt";break;
        case 1:type=".mp3";break;
        case 2:type=".avi";break;
        case 3:type=".cpp";break;
        case 4:type=".jpg";break;
        default :type=".dat";break;
        }
        if(Form3->CheckSh->Checked)share=1;
       if(CreateFile(CurrDir,name+type,CurrUser,length,Form3->Property->ItemIndex,share))
       ShowDir(CurrDir);
       }
}
//---------------------------------------------------------------------------
bool TForm2::CreateFile(DIRECTORY dir,String name,String user,int length,int property,int share)
{                                                            //创建文件
  if(SameFile(dir,name)){
        ShowMessage("该文件已存在,无法创建!"); return false;
        }
        FILENODE newfile;
        newfile=new filenode;
        newfile->user=user;
        newfile->name=name;
        newfile->date=Now();
        newfile->property=property;
        newfile->share=share;
        newfile->length=length;
        newfile->del=0;
        newfile->next=NULL;
        InsertFile(dir,newfile);
        dir->filenum++;
        return true;
}
//---------------------------------------------------------------------------

bool TForm2::SameFile(DIRECTORY dir, String name)              //同名文件
{
    FILENODE f;
    f=dir->filenode;
    while(f){
         if(f->del==0&&f->name==name)
         return true;
         f=f->next;}
    return false;
}
void __fastcall TForm2::N5Click(TObject *Sender)//新建文件夹
{
if(CurrUser!="root"&&CurrUser!=CurrDir->user&&CurrDir->property==0){
ShowMessage("你无限权在此目录下创建文件夹");return;}
Form4->Tag=0;
Form4->EditName->Clear();
Form4->EditName->Enabled=true;
Form4->Caption="新建文件夹";
Form4->ShowModal();
 String name;
  int share=0;

  if(Form4->Tag)
     {
       name=Form4->EditName->Text;
       if(Form4->CheckSh->Checked)share=1;
       if(CreateDir(CurrDir,name,CurrUser,Form4->Property->ItemIndex,share)){
       TTreeNode *t=TreeView1->Items->AddChild(CurrNode,name);
       t->SelectedIndex=2;
       CurrNode->Expand(false);
       ShowDir(CurrDir);
       }
       }
}
//---------------------------------------------------------------------------
bool TForm2::CreateDir(DIRECTORY dir,String name, String user, int property, int share)
{                                                             //创建目录
  if(SameDir(dir,name)){
        ShowMessage("该文件夹已存在,无法创建!");
        return false;
        }
        DIRECTORY newdir;
        newdir=new directory;
        newdir->name=name;
        newdir->user=user;
        newdir->date=Now();
        newdir->property=property;
        newdir->share=share;
        newdir->filenum=newdir->dirnum=0;
        newdir->del=0;
        newdir->next=NULL;
        newdir->dirpoint=NULL;
        newdir->filenode=NULL;
        newdir->father=NULL;
        InsertDir(dir,newdir);
        dir->dirnum++;
        return true;
}

bool TForm2::SameDir(DIRECTORY dir, String name)               //同名子目录
{
    DIRECTORY d;
    d=dir->dirpoint;
    while(d){
          if(d->del==0&&d->name==name)
          return true;
          d=d->next;}
    return false;
}

void __fastcall TForm2::N10Click(TObject *Sender)
{
  TListItem *Item;
  Item=ListView1->Selected;
  String Name;
   DIRECTORY tempdir;
   FILENODE tempfile;
  if(Item&&Item->ImageIndex==1)   //文件删除
    { Name=Item->Caption;
      tempfile=LocateFile(CurrDir,Name);
      if(CurrUser!="root"&&tempfile->user!=CurrUser){                      //文件删除权限
         ShowMessage("你无权限删除该文件!");
         return;
         }
      if(MessageDlg("确定删除文件"+Name+"吗?",mtInformation,TMsgDlgButtons()
      <<mbYes<<mbNo, 0)==mrYes){
      tempfile->del=1;
      CurrDir->filenum--;
      ShowDir(CurrDir);
      }
    }
    else if(Item&&Item->ImageIndex==0){     //在ListView中删除文件夹
       Name=Item->Caption;
       tempdir=LocateDir(CurrDir,Name);
        if(CurrUser!="root"&&tempdir->user!=CurrUser){                      //文件夹删除权限
         ShowMessage("你无权限删除该文件夹!");
         return;
         }
        if(tempdir->dirpoint||tempdir->filenode)ShowMessage("提示:文件夹中有内容!");
        if(MessageDlg("确定删除文件夹"+Name+"吗?",mtInformation,TMsgDlgButtons()
      <<mbYes<<mbNo, 0)==mrYes){
       DelDir(tempdir);
       ShowDir(CurrDir);
      }
         }


      else if(TreeView1->Selected){           //在TreeView中删除文件夹
           Name=CurrDir->name;
           tempdir=CurrDir;
           if(Name=="Root"){ShowMessage("根文件夹不可删除!");return;}
           if(CurrUser!="root"&&tempdir->user!=CurrUser){                      //文件夹删除权限
         ShowMessage("你无权限删除该文件夹!");
         return;
         }
         if(tempdir->dirpoint||tempdir->filenode)ShowMessage("提示:文件夹中有内容!");
        if(MessageDlg("确定删除文件夹"+Name+"吗?",mtInformation,TMsgDlgButtons()
      <<mbYes<<mbNo, 0)==mrYes){
CurrDir=CurrDir->father;
CurrNode=CurrNode->Parent;
DelDir(tempdir);
 ShowDir(CurrDir);
OutPath(CurrNode);
        }
        }

}
//---------------------------------------------------------------------------

void TForm2::DelDir(DIRECTORY  tempdir)       //删除文件夹操作
{//删除目录下所有文件
DIRECTORY dir,subdir;
FILENODE tempfile;
que.push(tempdir);
while(!que.empty())
{
dir=que.front();
que.pop();
dir->del=1;
tempfile=dir->filenode;
while(tempfile){
tempfile->del=1;
tempfile=tempfile->next;
}
subdir=dir->dirpoint;
while(subdir)     //子目录入队列
{que.push(subdir);
subdir=subdir->next;
}
}
CurrDir->dirnum--;
 int i=0;
  while(CurrNode->Item[i]->Text!=tempdir->name)i++;
  CurrNode->Item[i]->Delete();  //删除TreeView相应节点
  CurrNode->Selected=true;
}
//-------------------------------------------------------
void __fastcall TForm2::N12Click(TObject *Sender)  //重命名
{
  TListItem *Item;
  Item=ListView1->Selected;
  String Name;
   DIRECTORY tempdir;
   FILENODE tempfile;
  if(Item&&Item->ImageIndex==1)   //文件重命名
    {
      Name=Item->Caption;
      tempfile=LocateFile(CurrDir,Name);
      if(CurrUser!="root"&&tempfile->user!=CurrUser){                      //文件重命名权限
         ShowMessage("你无权限修改该文件!");
         return;
         }
         Form5->Tag=0;
         Form5->Edit1->Text=Name.SubString(1,Name.Length()-4);
         Form5->Edit1->MaxLength=12;
         Form5->ShowModal();
         if(Form5->Tag){
         String Type=tempfile->name.SubString(tempfile->name.Length()-2,3);
         Name=Form5->Edit1->Text+"."+Type;
         if(SameFile(CurrDir,Name)){
         ShowMessage("文件名重复,无法改名!");return;}
         tempfile->name=Name;
         ShowDir(CurrDir);
            }
         }
     else if(Item&&Item->ImageIndex==0){     //在ListView中重命名文件夹
       Name=Item->Caption;
       tempdir=LocateDir(CurrDir,Name);
        if(CurrUser!="root"&&tempdir->user!=CurrUser){                      //文件夹重命名权限
         ShowMessage("你无权限修改该文件夹!");
         return;
         }
         Form5->Tag=0;
         Form5->Edit1->Text=Name;
         Form5->Edit1->MaxLength=16;
         Form5->ShowModal();
         if(Form5->Tag){
         Name=Form5->Edit1->Text;
         if(SameDir(CurrDir,Name)){
         ShowMessage("文件夹名重复,无法改名!");return;}
         int i=0;
         while(CurrNode->Item[i]->Text!=tempdir->name)i++;
         CurrNode->Item[i]->Text=Name;
         tempdir->name=Name;
         ShowDir(CurrDir);
         }
         }
      else if(TreeView1->Selected){           //在TreeView中重命名文件夹
           Name=CurrDir->name;
           tempdir=CurrDir;
           if(Name=="Root"){ShowMessage("根文件夹不可重命名!");return;}
           if(CurrUser!="root"&&tempdir->user!=CurrUser){
         ShowMessage("你无权限修改该文件夹!");
         return;
         }
         Form5->Tag=0;
         Form5->Edit1->Text=Name;
         Form5->Edit1->MaxLength=16;
         Form5->ShowModal();
         if(Form5->Tag){
         Name=Form5->Edit1->Text;
         if(SameDir(CurrDir,Name)){
         ShowMessage("文件夹名重复,无法改名!");return;}
         CurrDir->name=Name;
         CurrNode->Text=Name;
        ShowDir(CurrDir);
        OutPath(CurrNode);
               }
       }
}


//---------------------------------------------------------------------------

void __fastcall TForm2::N22Click(TObject *Sender)
{                //编辑文件
  TListItem *Item;
  Item=ListView1->Selected;
  String Name;
  FILENODE tempfile;
  if(Item&&Item->ImageIndex==1)
    {
      Name=Item->Caption;
      tempfile=LocateFile(CurrDir,Name);
      if(CurrUser!="root"&&tempfile->user!=CurrUser){
         ShowMessage("你无权限编辑该文件!");return;}
      if(tempfile->property==0){

⌨️ 快捷键说明

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