蹭分<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, FileCtrl, ComObj, ActiveX, ShlObj;<br><br>type<br> TForm1 = class(TForm)<br> FileListBox1: TFileListBox;<br> DirectoryListBox1: TDirectoryListBox;<br> DriveComboBox1: TDriveComboBox;<br> procedure FileListBox1MouseDown(Sender: TObject; Button: TMouseButton;<br> Shift: TShiftState; X, Y: Integer);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br> TDrop = class(TInterfacedObject,IDropSource,IDataObject,IEnumFormatEtc)<br> FIndex: Word ;<br> public<br> procedure free;<br> //IDropSource<br> function QueryContinueDrag(fEscapePressed: BOOL; grfKeyState: Longint): HResult; stdcall;<br> function GiveFeedback(dwEffect: Longint): HResult; stdcall;<br> //IEnumFormatEtc<br> function AddFormat(Enum: TFormatEtc): Integer;<br> function Next(celt: Longint; out elt; pceltFetched: PLongint): HResult; stdcall;<br> function Skip(celt: Longint): HResult; stdcall;<br> function Reset: HResult; stdcall;<br> function Clone(out Enum: IEnumFormatEtc): HResult; stdcall;<br> //IDataObject<br> function GetData(const formatetcIn: TFormatEtc; out medium: TStgMedium): HResult; stdcall;<br> function GetDataHere(const formatetc: TFormatEtc; out medium: TStgMedium): HResult; stdcall;<br> function QueryGetData(const formatetc: TFormatEtc): HResult; stdcall;<br> function GetCanonicalFormatEtc(const formatetc: TFormatEtc; out formatetcOut: TFormatEtc): HResult;stdcall;<br> function SetData(const formatetc: TFormatEtc; var medium: TStgMedium; fRelease: BOOL): HResult; stdcall;<br> function EnumFormatEtc(dwDirection: Longint; out enumFormatEtc: IEnumFormatEtc): HResult; stdcall;<br> function DAdvise(const formatetc: TFormatEtc; advf: Longint; const advSink: IAdviseSink; out dwConnection: Longint): HResult; stdcall;<br> function DUnadvise(dwConnection: Longint): HResult; stdcall;<br> function EnumDAdvise(out enumAdvise: IEnumStatData): HResult; stdcall;<br> end;<br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br>//IDropSource<br>function TDrop.QueryContinueDrag(fEscapePressed: BOOL; grfKeyState: Longint): HResult; stdcall;<br>begin<br> if fEscapePressed or (grfKeyState and MK_RBUTTON = MK_RBUTTON) then begin<br> Result := DRAGDROP_S_CANCEL;<br> end else if grfKeyState and MK_LBUTTON = 0 then begin<br> Result := DRAGDROP_S_DROP;<br> end else begin<br> Result := S_OK;<br> end<br>end;<br><br>function TDrop.GiveFeedback(dwEffect: Longint): HResult; stdcall;<br>begin<br> Result := DRAGDROP_S_USEDEFAULTCURSORS;<br>end;<br><br>//IEnumFormatEtc<br>function TDrop.AddFormat(Enum: TFormatEtc): Integer;<br>begin<br> Result := -1;<br>end;<br><br>function TDrop.Next(celt: Longint; out elt; pceltFetched: PLongint): HResult; stdcall;<br>begin<br> Result := S_FALSE;<br> if celt=0 then Reset;<br> if FIndex>0 then exit;<br> with TFormatEtc(elt) do begin<br> cfFormat := CF_HDROP;<br> tymed := TYMED_HGLOBAL;<br> dwAspect := DVASPECT_CONTENT;<br> lIndex := 0;<br> ptd := nil;<br> end;<br> if Assigned(pceltFetched) then pceltFetched^:=FIndex;<br><br> if celt>0 then Inc(FIndex,celt) else inc(FIndex);<br> Result := S_OK;<br>end;<br><br>function TDrop.Skip(celt: Longint): HResult; stdcall;<br>begin Result := S_FALSE end;<br><br>function TDrop.Reset: HResult; stdcall;<br>begin<br> FIndex:=0;<br> Result := S_OK;<br>end;<br><br>function TDrop.Clone(out Enum: IEnumFormatEtc): HResult; stdcall;<br>begin Result := E_NOTIMPL; end; //不支持的接口<br><br>//IDataObject<br>function TDrop.GetData(const formatetcIn: TFormatEtc; out medium: TStgMedium): HResult; stdcall;<br>var<br>BufferText : String;<br>pGlobal : Pointer;<br>i : Integer;<br>begin<br> Result := DV_E_FORMATETC; //不支持的格式<br> if not (Self.QueryGetData(formatetcIn)=S_OK) then exit;<br> FillChar(Medium,Sizeof(TStgMedium),0);<br> Medium.tymed:=formatetcIn.tymed;<br><br> BufferText:=Form1.FileListBox1.Directory+'/';<br> for i:=0 to Form1.FileListBox1.Count - 1 do<br> if Form1.FileListBox1.Selected then begin<br> BufferText:=BufferText+Form1.FileListBox1.Items+#0+#0;<br> break;<br> end;<br> //需要拖拽多个文件的格式如下<br> //'c:/temp/aa.txt'+#0+#0+'c:/temp/aa.txt'+#0+#0;+'c:/temp/aa.txt'+#0+#0;<br> Medium.hGlobal := GlobalAlloc(GMEM_ZEROINIT or GMEM_MOVEABLE or GMEM_SHARE, Length(BufferText)+1+Sizeof(TDropFiles));<br> pGlobal := GlobalLock(Medium.hGlobal);<br> PDropFiles(pGlobal)^.pFiles:=Sizeof(TDropFiles);<br> PDropFiles(pGlobal)^.pt:=Point(0,0);<br> PDropFiles(pGlobal)^.fNC:=False;<br> PDropFiles(pGlobal)^.fWide:=False;<br> inc(Longword(pGlobal),Sizeof(TDropFiles)); //指针后移<br> CopyMemory(PGlobal,Pchar(BufferText),Length(BufferText)+1);<br> GlobalUnlock(Medium.hGlobal);<br> Medium.unkForRelease := nil;<br> Result := S_OK;<br>end;<br><br><br>function TDrop.QueryGetData(const formatetc: TFormatEtc): HResult; stdcall;<br>begin<br> Result := DV_E_FORMATETC; //不支持的格式<br><br> if (formatetc.cfFormat=CF_HDROP) and //表示支持文件拖拽格式<br> (formatetc.tymed=TYMED_HGLOBAL) and<br> (formatetc.dwAspect=DVASPECT_CONTENT) then<br> Result := S_OK;<br>end;<br><br>function TDrop.EnumFormatEtc(dwDirection: Longint; out enumFormatEtc: IEnumFormatEtc): HResult; stdcall;<br>begin<br> Result := S_FALSE;<br> Reset;<br> case dwDirection of<br> DATADIR_GET :<br> begin<br> enumFormatEtc:=Self as IEnumFormatEtc;<br> Result := S_OK;<br> end;<br> DATADIR_SET : Result := E_NOTIMPL;<br> end;<br>end;<br><br>function TDrop.SetData(const formatetc: TFormatEtc; var medium: TStgMedium; fRelease: BOOL): HResult; stdcall;<br>begin Result := E_NOTIMPL; end; //不支持的接口}<br><br>function TDrop.GetCanonicalFormatEtc(const formatetc: TFormatEtc; out formatetcOut: TFormatEtc): HResult;stdcall;<br>begin Result := E_NOTIMPL; end; //不支持的接口}<br><br>function TDrop.GetDataHere(const formatetc: TFormatEtc; out medium: TStgMedium): HResult; stdcall;<br>begin Result := E_NOTIMPL; end; //不支持的接口}<br><br>function TDrop.DAdvise(const formatetc: TFormatEtc; advf: Longint; const advSink: IAdviseSink; out dwConnection: Longint): HResult; stdcall;<br>begin Result := E_NOTIMPL; end; //不支持的接口}<br><br>function TDrop.DUnadvise(dwConnection: Longint): HResult; stdcall;<br>begin Result := E_NOTIMPL; end; //不支持的接口}<br><br>function TDrop.EnumDAdvise(out enumAdvise: IEnumStatData): HResult; stdcall;<br>begin Result := E_NOTIMPL; end; //不支持的接口}<br><br>procedure TDrop.free;<br>begin<br>end;<br><br>procedure TForm1.FileListBox1MouseDown(Sender: TObject;<br> Button: TMouseButton; Shift: TShiftState; X, Y: Integer);<br>var<br>Effect : Longint;<br>DataObject: TDrop;<br>begin<br>if not (Button=mbLeft) then exit;<br>DataObject:=TDrop.Create;<br>Effect := DROPEFFECT_NONE;<br>OleCheck(DoDragDrop(DataObject as IDataObject, DataObject as IDropSource, DROPEFFECT_COPY, Effect));<br>DataObject.Free;<br>end;<br><br>initialization<br> OleInitialize(nil);<br>finalization<br> OleUninitialize;<br><br>end.