小弟处学DELPHI,看到很多资料时都提到编写组件一事,所以心血来潮,参考书的资料写了下面的代码,但对于出现的错误不知道怎么解决!菜啊~不知道组件中的定义形式是怎么样的~请高手指点!谢谢!
代码如下;
unit DragDropFTP;interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;type
TDragDropFTP = class(TPanel)
private
hConnect:HInternet;
FActive:boolean;
FHostName:string;
FLogin:string;
FPassword:string;
FRemoteDir:string;
FSaveDialog:TSaveDialog;
FOnChange:TNotifyEvent;
FOnDblClick:TNotifyEvent;
procedure AddFile(lpFindFileData:TWin32FindData);
procedure Change;dynamic;
procedure doDblClick(sender:TObject);
procedure Loaded;override;
procedure setActive(Value:boolean);
procedure Log(const Text:string);
procedure WMDropFiles(var msg:TMessage);message WM_DROPFILES;
    { Private declarations }
  protected
    { Protected declarations }
public
ListView:TListView;
constructor Create(AOwner:TComponent);override;
procedure Connect;
procedure RefreshFileList;
    { Public declarations }
  published
property RemoteDir:string read FRemoteDir write FRemoteDir;
property Active:boolean read FActive write SetActive;
property HostName:string read FHostName write FHostName;
property LoginName:string read FLogin write FLogin;
property onChange:TNotifyEvent read FOnChange write FOnChange;
property onDblClick:TNotifyEvent read FOnDblClick write FOnDblClick;
property Password:string read FPassword write FPassword;
property SaveDialog:TSaveDialog read FSaveDialog write FSaveDialog;
property StatusLabel:TLabel read FLabel write FLabel;
end;
    { Published declarations }procedure Register;implementationprocedure Register;
begin
  RegisterComponents('FTP', [TDragDropFTP]);
