📄 ch25.htm
字号:
{
return
FtpDeleteFile(FFtpHandle, S.c_str());
}
bool __fastcall TMyFtp::DeleteFileCustom(System::AnsiString S)
{
S = *CustomToFileName(&S);
return DeleteFile(S);
}
bool __fastcall TMyFtp::GetFile(System::AnsiString FTPFile,
System::AnsiString NewFile)
{
return FtpGetFile(FFtpHandle, FTPFile.c_str(), NewFile.c_str(),
False, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);
}
bool __fastcall TMyFtp::SendFile1(System::AnsiString FTPFile,
System::AnsiString NewFile)
{
DWORD Size = 3000;
AnsiString S;
BOOL Transfer = FtpPutFile(FFtpHandle,
FTPFile.c_str(),
NewFile.c_str(),
FTP_TRANSFER_TYPE_BINARY, 0);
if (!Transfer)
{
int Error = GetLastError();
S = Format("Error Number: %d. Hex: %x",
OPENARRAY(TVarRec, (Error, Error)));
ShowMessage(S);
S.SetLength(Size);
if
(!InternetGetLastResponseInfo(&(DWORD)Error, S.c_str(), &Size))
{
Error = GetLastError();
ShowMessage(Format("Error Number: %d. Hex: %x",
OPENARRAY(TVarRec, (Error, Error))));
}
ShowMessage(Format("Error Number: %d. Hex: %x Info: %s",
OPENARRAY(TVarRec, (Error, Error, S))));
}
else
ShowMessage("Success");
return Transfer;
}
bool __fastcall
TMyFtp::SendFile2(System::AnsiString FTPFile,
System::AnsiString NewFile)
{
HINTERNET FHandle;
FHandle = FtpOpenFile(FFtpHandle, "sam.txt", GENERIC_READ,
FTP_TRANSFER_TYPE_BINARY, 0);
if (FHandle !=
NULL)
InternetCloseHandle(FHandle);
else
ShowMessage("Failed");
return TRUE;
}
namespace Ftp2
{
void __fastcall Register()
{
TComponentClass classes[1] = {__classid(TMyFtp)};
RegisterComponents("Unleash", classes, 0);
}
}
</FONT></PRE>
<P><A NAME="Heading25"></A><FONT COLOR="#000077"><B>Listing 25.3. Header for the
main module of the FTPWININET program showing how to use the FTP component.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////
// File: Main.h
// Project: FtpWinINet
// copyright (c) 1997 by Charlie Calvert
//
#ifndef MainH
#define MainH
#include <vcl\Classes.hpp>
#include
<vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include <vcl\ExtCtrls.hpp>
#include <vcl\Buttons.hpp>
#include <vcl\Menus.hpp>
#include <vcl\Dialogs.hpp>
#include
"Ftp2.h"
class TForm1 : public TForm
{
__published:
TListBox *ListBox1;
TImage *Image1;
TSpeedButton *SpeedButton1;
TMainMenu *MainMenu1;
TMenuItem *File1;
TMenuItem *Connect1;
TMenuItem *CopyFile1;
TMenuItem
*N1;
TMenuItem *Exit1;
TMenuItem *SendToSite;
TSaveDialog *SaveDialog1;
TMenuItem *Options1;
TMenuItem *ChangeDirectory1;
TMenuItem *RemoveDirectory1;
TMenuItem *CreateDirectory1;
TMenuItem *DeleteFile1;
TMenuItem
*EditConnectionData1;
TMyFtp *Ftp;
void __fastcall ConnectBtnClick(TObject *Sender);
void __fastcall ListBox1DblClick(TObject *Sender);
void __fastcall BackOneDirectoryBtnClick(TObject *Sender);
void __fastcall CopyFileClick(TObject
*Sender);
// void __fastcall NewDirectory(TObject *Sender);
void __fastcall NewDirClick(TObject *Sender);
void __fastcall Exit1Click(TObject *Sender);
void __fastcall SendToSiteClick(TObject *Sender);
void __fastcall
ChangeDirectory1Click(TObject *Sender);
void __fastcall RemoveDirectory1Click(TObject *Sender);
void __fastcall CreateDirectory1Click(TObject *Sender);
void __fastcall DeleteFile1Click(TObject *Sender);
void __fastcall
ListBox1DrawItem(TWinControl *Control, int Index,
const TRect &Rect, TOwnerDrawState State);
void __fastcall EditConnectionData1Click(TObject *Sender);
void __fastcall ListBox1MouseDown(TObject *Sender, TMouseButton Button,
TShiftState
Shift, int X, int Y);
private:
// TMyFtp *Ftp;
AnsiString FStartCaption;
Graphics::TBitmap *FFileBitmap;
Graphics::TBitmap *FFolderBitmap;
virtual __fastcall ~TForm1(void);
void ActivateMenus();
public:
virtual __fastcall
TForm1(TComponent* Owner);
};
extern TForm1 *Form1;
#endif
</FONT></PRE>
<P><A NAME="Heading26"></A><FONT COLOR="#000077"><B>Listing 25.4. The main module
of the FTPWININET program showing how to use the FTP component.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////
// File: MAIN.CPP
// Project: FtpWinINet
// copyright (c) 1997 by Charlie Calvert
//
#include <vcl\vcl.h>
#pragma hdrstop
#include "Ftp2.h"
#include
"Main.h"
#include "FtpNames1.h"
#pragma link "Ftp2"
#pragma resource "*.dfm"
#pragma resource "listicon.res"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
FStartCaption = Caption;
FFolderBitmap = new Graphics::TBitmap;
FFileBitmap = new Graphics::TBitmap;
FFolderBitmap->Handle = LoadBitmap((HINSTANCE)HInstance, "FolderBmp");
FFileBitmap->Handle =
LoadBitmap((HINSTANCE)HInstance, "FileBmp");
}
__fastcall TForm1::~TForm1()
{
FFolderBitmap->Free();
FFileBitmap->Free();
}
void TForm1::ActivateMenus()
{
int i;
for (i = 0; i < ComponentCount; i++)
{
if(dynamic_cast<TMenuItem *>(Components[i]))
dynamic_cast<TMenuItem *>(Components[i])->Enabled = True;
}
}
void __fastcall TForm1::ConnectBtnClick(TObject *Sender)
{
Ftp->OnNewDir = NewDirClick;
if
(FTPNames->GetConnectionData())
{
Application->ProcessMessages();
Ftp->Server = FTPNames->Server;
Ftp->UserID = FTPNames->UserID;
Ftp->Password = FTPNames->Password;
Screen->Cursor =
TCursor(crHourGlass);
Ftp->Connect();
Screen->Cursor = TCursor(crDefault);
ActivateMenus();
}
}
void __fastcall TForm1::ListBox1DblClick(TObject *Sender)
{
int i = ListBox1->ItemIndex;
AnsiString S =
ListBox1->Items->Strings[i];
Ftp->ChangeDirCustom(&S);
}
void __fastcall TForm1::BackOneDirectoryBtnClick(TObject *Sender)
{
Screen->Cursor = Controls::TCursor(crHourGlass);
Ftp->BackOneDir();
Screen->Cursor =
Controls::TCursor(crDefault);
}
void __fastcall TForm1::CopyFileClick(TObject *Sender)
{
AnsiString S = ListBox1->Items->Strings[ListBox1->ItemIndex];
Ftp->CustomToFileName(&S);
SaveDialog1->FileName = "C:\\"
+ S;
if (SaveDialog1->Execute())
Ftp->GetFile(S, SaveDialog1->FileName);
}
void __fastcall TForm1::NewDirClick(TObject *Sender)
{
Caption = FStartCaption + " ->> " + Ftp->CurDir;
ListBox1->Items =
Ftp->FindFiles();
}
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
Close();
}
void __fastcall TForm1::SendToSiteClick(TObject *Sender)
{
if (SaveDialog1->Execute())
{
AnsiString
SaveFile(ExtractFileName(SaveDialog1->FileName));
Ftp->SendFile1(SaveDialog1->FileName, SaveFile);
}
}
void __fastcall TForm1::ChangeDirectory1Click(TObject *Sender)
{
AnsiString S;
if (InputQuery("Change
Directory", "Enter Directory", S))
Ftp->ChangeDir(&S);
}
void __fastcall TForm1::RemoveDirectory1Click(TObject *Sender)
{
AnsiString Title("Delete Directory?");
AnsiString S =
ListBox1->Items->Strings[ListBox1->ItemIndex];
S = *Ftp->CustomToFileName(&S);
if (MessageBox((HWND)Handle, S.c_str(), Title.c_str(),
MB_YESNO | MB_ICONQUESTION) == ID_YES)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -