关于拖放的实现问题(50分)

  • 主题发起人 主题发起人 cometjun
  • 开始时间 开始时间
C

cometjun

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样才可以实现拖放文件夹,然后在列表框中显示该文件夹中所含有的文件名呢?<br>最好能有代码,谢谢!
 
unit dragdrop;<br><br>interface<br><br>uses<br> &nbsp;Windows, ActiveX, Dialogs,Sysutils;<br><br>type<br> &nbsp;TDropEvent = procedure(Sender:TObject;Msg:Pchar)of object;<br>TTMyDrop = class(TInterfacedObject, IDropTarget)<br> &nbsp;private<br> &nbsp; &nbsp;FOnDroped: TDropEvent;<br> &nbsp; &nbsp;procedure SetOnDroped(const Value: TDropEvent);<br> &nbsp;protected<br> &nbsp; &nbsp;{Declare IDropTarget methods here}<br> &nbsp; &nbsp;function DragEnter(const dataObj: IDataObject; grfKeyState: Longint;<br> &nbsp; &nbsp; &nbsp;pt: TPoint; var dwEffect: Longint): HResult; stdcall;<br> &nbsp; &nbsp;function DragOver(grfKeyState: Longint; pt: TPoint;<br> &nbsp; &nbsp; &nbsp;var dwEffect: Longint): HResult; stdcall;<br> &nbsp; &nbsp;function DragLeave: HResult; stdcall;<br> &nbsp; &nbsp;function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint;<br> &nbsp; &nbsp; &nbsp;var dwEffect: Longint): HResult; stdcall;<br> &nbsp;public<br> &nbsp; &nbsp;property OnDroped:TDropEvent read FOnDroped write SetOnDroped;<br> &nbsp;end;<br><br>{const<br> &nbsp;Class_TMyDrop: TGUID = '{846C94F8-7649-11D2-9836-0000E82EA1B1'; }<br><br>implementation<br><br>uses ComServ;<br><br>{ TTMyDrop }<br><br>function TTMyDrop.DragEnter(const dataObj: IDataObject;<br> &nbsp;grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;<br>var<br> &nbsp;enumFormatEtc: IEnumFormatEtc;<br> &nbsp;f:TFORMATETC;<br> &nbsp;count:Integer;<br> &nbsp;Found:boolean;<br>begin<br> &nbsp;dataObj.EnumFormatEtc(DATADIR_GET,enumFormatEtc);<br> &nbsp;Found:=false;<br> &nbsp;while (enumFormatEtc.Next(1,f,@count)=S_OK)and (count&gt;0) do<br> &nbsp;begin<br> &nbsp; &nbsp;if (f.cfFormat=CF_TEXT) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Found:=true;<br> &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;if Found then<br> &nbsp; &nbsp;Result:=S_OK<br> &nbsp;else<br> &nbsp;begin<br> &nbsp; &nbsp;result:=E_INVALIDARG;<br> &nbsp; &nbsp;dwEffect:=DROPEFFECT_NONE;<br> &nbsp;end;<br>end;<br><br>function TTMyDrop.DragLeave: HResult;<br>begin<br> &nbsp; result := S_OK;<br>end;<br><br>function TTMyDrop.DragOver(grfKeyState: Integer; pt: TPoint;<br> &nbsp;var dwEffect: Integer): HResult;<br>begin<br> &nbsp; result := S_OK;<br>end;<br><br>function TTMyDrop.Drop(const dataObj: IDataObject; grfKeyState: Integer;<br> &nbsp;pt: TPoint; var dwEffect: Integer): HResult;<br>var<br> &nbsp;enumFormatEtc: IEnumFormatEtc;<br> &nbsp;f:TFORMATETC;<br> &nbsp;count:Integer;<br> &nbsp;Found:boolean;<br> &nbsp;medium: TStgMedium;<br>begin<br> &nbsp;dataObj.EnumFormatEtc(DATADIR_GET,enumFormatEtc);<br> &nbsp;Found:=false;<br> &nbsp;while (enumFormatEtc.Next(1,f,@count)=S_OK)and (count&gt;0) do<br> &nbsp;begin<br> &nbsp; &nbsp;if (f.cfFormat=CF_TEXT) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Found:=true;<br> &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;if not Found then<br> &nbsp;begin<br> &nbsp; // result:=E_INVALIDARG;<br> &nbsp; &nbsp;dwEffect:=DROPEFFECT_NONE;<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;dataObj.GetData(f,medium);<br> &nbsp;if medium.tymed =1 then<br> &nbsp;begin<br> &nbsp; &nbsp;if Assigned(fOnDroped) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;fOnDroped(Self,PChar(GlobalLock(medium.hglobal)));<br> &nbsp; &nbsp; &nbsp;GlobalUnLock(medium.hglobal);<br> &nbsp; &nbsp;end;<br> &nbsp;// &nbsp;result := S_OK;<br> &nbsp;end;<br><br>end;<br><br>procedure TTMyDrop.SetOnDroped(const Value: TDropEvent);<br>begin<br> &nbsp;FOnDroped := Value;<br>end;<br><br>initialization<br> &nbsp;{TComObjectFactory.Create(ComServer, TTMyDrop, Class_TMyDrop,<br> &nbsp; &nbsp;'TMyDrop', '', ciMultiInstance, tmApartment); }<br>end.<br><br>--------------------------------<br>procedure FileIsDropped(var Msg: TMessage);<br>var hDrop : THandle;<br> &nbsp; &nbsp;fName : array[0..254] of char ;<br> &nbsp; &nbsp;ct,FileNum : integer;<br> &nbsp; &nbsp;dir: string;<br>begin<br> &nbsp; &nbsp;hDrop := Msg.WParam;<br> &nbsp; &nbsp;FileNum := DragQueryFile(hDrop,cardinal(-1),fName,254);<br> &nbsp; &nbsp;for ct := 0 to FileNum - 1 do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;DragQueryFile(hDrop,ct,fName,254);<br> &nbsp; &nbsp; &nbsp; &nbsp;if (FileGetAttr(fname) and faDirectory)&lt;&gt;0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dir:=string(fname);//这大概就是你想要的.你应该可以列出这个文件夹里的文件了.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//fname在这里是文件<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; end;<br> &nbsp; &nbsp;DragFinish(hDrop);<br>end;
 
太谢谢了!!!~~~~~~~~~~~~~~~~~~<br>感激iseek兄弟ing!!!~~~~~~~~~~~~~~
 
后退
顶部