main.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. unit main;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5. ComCtrls, StdCtrls, Grids, ExtCtrls,
  6. DropSource, DragDropText, DragDrop, DropTarget;
  7. type
  8. TFormAutoScroll = class(TForm)
  9. DropTextTarget1: TDropTextTarget;
  10. Panel1: TPanel;
  11. StringGrid1: TStringGrid;
  12. PanelSource: TPanel;
  13. Panel3: TPanel;
  14. DropTextSource1: TDropTextSource;
  15. RichEdit1: TRichEdit;
  16. procedure FormCreate(Sender: TObject);
  17. procedure PanelSourceMouseDown(Sender: TObject; Button: TMouseButton;
  18. Shift: TShiftState; X, Y: Integer);
  19. procedure DropTextTarget1Drop(Sender: TObject; ShiftState: TShiftState;
  20. Point: TPoint; var Effect: Integer);
  21. procedure DropTextTarget1Enter(Sender: TObject;
  22. ShiftState: TShiftState; Point: TPoint; var Effect: Integer);
  23. procedure DropTextTarget1DragOver(Sender: TObject;
  24. ShiftState: TShiftState; Point: TPoint; var Effect: Integer);
  25. private
  26. { Private declarations }
  27. public
  28. { Public declarations }
  29. end;
  30. var
  31. FormAutoScroll: TFormAutoScroll;
  32. implementation
  33. {$R *.DFM}
  34. uses
  35. ActiveX;
  36. procedure TFormAutoScroll.FormCreate(Sender: TObject);
  37. var
  38. i,j: integer;
  39. begin
  40. // Populate the grid with data
  41. for i := ord('A') to ord('Z') do
  42. StringGrid1.Cells[0, 1+i-ord('A')] := chr(i);
  43. for i := 0 to StringGrid1.ColCount-1 do
  44. StringGrid1.Cells[1+i, 0] := IntToStr(i);
  45. for i := ord('A') to ord('Z') do
  46. for j := 0 to StringGrid1.ColCount-1 do
  47. StringGrid1.Cells[1+j, 1+i-ord('A')] := chr(i)+IntToStr(j);
  48. end;
  49. procedure TFormAutoScroll.PanelSourceMouseDown(Sender: TObject;
  50. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  51. begin
  52. if DragDetectPlus(PanelSource.Handle, Point(X,Y)) then
  53. begin
  54. // Drag the current time of day.
  55. DropTextSource1.Text := DateTimeToStr(Now);
  56. DropTextSource1.Execute;
  57. end;
  58. end;
  59. procedure TFormAutoScroll.DropTextTarget1Enter(Sender: TObject;
  60. ShiftState: TShiftState; Point: TPoint; var Effect: Integer);
  61. var
  62. FirstCell: TRect;
  63. CustomNoScrollZone: TRect;
  64. begin
  65. (*
  66. ** Set up a custom no-scroll zone:
  67. ** 1. Get the grids client rect.
  68. ** 2. Move the top left corner to the first data cell.
  69. ** 3. Shrink the rect 1/3 or the grid cell size.
  70. *)
  71. CustomNoScrollZone := StringGrid1.ClientRect;
  72. FirstCell := StringGrid1.CellRect(StringGrid1.LeftCol,StringGrid1.TopRow);
  73. CustomNoScrollZone.TopLeft := FirstCell.TopLeft;
  74. InflateRect(CustomNoScrollZone, -StringGrid1.DefaultColWidth div 3,
  75. -StringGrid1.DefaultRowHeight div 3);
  76. TCustomDropTarget(Sender).NoScrollZone := CustomNoScrollZone;
  77. end;
  78. procedure TFormAutoScroll.DropTextTarget1DragOver(Sender: TObject;
  79. ShiftState: TShiftState; Point: TPoint; var Effect: Integer);
  80. var
  81. CellX, CellY: integer;
  82. begin
  83. // Determine if the cursor if over a data cell. If it isn't, we do not accept
  84. // a drop.
  85. StringGrid1.MouseToCell(Point.x, Point.y, CellX, CellY);
  86. if (CellX < 1) or (CellY < 1) then
  87. Effect := DROPEFFECT_NONE;
  88. end;
  89. procedure TFormAutoScroll.DropTextTarget1Drop(Sender: TObject;
  90. ShiftState: TShiftState; Point: TPoint; var Effect: Integer);
  91. var
  92. CellX, CellY: integer;
  93. begin
  94. // Determine which cell we dropped on and fill it with the dragged text.
  95. StringGrid1.MouseToCell(Point.x, Point.y, CellX, CellY);
  96. StringGrid1.Cells[CellX, Celly] := TDropTextTarget(Sender).Text;
  97. end;
  98. end.