main.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. unit main;
  2. interface
  3. uses
  4. DragDrop,
  5. DropTarget,
  6. DropSource,
  7. DragDropText,
  8. Windows, Classes, Graphics, Forms, StdCtrls, Controls;
  9. type
  10. TForm1 = class(TForm)
  11. Label1: TLabel;
  12. MemoLeft: TMemo;
  13. MemoRight: TMemo;
  14. DropTextTarget1: TDropTextTarget;
  15. CheckBoxLeft: TCheckBox;
  16. CheckBoxRight: TCheckBox;
  17. MemoSource: TMemo;
  18. DropTextSource1: TDropTextSource;
  19. DropDummy1: TDropDummy;
  20. procedure FormDestroy(Sender: TObject);
  21. procedure CheckBoxLeftClick(Sender: TObject);
  22. procedure CheckBoxRightClick(Sender: TObject);
  23. procedure MemoSourceMouseDown(Sender: TObject; Button: TMouseButton;
  24. Shift: TShiftState; X, Y: Integer);
  25. procedure DropTextTarget1Enter(Sender: TObject;
  26. ShiftState: TShiftState; Point: TPoint; var Effect: Integer);
  27. procedure DropTextTarget1Leave(Sender: TObject);
  28. procedure DropTextTarget1Drop(Sender: TObject; ShiftState: TShiftState;
  29. Point: TPoint; var Effect: Integer);
  30. procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  31. Y: Integer);
  32. private
  33. public
  34. end;
  35. var
  36. Form1: TForm1;
  37. implementation
  38. {$R *.DFM}
  39. procedure TForm1.MemoSourceMouseDown(Sender: TObject; Button: TMouseButton;
  40. Shift: TShiftState; X, Y: Integer);
  41. begin
  42. // Wait for user to move cursor before we start the drag/drop.
  43. if (DragDetectPlus(TWinControl(Sender).Handle, Point(X,Y))) then
  44. begin
  45. DropTextSource1.Text := MemoSource.Lines.Text;
  46. DropTextSource1.Execute;
  47. end;
  48. end;
  49. procedure TForm1.FormDestroy(Sender: TObject);
  50. begin
  51. // Unregister all targets.
  52. // This is not strictly nescessary since the target component will perform
  53. // the unregistration automatically when it is destroyed. Feel free to skip
  54. // this step if you like.
  55. DropTextTarget1.Unregister;
  56. end;
  57. procedure TForm1.CheckBoxLeftClick(Sender: TObject);
  58. begin
  59. // Register or unregister control as drop target according to users selection.
  60. if TCheckBox(Sender).Checked then
  61. DropTextTarget1.Register(MemoLeft)
  62. else
  63. DropTextTarget1.Unregister(MemoLeft);
  64. end;
  65. procedure TForm1.CheckBoxRightClick(Sender: TObject);
  66. begin
  67. // Register or unregister control as drop target according to users selection.
  68. if TCheckBox(Sender).Checked then
  69. DropTextTarget1.Register(MemoRight)
  70. else
  71. DropTextTarget1.Unregister(MemoRight);
  72. end;
  73. procedure TForm1.DropTextTarget1Enter(Sender: TObject;
  74. ShiftState: TShiftState; Point: TPoint; var Effect: Integer);
  75. begin
  76. // Highlight the current drag target.
  77. // Use the TDropTarget.Target property to determine which control is
  78. // the current drop target:
  79. (TDropTarget(Sender).Target as TMemo).Color := clRed;
  80. end;
  81. procedure TForm1.DropTextTarget1Leave(Sender: TObject);
  82. begin
  83. // Remove highlight.
  84. (TDropTarget(Sender).Target as TMemo).Color := clWindow;
  85. end;
  86. procedure TForm1.DropTextTarget1Drop(Sender: TObject;
  87. ShiftState: TShiftState; Point: TPoint; var Effect: Integer);
  88. begin
  89. // Copy dragged text from target component into target control.
  90. (TDropTarget(Sender).Target as TMemo).Lines.Text := TDropTextTarget(Sender).Text;
  91. // Remove highlight.
  92. (TDropTarget(Sender).Target as TMemo).Color := clWindow;
  93. end;
  94. procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  95. Y: Integer);
  96. var
  97. i: integer;
  98. Control: TWinControl;
  99. begin
  100. // Remove highligt from all controls.
  101. for i := 0 to ComponentCount-1 do
  102. if (Components[i] is TMemo) then
  103. TMemo(Components[i]).Color := clWindow;
  104. // Demo of TDropTarget.FindTarget:
  105. // Highlight the control under the cursor if it is a drop target.
  106. Control := DropTextTarget1.FindTarget((Sender as TWinControl).ClientToScreen(Point(X,Y)));
  107. if (Control <> nil) and (Control is TMemo) then
  108. TMemo(Control).Color := clLime;
  109. end;
  110. end.