📄 mainformunit.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainFormUnit.h"
#include "FileInfoUnit.h"
#include <FileCtrl.hpp>
#include "frmSplitUnit.h"
//包含SelectDirectory声明
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
pFileCutMerge=NULL;
pInfo=NULL;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormResize(TObject *Sender)
{
ListView1->Height =this->Panel2->Height-20;
ListView1->Width =this->Panel2->Width-20;
Image1->Height =this->Panel3->Height -GroupBox1->Height-30;
Image1->Width =this->Panel3->Width-30;
GroupBox1->Width = this->Panel3->Width-30;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btnOpenPackageClick(TObject *Sender)
{
OpenPackage();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::OpenPackage()
{
//打开包
AnsiString PackageFileName;
OpenDialog1->FilterIndex =1;
if(OpenDialog1->Execute())
PackageFileName=OpenDialog1->FileName;
else
return ;
Screen->Cursor=crHourGlass;
try
{
bool ret;
if(pFileCutMerge==NULL)//是否已经创建了对象?没有则创建
ret=pFileCutMerge=new FileCutMerge(PackageFileName,false,this);
else
{
pFileCutMerge->SavePackageToDisk();
ret=pFileCutMerge->OpenPackage(PackageFileName,false);
}
if(ret==false)
{
ShowMessage("不能打开文件包:"+PackageFileName+",可能您指定了另外一种格式的文档,或此包已损坏!");
return ;
}
//获取文件包中文件数
int FileNum=pFileCutMerge->GetFileNum();
ListView1->Items->Clear();
//显示文件图标
int i;
TListItem *pListItem;
for(i=0;i<FileNum;i++)
{
pInfo=pFileCutMerge->GetFileInfo(i);
pListItem=ListView1->Items->Add();
pListItem->Caption=pInfo->FileName;
}
//设置窗体标题
this->Caption ="我的文件合并分割工具--["+PackageFileName+"]";
}
catch(String &s)
{
ShowMessage(s);
Screen->Cursor=crDefault;
}
catch(...)
{
pFileCutMerge =NULL;
Screen->Cursor=crDefault;
}
Screen->Cursor=crDefault;
}
void __fastcall TMainForm::ShowFileInfo(int num)
{
//显示指定文件的有关信息
if(pFileCutMerge==NULL)
return ;
pInfo=pFileCutMerge->GetFileInfo(num);
if(pInfo!=NULL)
{
lblName->Caption = pInfo->FileName;
lblSize->Caption =FloatToStr(pInfo->FileSize/1000.0)+"K ( "+ IntToStr(pInfo->FileSize )+" ) 字节";
lblPath->Caption =pInfo->FilePath ;
}
else
{
lblName->Caption = "";
lblSize->Caption ="";
lblPath->Caption ="" ;
}
}
__fastcall TMainForm::~TMainForm()
{
//清除有关对象
ListView1->Items->Clear ();//不加这一句会自动执行ListView1SelectItem函数,导致出错
if(pFileCutMerge!=NULL)
delete pFileCutMerge;
}
void __fastcall TMainForm::ListView1SelectItem(TObject *Sender,
TListItem *Item, bool Selected)
{
ShowFileInfo(Item->Index);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
lblName->Caption ="";
lblSize->Caption="";
lblPath->Caption="";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ExtractAll()
{
//展开包中的全部文件
//是否有有效的文件包打开
if(pFileCutMerge==NULL)
return;
if(pFileCutMerge->GetFileNum()==0)
return;
//选择一个文件夹
String Dir=SelectPath();
if(Dir.IsEmpty())
return;
Screen->Cursor=crHourGlass;
bool ret=pFileCutMerge->ExtractAllFiles(Dir,true);
Screen->Cursor=crDefault;
if(ret)
ShowMessage("所有文件已展开到文件夹:"+Dir);
else
ShowMessage("在展开文件时发生错误");
}
void __fastcall TMainForm::btnExtractAllClick(TObject *Sender)
{
ExtractAll();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ExtractFile(int num)
{
//展开单个文件,需要指明其序号
//是否有有效的文件包打开
if(pFileCutMerge==NULL)
return;
//选择一个文件夹
String Dir=SelectPath();
if(Dir.IsEmpty())
return;
bool ret=pFileCutMerge->ExtractFile(num,Dir,true);
if(ret)
ShowMessage("所选文件已展开到文件夹:"+Dir);
else
ShowMessage("在展开文件时发生错误");
}
void __fastcall TMainForm::btnExtractFileClick(TObject *Sender)
{
//没有选中则退出
if(ListView1->SelCount==0) return;
//获取选中的文件
TListItem *pItem=ListView1->Selected;
int num=pItem->Index ;
ExtractFile(num);
}
//---------------------------------------------------------------------------
String __fastcall TMainForm::SelectPath()
{
//选取文件夹,如果没选则返回空,否则,返回用户选择的路径,可以新建文件路径
String Dir;
bool ret=SelectDirectory(Dir, TSelectDirOpts() << sdAllowCreate << sdPerformCreate << sdPrompt,0);
if(ret==false)
return "";
else
return Dir;
}
void __fastcall TMainForm::AddFile()
{
//向包中增加文件
//是否有有效的文件包打开
if(pFileCutMerge==NULL)
return;
//指定要复制的文件
AnsiString SourceFile;
OpenDialog1->Options.Clear();
OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
OpenDialog1->FilterIndex=3;
if(OpenDialog1->Execute())
{
int FileNum=pFileCutMerge->GetFileNum();
bool flag=false; //用于解决向包中加入包文件本身的标志
Screen->Cursor=crHourGlass;
for(int i=0;i<OpenDialog1->Files->Count;i++)
{
//获取要加入的文件名
SourceFile=OpenDialog1->Files->Strings[i];
//向包中加入文件,不允许再加入包文件本身
if(SourceFile!=pFileCutMerge->PackageFileName)
{
//读取文件信息对象,并显示在ListView中
try
{
pFileCutMerge->AddFile(SourceFile);
if(flag==false)
pInfo=pFileCutMerge->GetFileInfo(FileNum+i);
else
pInfo=pFileCutMerge->GetFileInfo(FileNum+i-1);
TListItem *pListItem=ListView1->Items->Add();
pListItem->Caption=pInfo->FileName;
}
catch(...)
{
Screen->Cursor=crDefault;
return;
}
}
else
flag=true;
}
Screen->Cursor=crDefault;
}
}
void __fastcall TMainForm::btnAddFileClick(TObject *Sender)
{
AddFile();
}
//---------------------------------------------------------------------------
/*
void __fastcall TMainForm::DeleteFile(int num)
{
//删除指定的文件
//是否有有效的文件包打开
if(pFileCutMerge==NULL)
return;
bool ret=pFileCutMerge->DeleteAFile(num);
if(ret)
ShowMessage("所选文件已删除");
else
ShowMessage("在删除文件时发生错误");
}
*/
void __fastcall TMainForm::btnDeleteFileClick(TObject *Sender)
{
//没有选中则退出
if(ListView1->SelCount==0) return;
//获取选中的文件
TItemStates selected = TItemStates() << isSelected;
TListItem *Item = ListView1->Selected;
int num;
Screen->Cursor=crHourGlass;
while (Item)
{
num=Item->Index;
pFileCutMerge->DeleteAFile(num);
Item = ListView1->GetNextItem(Item, sdAll, selected);
ListView1->Items->Delete(num);
}
Screen->Cursor=crDefault;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NewPackage()
{
//新建一个包
AnsiString PackageFileName;
SaveDialog1->Title ="请指明一个文件名作为包文件名";
if(SaveDialog1->Execute())
{
PackageFileName=SaveDialog1->FileName ;
if(ExtractFileExt(PackageFileName).IsEmpty())
PackageFileName+=".jxl";
else
if(ExtractFileExt(PackageFileName)!=".jxl")
PackageFileName+=".jxl";
}
else
return;
if(pFileCutMerge==NULL)
pFileCutMerge=new FileCutMerge(this);
else
{
pFileCutMerge->SavePackageToDisk();
}
bool ret=pFileCutMerge->OpenPackage(PackageFileName,true);
if (ret==false)
return;
//清空文件
ListView1->Items->Clear();
//设置窗体标题:
this->Caption="我的文件合并分割工具--["+PackageFileName+"]";
}
void __fastcall TMainForm::btnNewPackageClick(TObject *Sender)
{
NewPackage();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btnSplitPackageClick(TObject *Sender)
{
//是否有有效的文件包打开
if((pFileCutMerge==NULL)||(pFileCutMerge->PackageFileName==""))
return;
TfrmSplit *pForm=new TfrmSplit(pFileCutMerge,this);
pForm->ShowModal();
delete pForm;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MergePackage()
{
//合并分割后的文件为包
//是否有有效的文件包打开
if(pFileCutMerge==NULL)
pFileCutMerge=new FileCutMerge(this);
//查找分割文件群
AnsiString MergeFile;
OpenDialog1->FilterIndex =2;
if(OpenDialog1->Execute())
{
String ext=ExtractFileExt(OpenDialog1->FileName);
String FileName=ExtractFileName(OpenDialog1->FileName);
int loc=FileName.Pos(ext);
FileName.Delete(loc,ext.Length());
String path=ExtractFilePath(OpenDialog1->FileName);
MergeFile=path+FileName;
}
else
return;
Screen->Cursor=crHourGlass;
bool ret=pFileCutMerge->MergeFile(MergeFile);
Screen->Cursor=crDefault;
if(ret)
ShowMessage("成功合并文件包,保存在"+MergeFile+".jxl");
else
ShowMessage("在合并文件时发生错误");
}
void __fastcall TMainForm::btnMergePackageClick(TObject *Sender)
{
MergePackage();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -