我把dragdrop中的DropURLSource贴了,如果满意可以给分了我等分用呀![
]<br>unit DropURLSource;<br>interface<br>uses<br> DropSource,<br> Classes, ActiveX;<br>{$include DragDrop.inc}<br><br>type<br> TDropURLSource = class(TDropSource)<br> private<br> fURL: String;<br> fTitle: String;<br> protected<br> function DoGetData(const FormatEtcIn: TFormatEtc; OUT Medium: TStgMedium):HRESULT; Override;<br> public<br> constructor Create(aOwner: TComponent); Override;<br> function CutOrCopyToClipboard: boolean; Override;<br> published<br> property URL: String Read fURL Write fURL;<br> property Title: String Read fTitle Write fTitle;<br> end;<br><br>procedure Register;<br><br>implementation<br><br>uses<br> Windows,<br> SysUtils,<br> ClipBrd,<br> ShlObj;<br><br>procedure Register;<br>begin<br> RegisterComponents('DragDrop', [TDropURLSource]);<br>end;<br>function ConvertURLToFilename(url: string): string;<br>const<br> Invalids = '//:?*<>,|''"';<br>var<br> i: integer;<br>begin<br> if lowercase(copy(url,1,7)) = 'http://' then<br> url := copy(url,8,128) // limit to 120 chars.<br> else if lowercase(copy(url,1,6)) = 'ftp://' then<br> url := copy(url,7,127)<br> else if lowercase(copy(url,1,7)) = 'mailto:' then<br> url := copy(url,8,128)<br> else if lowercase(copy(url,1,5)) = 'file:' then<br> url := copy(url,6,126);<br><br> if url = '' then url := 'untitled';<br> result := url;<br> for i := 1 to length(result) do<br> if result
= '/'then<br> begin<br> result := copy(result,1,i-1);<br> break;<br> end<br> else if pos(result,Invalids) <> 0 then<br> result := ' ';<br> appendstr(result,'.url');<br>end;<br><br><br>constructor TDropURLSource.Create(aOwner: TComponent);<br>begin<br> inherited Create(aOwner);<br> fURL := '';<br> fTitle := '';<br> DragTypes := [dtLink]; // Only dtLink allowed<br><br> AddFormatEtc(CF_URL, NIL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);<br> AddFormatEtc(CF_FILEGROUPDESCRIPTOR, NIL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);<br> AddFormatEtc(CF_FILECONTENTS, NIL, DVASPECT_CONTENT, 0, TYMED_HGLOBAL);<br> AddFormatEtc(CF_TEXT, NIL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);<br>end;<br>function TDropURLSource.CutOrCopyToClipboard: boolean;<br>var<br> FormatEtcIn: TFormatEtc;<br> Medium: TStgMedium;<br>begin<br> result := false;<br> FormatEtcIn.cfFormat := CF_URL;<br> FormatEtcIn.dwAspect := DVASPECT_CONTENT;<br> FormatEtcIn.tymed := TYMED_HGLOBAL;<br> if fURL = '' then exit;<br> if GetData(formatetcIn,Medium) = S_OK then<br> begin<br> Clipboard.SetAsHandle(CF_URL,Medium.hGlobal);<br> result := true;<br> end else exit;<br><br> //render several formats...<br> FormatEtcIn.cfFormat := CF_TEXT;<br> FormatEtcIn.dwAspect := DVASPECT_CONTENT;<br> FormatEtcIn.tymed := TYMED_HGLOBAL;<br> if GetData(formatetcIn,Medium) = S_OK then<br> begin<br> Clipboard.SetAsHandle(CF_TEXT,Medium.hGlobal);<br> result := true;<br> end;<br>end;<br>// ----------------------------------------------------------------------------- <br><br>function TDropURLSource.DoGetData(const FormatEtcIn: TFormatEtc; OUT Medium: TStgMedium):HRESULT;<br>const<br> URLPrefix = '[InternetShortcut]'#10'URL=';<br>var<br> pFGD: PFileGroupDescriptor;<br> pText: PChar;<br>begin<br><br> Medium.tymed := 0;<br> Medium.UnkForRelease := NIL;<br> Medium.hGlobal := 0;<br><br> //--------------------------------------------------------------------------<br> if ((FormatEtcIn.cfFormat = CF_URL) or (FormatEtcIn.cfFormat = CF_TEXT)) and<br> (FormatEtcIn.dwAspect = DVASPECT_CONTENT) and<br> (FormatEtcIn.tymed and TYMED_HGLOBAL <> 0) then<br> begin<br> Medium.hGlobal := GlobalAlloc(GMEM_SHARE or GHND, Length(fURL)+1);<br> if (Medium.hGlobal = 0) then<br> result := E_OUTOFMEMORY<br> else<br> begin<br> medium.tymed := TYMED_HGLOBAL;<br> pText := PChar(GlobalLock(Medium.hGlobal));<br> try<br> StrCopy(pText, PChar(fURL));<br> finally<br> GlobalUnlock(Medium.hGlobal);<br> end;<br> result := S_OK;<br> end;<br> end<br> //--------------------------------------------------------------------------<br> else if (FormatEtcIn.cfFormat = CF_FILECONTENTS) and<br> (FormatEtcIn.dwAspect = DVASPECT_CONTENT) and<br> (FormatEtcIn.tymed and TYMED_HGLOBAL <> 0) then<br> begin<br> Medium.hGlobal := GlobalAlloc(GMEM_SHARE or GHND, Length(URLPrefix + fURL)+1);<br> if (Medium.hGlobal = 0) then<br> result := E_OUTOFMEMORY<br> else<br> begin<br> medium.tymed := TYMED_HGLOBAL;<br> pText := PChar(GlobalLock(Medium.hGlobal));<br> try<br> StrCopy(pText, PChar(URLPrefix + fURL));<br> finally<br> GlobalUnlock(Medium.hGlobal);<br> end;<br> result := S_OK;<br> end;<br> end<br> //--------------------------------------------------------------------------<br> else if (FormatEtcIn.cfFormat = CF_FILEGROUPDESCRIPTOR) and<br> (FormatEtcIn.dwAspect = DVASPECT_CONTENT) and<br> (FormatEtcIn.tymed and TYMED_HGLOBAL <> 0) then<br> begin<br> Medium.hGlobal := GlobalAlloc(GMEM_SHARE or GHND, SizeOf(TFileGroupDescriptor));<br> if (Medium.hGlobal = 0) then<br> begin<br> result := E_OUTOFMEMORY;<br> Exit;<br> end;<br> medium.tymed := TYMED_HGLOBAL;<br> pFGD := pointer(GlobalLock(Medium.hGlobal));<br> try<br> with pFGD^ do<br> begin<br> cItems := 1;<br> fgd[0].dwFlags := FD_LINKUI;<br> if title = '' then<br> StrPCopy(fgd[0].cFileName,ConvertURLToFilename(fURL))<br> else<br> StrPCopy(fgd[0].cFileName,ConvertURLToFilename(fTitle));<br> end;<br> finally<br> GlobalUnlock(Medium.hGlobal);<br> end;<br> result := S_OK;<br> end else<br> result := DV_E_FORMATETC;<br>end;<br>end.