DropFileSource3.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. unit DropFileSource3;
  2. // -----------------------------------------------------------------------------
  3. //
  4. // *** NOT FOR RELEASE ***
  5. //
  6. // *** INTERNAL USE ONLY ***
  7. //
  8. // -----------------------------------------------------------------------------
  9. // Project: Drag and Drop Component Suite
  10. // Module: DropFileSource3
  11. // Description: Test case for deprecated TDropSource class.
  12. // Version: 4.0
  13. // Date: 25-JUN-2000
  14. // Target: Win32, Delphi 3-6 and C++ Builder 3-5
  15. // Authors: Angus Johnson, ajohnson@rpi.net.au
  16. // Anders Melander, anders@melander.dk, http://www.melander.dk
  17. // Copyright © 1997-2000 Angus Johnson & Anders Melander
  18. // -----------------------------------------------------------------------------
  19. interface
  20. uses
  21. DragDrop,
  22. DragDropPIDL,
  23. DragDropFormats,
  24. DragDropFile,
  25. DropSource3,
  26. ActiveX, Classes;
  27. {$include DragDrop.inc}
  28. type
  29. TDropFileSourceX = class(TDropSource)
  30. private
  31. fFiles: TStrings;
  32. fMappedNames: TStrings;
  33. FFileClipboardFormat: TFileClipboardFormat;
  34. FPIDLClipboardFormat: TPIDLClipboardFormat;
  35. FPreferredDropEffectClipboardFormat: TPreferredDropEffectClipboardFormat;
  36. FFilenameMapClipboardFormat: TFilenameMapClipboardFormat;
  37. FFilenameMapWClipboardFormat: TFilenameMapWClipboardFormat;
  38. procedure SetFiles(files: TStrings);
  39. procedure SetMappedNames(names: TStrings);
  40. protected
  41. function DoGetData(const FormatEtcIn: TFormatEtc;
  42. out Medium: TStgMedium):HRESULT; override;
  43. function CutOrCopyToClipboard: boolean; override;
  44. public
  45. constructor Create(aOwner: TComponent); override;
  46. destructor Destroy; override;
  47. published
  48. property Files: TStrings read fFiles write SetFiles;
  49. //MappedNames is only needed if files need to be renamed during a drag op
  50. //eg dragging from 'Recycle Bin'.
  51. property MappedNames: TStrings read fMappedNames write SetMappedNames;
  52. end;
  53. procedure Register;
  54. implementation
  55. uses
  56. Windows,
  57. ShlObj,
  58. SysUtils,
  59. ClipBrd;
  60. procedure Register;
  61. begin
  62. RegisterComponents(DragDropComponentPalettePage, [TDropFileSourceX]);
  63. end;
  64. // -----------------------------------------------------------------------------
  65. // -----------------------------------------------------------------------------
  66. // -----------------------------------------------------------------------------
  67. // -----------------------------------------------------------------------------
  68. constructor TDropFileSourceX.Create(aOwner: TComponent);
  69. begin
  70. inherited Create(aOwner);
  71. fFiles := TStringList.Create;
  72. fMappedNames := TStringList.Create;
  73. FFileClipboardFormat := TFileClipboardFormat.Create;
  74. FPIDLClipboardFormat := TPIDLClipboardFormat.Create;
  75. FPreferredDropEffectClipboardFormat := TPreferredDropEffectClipboardFormat.Create;
  76. FFilenameMapClipboardFormat := TFilenameMapClipboardFormat.Create;
  77. FFilenameMapWClipboardFormat := TFilenameMapWClipboardFormat.Create;
  78. AddFormatEtc(FFileClipboardFormat.GetClipboardFormat, NIL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);
  79. AddFormatEtc(FPIDLClipboardFormat.GetClipboardFormat, NIL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);
  80. AddFormatEtc(FPreferredDropEffectClipboardFormat.GetClipboardFormat, NIL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);
  81. AddFormatEtc(FFilenameMapClipboardFormat.GetClipboardFormat, NIL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);
  82. AddFormatEtc(FFilenameMapWClipboardFormat.GetClipboardFormat, NIL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);
  83. end;
  84. // -----------------------------------------------------------------------------
  85. destructor TDropFileSourceX.destroy;
  86. begin
  87. FFileClipboardFormat.Free;
  88. FPIDLClipboardFormat.Free;
  89. FPreferredDropEffectClipboardFormat.Free;
  90. FFilenameMapClipboardFormat.Free;
  91. FFilenameMapWClipboardFormat.Free;
  92. fFiles.Free;
  93. fMappedNames.free;
  94. inherited Destroy;
  95. end;
  96. // -----------------------------------------------------------------------------
  97. procedure TDropFileSourceX.SetFiles(files: TStrings);
  98. begin
  99. fFiles.assign(files);
  100. end;
  101. // -----------------------------------------------------------------------------
  102. procedure TDropFileSourceX.SetMappedNames(names: TStrings);
  103. begin
  104. fMappedNames.assign(names);
  105. end;
  106. // -----------------------------------------------------------------------------
  107. function TDropFileSourceX.CutOrCopyToClipboard: boolean;
  108. var
  109. FormatEtcIn: TFormatEtc;
  110. Medium: TStgMedium;
  111. begin
  112. FormatEtcIn.cfFormat := CF_HDROP;
  113. FormatEtcIn.dwAspect := DVASPECT_CONTENT;
  114. FormatEtcIn.tymed := TYMED_HGLOBAL;
  115. if (Files.count = 0) then result := false
  116. else if GetData(formatetcIn,Medium) = S_OK then
  117. begin
  118. Clipboard.SetAsHandle(CF_HDROP,Medium.hGlobal);
  119. result := true;
  120. end else result := false;
  121. end;
  122. // -----------------------------------------------------------------------------
  123. function TDropFileSourceX.DoGetData(const FormatEtcIn: TFormatEtc;
  124. out Medium: TStgMedium):HRESULT;
  125. begin
  126. Medium.tymed := 0;
  127. Medium.UnkForRelease := NIL;
  128. Medium.hGlobal := 0;
  129. result := E_UNEXPECTED;
  130. if fFiles.count = 0 then
  131. exit;
  132. //--------------------------------------------------------------------------
  133. if FFileClipboardFormat.AcceptFormat(FormatEtcIn) then
  134. begin
  135. FFileClipboardFormat.Files.Assign(FFiles);
  136. if FFileClipboardFormat.SetDataToMedium(FormatEtcIn, Medium) then
  137. result := S_OK;
  138. end else
  139. //--------------------------------------------------------------------------
  140. if FFilenameMapClipboardFormat.AcceptFormat(FormatEtcIn) then
  141. begin
  142. FFilenameMapClipboardFormat.FileMaps.Assign(fMappedNames);
  143. if FFilenameMapClipboardFormat.SetDataToMedium(FormatEtcIn, Medium) then
  144. result := S_OK;
  145. end else
  146. //--------------------------------------------------------------------------
  147. if FFilenameMapWClipboardFormat.AcceptFormat(FormatEtcIn) then
  148. begin
  149. FFilenameMapWClipboardFormat.FileMaps.Assign(fMappedNames);
  150. if FFilenameMapWClipboardFormat.SetDataToMedium(FormatEtcIn, Medium) then
  151. result := S_OK;
  152. end else
  153. //--------------------------------------------------------------------------
  154. if FPIDLClipboardFormat.AcceptFormat(FormatEtcIn) then
  155. begin
  156. FPIDLClipboardFormat.Filenames.Assign(FFiles);
  157. if FPIDLClipboardFormat.SetDataToMedium(FormatEtcIn, Medium) then
  158. result := S_OK;
  159. end else
  160. //--------------------------------------------------------------------------
  161. //This next format does not work for Win95 but should for Win98, WinNT ...
  162. //It stops the shell from prompting (with a popup menu) for the choice of
  163. //Copy/Move/Shortcut when performing a file 'Shortcut' onto Desktop or Explorer.
  164. if FPreferredDropEffectClipboardFormat.AcceptFormat(FormatEtcIn) then
  165. begin
  166. FPreferredDropEffectClipboardFormat.Value := FeedbackEffect;
  167. if FPreferredDropEffectClipboardFormat.SetDataToMedium(FormatEtcIn, Medium) then
  168. result := S_OK;
  169. end else
  170. result := DV_E_FORMATETC;
  171. end;
  172. end.