end;
constructor TDragDropFTP.Create;
begin
inherited Create(AOwner);
parent:=Owner As TWinControl;
if csDesigning in ComponentState then
begin
Active:=true;
Height:=128;
Width:=261;
BevelOuter:=bvnone;
BevelInner:=bvnone;
borderwidth:=3;
end;
ListView:=TListView.Create(self);
with ListView do
begin
parent:=self;
ReadOnly:=true;
align:=alClient;
viewstyle:=vsReport;
with Columns.Add do
begin
caption:='Name';
width:=150;
end;
with Columns.Add do
begin
caption:='Size';
width:=70;
Alignment:=taRightJustify;
end;
with Columns.Add do
begin
caption:='Date';
width:=100;
end;
LargeImages:=TImageList.Create(self);
LargeImages.Height:=32;
LargeImages.Width:=32;
OnDblClick:=DoDblClick;
end;
end;
procedure TDragDropFTP.Connect;
var
hSessin:HInternet;
begin
if assigned(hConnent) then InternetClosehandle(hConnect);
if HostName='' then raise Exception.Create('HostName must be specified');
if LoginName='' then raise Exception.Create('LoginName must be specified');
if Password='' then raise Exception.Create('Password must be specified');
Log('Connecting to'+HostName);
hSession:=InternetOpen('BetaSoft',INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
if Assigned(hSession)then
begin
hConnect:=InternetConnect(
hSession,
PChar(Hostname),
INTERNET_DEFAULT_FTP_PORT,
PChar(LoginName),
PChar(Password),
INTERNET_SERVICE_FTP,
INTERNET_FLAG_PASSIVE,
0);
if Assigned(hConnect)then
begin
Log('Connented to'+Hostname);
if not FtpSetCurrentDirectory(hConnect,PChar(RemoteDir))
then ShowMessage('Could not change to'+RemoteDir);
RefreshFileList;
Exit;
end
end;
ShowMessage('Could not connect to'+Hostname);
end;
procedure TDragDropFTP.RefreshFileList;
var
lpFindFileData:TWin32FindData;
hFind:HInternet;
begin
Log('Transferring data...');
ListView.items.clear;
with ListView.items.add do
begin
Caption:='[..]';
SubItems.Add('');
SubItems.Add('');
end;
hFind:=FtpFindFirstFile(hConnect,nil,lpFindFileData,0,0);
if Assigned(hFind)then
begin
if GetLastError<>ERROR_NO_MORE_FILES then
begin
AddFile(lpFindFileData);
while InternetFindNextFile(hFind,@lpFindFileData)
do AddFile(lpFindFileData);
end;
InternetCloseHandle(hFind); 
end;
Log('Transfer completed');
end;
procedure TDragDropFTP.AddFile(lpFindFileData:Twin32FindData);
var
Seconds,Minutes,Hours,Day,Month,Year,dosDate,dosTime:word;
begin
Application.ProcessMessage;
with ListView.items.add do
begin
isDirectory:=(lpFindFileData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY)=FILE_ATTRIBUTE_DIRECTORY;
if isDirectory then
begin
caption:=PChar('['+String(lpFindFileData.cFilename)+']');
subitems.add('');
end
else
begin
caption:=lpFindFileData.cFilename;
subitems.add(inttostr(lpFindFileData.nFileSizeLow));
end;
FileTimeToDOSDateTime(lpFindFileData.ftLastWriteTime,dosDate,dosTime);
Day:=dosDate and 31;
Month:=(dosDate and (32+64+128+256))shr 5;
Year:=1980+(dosDate and (5535-512))shr 9;Seconds:=2*(dosTime and 31);
Minutes:=(dosTime and (32+64+128+256))shr 5;
Hours:=(dosTime and (65535-2047))shr 11;subItems.Add(DateTimeToStr(
EncodeDate(Year,Month,Day)+
EncodeTime(Hours,Minutes,Seconds,0)));
end;
end;
procedure TDragDropFTP.DoDblClick;
var
TheFile,Folder:string;
begin
if (ListView.Selected<>nil)then
begin
TheFile:=ListView.Selected.Caption;
if ListView.Selected.SubItems[0]<>'' then
begin
if Assigned(SaveDialog) then
with SaveDialog do
begin
FileName:=TheFile;
if Execute then
FTPGetFile(hConnect,PChar(TheFile),PChar(FileName),false,0,0,0);
end;
end
else
begin
Folder:=Copy(TheFile,2,Length(TheFile)-2);
if FtpSetCurrentDirectory(hConnect,PChar(Folder))
then RefreshFileList
else ShowMessage('Cound not change to directory'+Folder);
end;
end;
if Assigned(FOnDblClick) then FOnDblClick(sender)
end;
procedure TDragDropFTP.WMDropFiles;
var
FileName:PChar;
i,count,size,Drag:integer;
begin
FileName:='';
Application.BringToFront;
Drop:=msg.WParam;
count:=DragQueryFile(Drop,$FFFFFFFF,'',0);
ListView.items.Clear;
for i:=1 to count do
begin
size:=DrapQueryFile(Drop,i-1,nil,1);
GetMem(filename,size+1);
DrapQueryFile(Drop,i-1,FileName,size+1);
FTPPutFile(hConnect,FileName,PChar(ExtractFileName(string(FileName))),0,0);
end;
FreeMem(filename);
refreshFileList;
DragFinish(Drop);
Change;
end;
end.

解决方案 »

  1.   

    难道这样写能把你累着?unit DragDropFTP;interfaceuses
      Windows, Messages, SysUtils,
      Classes, Graphics, Controls,
      Forms, Dialogs, ExtCtrls;type
        TDragDropFTP = class(TPanel)
      private
        hConnect:HInternet;
        FActive:boolean;
        FHostName:string;
        FLogin:string;
        FPassword:string;
        FRemoteDir:string;
        FSaveDialog:TSaveDialog;
        FOnChange:TNotifyEvent;
        FOnDblClick:TNotifyEvent;
        procedure AddFile(lpFindFileData:TWin32FindData);
        procedure Change;dynamic;
        procedure doDblClick(sender:TObject);
        procedure Loaded;override;
        procedure setActive(Value:boolean);
        procedure Log(const Text:string);
        procedure WMDropFiles(var msg:TMessage);message WM_DROPFILES;
      protected
        { Protected declarations }
      public
        ListView:TListView;
        constructor Create(AOwner:TComponent);override;
        procedure Connect;
        procedure RefreshFileList;
        { Public declarations }
      published
        property RemoteDir:string read FRemoteDir write FRemoteDir;
        property Active:boolean read FActive write SetActive;
        property HostName:string read FHostName write FHostName;
        property LoginName:string read FLogin write FLogin;
        property onChange:TNotifyEvent read FOnChange write FOnChange;
        property onDblClick:TNotifyEvent read FOnDblClick write FOnDblClick;
        property Password:string read FPassword write FPassword;
        property SaveDialog:TSaveDialog read FSaveDialog write FSaveDialog;
        property StatusLabel:TLabel read FLabel write FLabel;
        { Published declarations }
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('FTP', [TDragDropFTP]);
    end;constructor TDragDropFTP.Create;
    begin
        inherited Create(AOwner);
        parent:=Owner As TWinControl;    if csDesigning in ComponentState then
        begin
            Active:=true;
            Height:=128;
            Width:=261;
            BevelOuter:=bvnone;
            BevelInner:=bvnone;
            borderwidth:=3;
        end;    ListView:=TListView.Create(self);    with ListView do
        begin
            parent:=self;
            ReadOnly:=true;
            align:=alClient;
            viewstyle:=vsReport;        with Columns.Add do
            begin
                caption:='Name';
                width:=150;
            end;        with Columns.Add do
            begin
                caption:='Size';
                width:=70;
                Alignment:=taRightJustify;
            end;
            
    with Columns.Add do
            begin
                caption:='Date';
                width:=100;
            end;
            
    LargeImages:=TImageList.Create(self);
            LargeImages.Height:=32;
            LargeImages.Width:=32;
            OnDblClick:=DoDblClick;
        end;
    end;