📄 main.cpp
字号:
{
if ( ZipBuilder1->ErrCode )
ShowMessage( IntToStr( ZipBuilder1->ErrCode ) + " " + ZipBuilder1->Message );
else
ShowMessage( "No last error present" );
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Exit1Click( TObject *Sender )
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Zipcomment1Click( TObject *Sender )
{
if ( ZipBuilder1->ZipComment != "" ) {
MsgForm->RichEdit1->Clear();
MsgForm->RichEdit1->Lines->Add( ZipBuilder1->ZipComment );
MsgForm->Show();
} else
ShowMessage( "No Zip comment in this zip file" );
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::DLLversioninfo1Click( TObject *Sender )
{
ShowMessage(ZipBuilder1->FullVersionString());
}
//***********************ZipBuilder Event handling***************************
//---------------------------------------------------------------------------
void __fastcall TMainForm::ZipBuilder1Message( TObject *Sender, int ErrCode, String Message )
{
MsgForm->RichEdit1->Lines->Append( Message );
::PostMessage( MsgForm->RichEdit1->Handle, EM_SCROLLCARET, 0L, 0L );
Application->ProcessMessages();
if ( ErrCode > 0 && !ZipBuilder1->Unattended ) ShowMessage( "Error Msg: " + Message );
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ZipBuilder1DirUpdate( TObject *Sender )
{
FillGrid();
FilesLabel->Caption = IntToStr( ZipBuilder1->Count );
if ( UpperCase( ExtractFileExt( ZipBuilder1->ZipFilename ) ) == ".EXE" )
ConvertBut->Caption = "Convert to ZIP";
else
ConvertBut->Caption = "Convert to EXE";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ZipBuilder1Progress(TObject *Sender, ProgressType ProgrType,
String Filename, unsigned FileSize)
{
static long TotalSize1, TotalProgress1, TotalSize2, TotalProgress2;
int Step;
switch ( ProgrType ) {
case TotalSize2Process:
// ZipBuilder1Message( this, 0, "in OnProgress type TotalBytes, size= " + IntToStr( FileSize ) );
MsgForm->StatusBar1->Panels->Items[0]->Text = "Total size: " + IntToStr( (int)FileSize / 1024 ) + " Kb";
MsgForm->ProgressBar2->Position = 1;
TotalSize2 = FileSize;
TotalProgress2 = 0;
break;
case TotalFiles2Process:
// ZipBuilder1Message( this, 0, "in OnProgress type TotalFiles, files= " + IntToStr( FileSize ) );
MsgForm->StatusBar1->Panels->Items[1]->Text = IntToStr( (int)FileSize ) + " files";
break;
case NewFile:
// ZipBuilder1Message( this, 0, "in OnProgress type NewFile, size= " + IntToStr( FileSize ) );
MsgForm->FileBeingZipped->Caption = Filename;
MsgForm->ProgressBar1->Position = 1; // Current position of bar.
TotalSize1 = FileSize;
TotalProgress1 = 0;
break;
case ProgressUpdate:
// ZipBuilder1Message( this, 0, "in OnProgress type Update, size= " + IntToStr( FileSize ) );
// FileSize gives now the bytes processed since the last call.
TotalProgress1 += FileSize;
TotalProgress2 += FileSize;
if ( TotalSize1 ) {
Step = (int)( (unsigned __int64)TotalProgress1 * 10000ui64 / (unsigned __int64)TotalSize1 );
MsgForm->ProgressBar1->Position = (PGBCONV)(1 + Step);
} else MsgForm->ProgressBar1->Position = 10001;
if ( TotalSize2 ) {
Step = (int)( (unsigned __int64)TotalProgress2 * 10000ui64 / (unsigned __int64)TotalSize2 );
MsgForm->ProgressBar2->Position = (PGBCONV)(1 + Step);
}
break;
case EndOfBatch:
// Reset the progress bar and filename.
// ZipBuilder1Message( this, 0, "in OnProgress type EndOfBatch" );
MsgForm->FileBeingZipped->Caption = "";
MsgForm->ProgressBar1->Position = 1;
MsgForm->StatusBar1->Panels->Items[0]->Text = "";
MsgForm->StatusBar1->Panels->Items[1]->Text = "";
MsgForm->ProgressBar2->Position = 1;
}
}
//***********************User defined functions *****************************
//---------------------------------------------------------------------------
String TMainForm::ShowLTime( long s, long f )
{
String smin, ssec; // ,smil
int st = f - s;
//int mil = st % 1000;
//mil %= 60;
int sec = st / 1000;
int min = sec / 60;
sec %= 60;
//if (mil > 9) smil = inttostr(mil); else smil = "0" + IntToStr(mil);
ssec = ( sec > 9 ) ? IntToStr( sec ) : "0" + IntToStr( sec );
smin = ( min > 9 ) ? IntToStr( min ) : "0" + IntToStr( min );
return smin + ":" + ssec;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::SetZipFName( String aCaption, bool AssignName )
{
// Assigning the filename will cause the table of contents to be read.
// and possibly reset it to an empty string (If error found).
if ( AssignName ) ZipBuilder1->ZipFilename = aCaption;
ZipFName->Caption = (ZipBuilder1->ZipFilename == "") ? AnsiString( "<none>" ) :
MinimizeName( ZipBuilder1->ZipFilename, ZipFName->Canvas, ZipFName->Width );
if ( ZipFName->Canvas->TextWidth( ZipBuilder1->ZipFilename ) > ZipFName->Width ) {
ZipFName->Hint = ZipBuilder1->ZipFilename;
ZipFName->ShowHint = true;
} else ZipFName->ShowHint = false;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::SetZipTotals( void )
{
StringGrid1->Cells[0][ZipBuilder1->Count + 1] = "Total";
StringGrid1->Cells[1][ZipBuilder1->Count + 1] = IntToStr( (unsigned int)TotComp );
StringGrid1->Cells[2][ZipBuilder1->Count + 1] = IntToStr( (unsigned int)TotUncomp );
unsigned __int64 cs = TotComp, us = TotUncomp;
if ( us )
StringGrid1->Cells[4][ZipBuilder1->Count + 1] = IntToStr( (unsigned int)(100 - cs * 100 / us - ((cs * 100 % us) >= us / 2) ) ) + " % ";
else
StringGrid1->Cells[4][ZipBuilder1->Count + 1] = "0 % ";
StringGrid1->Cells[5][ZipBuilder1->Count + 1] = "";
}
//---------------------------------------------------------------------------
bool __fastcall TMainForm::AskDirDialog( const HWND FormHandle, String &DirPath )
{
BROWSEINFO bi;
LPSTR lpBuffer;
ITEMIDLIST *pidl; // PIDL selected by user
LPMALLOC g_pMalloc;
bool Result = false;
// Get the shell's allocator.
if ( (SHGetMalloc( &g_pMalloc ) )== E_FAIL ) return false;
// Allocate a buffer to receive browse information.
if ( (lpBuffer = (LPSTR)g_pMalloc->Alloc( MAX_PATH)) != 0 ) {
bi.hwndOwner = FormHandle;
bi.pidlRoot = NULL;
bi.pszDisplayName = lpBuffer;
bi.lpszTitle = "";
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.lParam = 0;
// if pidl = 0 then cancel is used.
if ( (pidl = SHBrowseForFolder( &bi )) != 0 ) {
// if 0 then pidl not part of namespace
if ( SHGetPathFromIDList( pidl, lpBuffer ) ) {
DirPath = lpBuffer;
if ( DirPath[DirPath.Length()] != '\\' ) DirPath += '\\';
Result = true;
}
// Free the PIDL returned by SHBrowseForFolder.
g_pMalloc->Free( pidl );
}
// Free the string buffer used for the name
g_pMalloc->Free( lpBuffer );
}
// Release the shell's allocator.
g_pMalloc->Release();
return Result;
}
//---------------------------------------------------------------------------
/* Folder types are a.o.
* CSIDL_DESKTOPDIRECTORY, CSIDL_STARTMENU, CSIDL_SENDTO,
* CSIDL_PROGRAMS, CSIDL_STARTUP etc.
*/
long __fastcall TMainForm::GetSpecialFolder( int aFolder, String &Location )
{
long FolderErr = 0; // No error.
char *RealPath;
ITEMIDLIST *pidl;
LPMALLOC g_pMalloc;
// Get the shell's allocator.
if ( ::SHGetMalloc( &g_pMalloc ) == E_FAIL ) return E_FAIL;
// Allocate a buffer to receive the path information.
if ( (RealPath = (char *)g_pMalloc->Alloc( MAX_PATH )) != NULL ) {
HRESULT hRes = ::SHGetSpecialFolderLocation( Handle, aFolder, &pidl );
if ( hRes == NOERROR ) {
bool Success = ::SHGetPathFromIDList( pidl, RealPath );
if ( Success ) {
Location = RealPath;
Location += "\\";
} else FolderErr = E_UNEXPECTED;
// Free the PIDL allocated by SHGetSpecialFolderLocation.
g_pMalloc->Free( pidl );
} else FolderErr = hRes;
// Free the string buffer used for the name
g_pMalloc->Free( RealPath );
} else FolderErr = E_OUTOFMEMORY;
// Release the shell's allocator.
g_pMalloc->Release();
return FolderErr;
}
//**************************Grid functions **********************************
//---------------------------------------------------------------------------
void __fastcall TMainForm::FillGrid( void )
{
// Remove everything from grid except col titles.
StringGrid1->RowCount = 2;
ClearGrid();
if(!ZipBuilder1->Count) return;
StringGrid1->RowCount = ZipBuilder1->Count + 2;
TotUncomp = TotComp = 0;
for(int i = 1; i <= ZipBuilder1->Count; i++)
{
ZipDirEntry *zde = (ZipDirEntry *)ZipBuilder1->ZipContents->Items[i - 1];
StringGrid1->Cells[0][i] = ExtractFileName( zde->FileName );
StringGrid1->Cells[1][i] = IntToStr( (int)zde->CompressedSize );
StringGrid1->Cells[2][i] = IntToStr( (int)zde->UncompressedSize );
StringGrid1->Cells[3][i] = FormatDateTime( "ddddd t", FileDateToDateTime(zde->DateTime));
unsigned __int64 cs = zde->CompressedSize, us = zde->UncompressedSize;
if(us)
StringGrid1->Cells[4][i] = IntToStr((unsigned int)(100 - cs * 100 / us - ((cs * 100 % us) >= us / 2))) + "% ";
else
StringGrid1->Cells[4][i] = "0% ";
StringGrid1->Cells[5][i] = ExtractFilePath(zde->FileName);
StringGrid1->Cells[6][i] = "N"; // means not selected
TotUncomp += us;
TotComp += cs;
}
SortGrid();
SetZipTotals();
StringGrid1->Row = 1;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::RenButClick( TObject *Sender )
{
RenForm->Show();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
ZipBuilder1->Unload_Zip_Dll();
ZipBuilder1->Unload_Unz_Dll();
Action = caFree;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClearGrid(void)
{
for(int i = 1; i < StringGrid1->RowCount -1; ++i)
{ // we keep frist and last row
StringGrid1->Rows[i]->Clear();
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::SortGrid(void)
{
TStringList* List = new TStringList();
List->Sorted = true;
for(int i = 1; i < StringGrid1->RowCount - 1; ++i)
List->Add(StringGrid1->Rows[i]->Text);
ClearGrid();
for(int i = 0; i < List->Count; ++i)
StringGrid1->Rows[i+1]->Text = List->Strings[i];
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
UINT Format;
if (State.Contains(gdFixed)) // for fixed cells
{
StringGrid1->Canvas->Brush->Color = clBtnFace;
StringGrid1->Canvas->Font->Name = "Arial Black";
StringGrid1->Canvas->Font->Size = 9;
StringGrid1->Canvas->Font->Color = clBlack;
StringGrid1->Canvas->FillRect(Rect);
Frame3D(StringGrid1->Canvas, Rect, clBtnHighlight, clBtnShadow,1);
}
else // for normal cells
{
if(StringGrid1->Cells[0][ARow] != "Total")
{
StringGrid1->Canvas->Brush->Color = (StringGrid1->Cells[6][ARow] == "Y") ? clHighlight : clWindow;
StringGrid1->Canvas->Font = StringGrid1->Font;
StringGrid1->Canvas->Font->Color = (StringGrid1->Cells[6][ARow] == "Y") ? clHighlightText :clBlack;
StringGrid1->Canvas->FillRect(Rect);
}
else //total row
{
StringGrid1->Canvas->Brush->Color = clAqua;
StringGrid1->Canvas->Font = StringGrid1->Font;
StringGrid1->Canvas->Font->Color = clRed;
StringGrid1->Canvas->FillRect(Rect);
}
}
switch(ACol)
{
case 0:
case 5:
Format = DT_LEFT;
break;
default:
Format = DT_RIGHT;
break;
}
RECT R = RECT(Rect);
char* text = StringGrid1->Cells[ACol][ARow].c_str();
DrawText(StringGrid1->Canvas->Handle, text, StrLen(text), &R, Format);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::StringGrid1SelectCell(TObject *Sender, int ACol,
int ARow, bool &CanSelect)
{
if((ARow != 0) && (ARow != StringGrid1->RowCount)) //1st and total not allowed
{ // Select or deselect cell
StringGrid1->Cells[6][ARow] = (StringGrid1->Cells[6][ARow]== "N") ? "Y" : "N";
}
for(int i = 0; i < StringGrid1->ColCount; ++i) //forces repaint of whole row
StringGrid1->Cells[i][ARow]= StringGrid1->Cells[i][ARow];
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -