Main.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. unit Main;
  2. interface
  3. uses
  4. Windows, Classes, Controls, Forms, ExtCtrls, StdCtrls,
  5. DragDrop, DropSource, DragDropFile, DropTarget, Graphics, ImgList;
  6. type
  7. TVirtualFileDataFormat = class(TCustomDataFormat)
  8. private
  9. FContents: string;
  10. FFileName: string;
  11. public
  12. constructor Create(AOwner: TDragDropComponent); override;
  13. function Assign(Source: TClipboardFormat): boolean; override;
  14. function AssignTo(Dest: TClipboardFormat): boolean; override;
  15. procedure Clear; override;
  16. function HasData: boolean; override;
  17. function NeedsData: boolean; override;
  18. property FileName: string read FFileName write FFileName;
  19. property Contents: string read FContents write FContents;
  20. end;
  21. TFormMain = class(TForm)
  22. EditFilename: TEdit;
  23. Label1: TLabel;
  24. MemoContents: TMemo;
  25. Label2: TLabel;
  26. DropFileSource1: TDropFileSource;
  27. DropFileTarget1: TDropFileTarget;
  28. DropDummy1: TDropDummy;
  29. Panel2: TPanel;
  30. Memo1: TMemo;
  31. PanelDragDrop: TPanel;
  32. Image1: TImage;
  33. ImageList1: TImageList;
  34. procedure OnMouseDown(Sender: TObject; Button: TMouseButton;
  35. Shift: TShiftState; X, Y: Integer);
  36. procedure FormCreate(Sender: TObject);
  37. procedure DropFileTarget1Drop(Sender: TObject; ShiftState: TShiftState;
  38. Point: TPoint; var Effect: Integer);
  39. private
  40. { Private declarations }
  41. FSourceDataFormat: TVirtualFileDataFormat;
  42. FTargetDataFormat: TVirtualFileDataFormat;
  43. public
  44. { Public declarations }
  45. end;
  46. var
  47. FormMain: TFormMain;
  48. implementation
  49. {$R *.DFM}
  50. uses
  51. DragDropFormats,
  52. ShlObj,
  53. SysUtils;
  54. procedure TFormMain.FormCreate(Sender: TObject);
  55. begin
  56. // Disable all the normal data formats supported by the drag/drop components.
  57. // Note: We use TDropFileXXX components in this demo, but since we disable
  58. // all the default data formats and then add our own, any of the standard
  59. // source & target components could have been used.
  60. // We could have created pair of custom source and target components, but for
  61. // this demo it is much easier to just (mis)use some of the existing ones.
  62. while (DropFileSource1.DataFormats.Count > 0) do
  63. DropFileSource1.DataFormats.Remove(DropFileSource1.DataFormats[0]);
  64. while (DropFileTarget1.DataFormats.Count > 0) do
  65. DropFileTarget1.DataFormats.Remove(DropFileTarget1.DataFormats[0]);
  66. // Add our own custom data format to the drag/drop components.
  67. FSourceDataFormat := TVirtualFileDataFormat.Create(DropFileSource1);
  68. FTargetDataFormat := TVirtualFileDataFormat.Create(DropFileTarget1);
  69. end;
  70. procedure TFormMain.OnMouseDown(Sender: TObject; Button: TMouseButton;
  71. Shift: TShiftState; X, Y: Integer);
  72. begin
  73. if DragDetectPlus(Handle, Point(X,Y)) then
  74. begin
  75. // Transfer the file name and contents to the data format...
  76. FSourceDataFormat.FileName := EditFileName.Text;
  77. FSourceDataFormat.Contents := MemoContents.Lines.Text;
  78. // ...and let it rip!
  79. DropFileSource1.Execute;
  80. end;
  81. end;
  82. procedure TFormMain.DropFileTarget1Drop(Sender: TObject;
  83. ShiftState: TShiftState; Point: TPoint; var Effect: Integer);
  84. begin
  85. // Transfer the file name and contents from the data format.
  86. EditFileName.Text := FTargetDataFormat.FileName;
  87. MemoContents.Lines.Text := FTargetDataFormat.Contents;
  88. end;
  89. { TVirtualFileDataFormat }
  90. constructor TVirtualFileDataFormat.Create(AOwner: TDragDropComponent);
  91. begin
  92. inherited Create(AOwner);
  93. // Add the "file group descriptor" and "file contents" clipboard formats to
  94. // the data format's list of compatible formats.
  95. // Note: This is normally done via TCustomDataFormat.RegisterCompatibleFormat,
  96. // but since this data format is only used here, it is just as easy for us to
  97. // add the formats manually.
  98. CompatibleFormats.Add(TFileContentsClipboardFormat.Create);
  99. CompatibleFormats.Add(TFileGroupDescritorClipboardFormat.Create);
  100. end;
  101. function TVirtualFileDataFormat.Assign(Source: TClipboardFormat): boolean;
  102. begin
  103. Result := True;
  104. (*
  105. ** TFileContentsClipboardFormat
  106. *)
  107. if (Source is TFileContentsClipboardFormat) then
  108. begin
  109. FContents := TFileContentsClipboardFormat(Source).Data
  110. end else
  111. (*
  112. ** TFileGroupDescritorClipboardFormat
  113. *)
  114. if (Source is TFileGroupDescritorClipboardFormat) then
  115. begin
  116. if (TFileGroupDescritorClipboardFormat(Source).FileGroupDescriptor^.cItems > 0) then
  117. FFileName := TFileGroupDescritorClipboardFormat(Source).FileGroupDescriptor^.fgd[0].cFileName;
  118. end else
  119. (*
  120. ** None of the above...
  121. *)
  122. Result := inherited Assign(Source);
  123. end;
  124. function TVirtualFileDataFormat.AssignTo(Dest: TClipboardFormat): boolean;
  125. var
  126. FGD : TFileGroupDescriptor;
  127. begin
  128. Result := True;
  129. (*
  130. ** TFileContentsClipboardFormat
  131. *)
  132. if (Dest is TFileContentsClipboardFormat) then
  133. begin
  134. TFileContentsClipboardFormat(Dest).Data := FContents;
  135. end else
  136. (*
  137. ** TFileGroupDescritorClipboardFormat
  138. *)
  139. if (Dest is TFileGroupDescritorClipboardFormat) then
  140. begin
  141. FillChar(FGD, SizeOf(FGD), 0);
  142. FGD.cItems := 1;
  143. StrLCopy(@FGD.fgd[0].cFileName[0], PChar(FFileName),
  144. SizeOf(FGD.fgd[0].cFileName));
  145. TFileGroupDescritorClipboardFormat(Dest).CopyFrom(@FGD);
  146. end else
  147. (*
  148. ** None of the above...
  149. *)
  150. Result := inherited AssignTo(Dest);
  151. end;
  152. procedure TVirtualFileDataFormat.Clear;
  153. begin
  154. FFileName := '';
  155. FContents := ''
  156. end;
  157. function TVirtualFileDataFormat.HasData: boolean;
  158. begin
  159. Result := (FFileName <> '');
  160. end;
  161. function TVirtualFileDataFormat.NeedsData: boolean;
  162. begin
  163. Result := (FFileName = '') or (FContents = '');
  164. end;
  165. end.