📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
if(NMFTP1->Connected)
{
NMFTP1->Disconnect();
ListBox1->Clear();
}
else
{
if(this->CheckBox1->Checked)
{
this->NMFTP1->Proxy = this->PServer->Text;
this->NMFTP1->ProxyPort = StrToInt(PPort->Text);
}
Form1->NMFTP1->Host = HostTxt->Text;
Form1->NMFTP1->Port = StrToInt(PortTxt->Text);
// Form1->NMFTP1->TimeOut = 1000;
Form1->NMFTP1->UserID = UserTxt->Text;
Form1->NMFTP1->Password = PassTxt->Text;
Form1->NMFTP1->Connect();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ListBox1->Clear();
NMFTP1->List();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
int i ;
String sPath;
String sTemp;
for (i = 0; i <= ListBox1->Items->Count - 1; i++)
{
sPath = ListBox1->Items->Strings[i] ;
sTemp = sPath;
sTemp.Delete(1, sTemp.Length() - 1);
// 判断所选择的是否为目录 (..) 或 (/)?
if ((sPath == "..") || (sTemp == "/"))
{
ListBox1->Selected[i] = false ;
}
if (ListBox1->Selected[i])
{
// 执行下载(Download)功能Download(<remote file>, <local file>) ;
NMFTP1->Download(sPath, LocalPath + sPath);
// 刷新本机 (local) 端目录内容
FileListBox1->Update() ;
ListBox1->Selected[i] = false ;
}
}
StatusBar1->SimpleText = "Status: File Download Complete." ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
int i ;
AnsiString sFileName;
this->UploadDir->Text = RemotePath;
this->UploadDir->Update();
if(RemotePath != "")
{
for (i = 0; i <= FileListBox1->Items->Count - 1; i++)
{
if (FileListBox1->Selected[i])
{
sFileName = FileListBox1->FileName;
sFileName.Delete(1, LocalPath.Length());
break ;
}
}
// 执行上传(Upload)功能 Upload(<local file>, <remote file> )
NMFTP1->Upload(FileListBox1->FileName, sFileName);
ListBox1->Clear() ;
// 执行 FTP Remote 端目录浏览(LIST)功能
NMFTP1->List() ;
StatusBar1->SimpleText = "Status: File Upload Complete." ;
// 刷新本机 (local) 端目录内容
FileListBox1->Update();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMFTP1Connect(TObject *Sender)
{
StatusBar1->SimpleText = "FTP Site Connected.";
this->BitBtn5->Caption = "断开连接";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMFTP1ListItem(AnsiString Listing)
{
AnsiString sList,sTemp;
sList = Trim((const AnsiString)StrRScan(Listing.c_str(),' '));
sTemp = "<DIR>";
if(StrPos(Listing.c_str(),sTemp.c_str()))
{
ListBox1->Items->Add(sList + "/");
}
else
{
ListBox1->Items->Add(sList);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMFTP1PacketRecvd(TObject *Sender)
{
StatusBar1->SimpleText= "目前已下载"+IntToStr(NMFTP1->BytesRecvd)+
"字节(总共"+IntToStr(NMFTP1->BytesTotal)+"字节)";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMFTP1PacketSent(TObject *Sender)
{
StatusBar1->SimpleText= "目前已下载"+IntToStr(NMFTP1->BytesSent)+
"字节(总共"+IntToStr(NMFTP1->BytesTotal)+"字节)";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
this->ListBox1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
this->NMFTP1->Abort();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
this->NMFTP1->Abort();
ListBox1->Clear();
}
void __fastcall TForm1::NMFTP1ConnectionFailed(TObject *Sender)
{
this->StatusBar1->SimpleText = "Connection failed!";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DriveComboBox1Change(TObject *Sender)
{
LocalPath = AnsiString(DriveComboBox1->Drive) + ":\\";
DownloadDir->Text = LocalPath;
this->DownloadDir->Update();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DirectoryListBox1Change(TObject *Sender)
{
if((StrRScan(DirectoryListBox1->Directory.c_str(),'\\') + 1) != StrEnd(DirectoryListBox1->Directory.c_str()))
{
LocalPath = AnsiString(DirectoryListBox1->Directory) + "\\";
}
else
{
LocalPath = AnsiString(DirectoryListBox1->Directory);
}
DownloadDir->Text = LocalPath;
this->DownloadDir->Update();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FileListBox1Change(TObject *Sender)
{
this->FileListBox1->Refresh();
DownloadDir->Text = this->FileListBox1->FileName;
this->DownloadDir->Update();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1DblClick(TObject *Sender)
{
int i ;
AnsiString sPath, sTemp ;
sPath = "";
for (i = 0; i <= ListBox1->Items->Count - 1; i++)
{
if (ListBox1->Selected[i])
{
sTemp = ListBox1->Items->Strings[i];
int judge = sTemp.Pos(".");
if(judge == 0)
{
RemotePath += ListBox1->Items->Strings[i];
sPath = ListBox1->Items->Strings[i];
}
else
{
RemotePath = "";
sPath = "";
}
break;
}
}
if (sPath != "")
{
NMFTP1->ChangeDir(Trim(sPath)); //(sPath));
this->ListBox1->Clear();
RemotePath += AnsiString("\\");
this->UploadDir->Text = RemotePath;
this->UploadDir->Update();
// 执行 FTP Remote 端目录浏览(LIST)功能
NMFTP1->List();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMFTP1Disconnect(TObject *Sender)
{
this->StatusBar1->SimpleText = "FTP Service Closed.";
this->BitBtn5->Caption = "连接服务器";
this->BitBtn5->Update();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMFTP1Error(TComponent *Sender, WORD Errno,
AnsiString Errmsg)
{
this->StatusBar1->SimpleText = "Error" + IntToStr(Errno) + ":" + Errmsg;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMFTP1HostResolved(TComponent *Sender)
{
StatusBar1->SimpleText = "Host resolved.";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMFTP1InvalidHost(bool &Handled)
{
MessageDlg("Invalid Host", mtError, TMsgDlgButtons() << mbOK, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMFTP1Status(TComponent *Sender, AnsiString Status)
{
StatusBar1->SimpleText = Status;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMFTP1Failure(bool &Handled, TCmdType Trans_Type)
{
String sMessage ;
switch (Trans_Type)
{
case cmdChangeDir:
sMessage = "Change Directory Failed.";
break;
case cmdList:
sMessage = "List File/Directory Failed.";
break;
case cmdDownload:
sMessage = "Download File Failed.";
break;
case cmdUpload:
sMessage = "Upload File Failed.";
break;
default:
break;
}
StatusBar1->SimpleText = sMessage ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMFTP1Success(TCmdType Trans_Type)
{
String sMessage ;
switch (Trans_Type)
{
case cmdChangeDir:
sMessage = "Change Directory Success.";
break;
case cmdList:
sMessage = "List File/Directory Success.";
break;
case cmdDownload:
sMessage = "Download File Success.";
break;
case cmdUpload:
sMessage = "Upload File Success.";
break;
default:
break;
}
StatusBar1->SimpleText = sMessage ;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -