procedure Register; begin RegisterComponents('Sunisoft', [TDropFileListBox]); //注册组件到组件板上 end;
constructor TDropFileListBox.Create(AOwner: TComponent); begin inherited Create(AOwner); FEnabled:=true; //类被构造时,使DropEnabeld的默认值为True end;
destructor TDropFileListBox.Destroy; begin inherited Destroy; end;
//改变属性DropEnabled的调用过程 procedure TDropFileListBox.FDropEnabled(Enabled:Boolean); begin FEnabled:=Enabled; DragAcceptFiles(Self.Handle,Enabled);//设置组件窗口是否接受文件拖放 end;
//接受WM_DropFiles消息的过程 procedure TDropFileListBox.DropFiles(var Mes:TMessage); var FN:TStringList; FileName:array [1..256] of char; sFN:String; i,Count,p:integer; begin FN:=TStringList.Create; Count:=DragQueryFile(Mes.WParam,$FFFFFFFF,@FileName,256);//得到拖放文件的个数 For i:=0 to Count-1 do begin DragQueryFile(mes.WParam,i,@FileName,256);//查询文件名称 sFN:=FileName; p:=pos(chr(0),sFN);//去掉文件名末尾的ASCII码为0的字符 sFN:=copy(sFN,1,p-1); FN.Add(sFN); end; DragFinish(mes.WParam); //释放所使用的资源 if Assigned(FDropFile) then FDropFile(self, FN); //调用事件,并返回文件名列表参数 FN.Free; end;