TDragOverEvent = procedure(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean) of object;property OnDragOver: TDragOverEvent;DescriptionUse an OnDragOver event to signal that the control can accept a dragged object so the user can drop or dock it.Within the OnDragOver event handler, change the Accept parameter to False to reject the dragged object. Leave Accept as True to allow the user to drop or dock the dragged object on the control.To change the shape of the cursor, indicating that the control can accept the dragged object, change the value of the DragCursor property for the control before the OnDragOver event occurs. The Source is the object being dragged, the Sender is the potential drop or dock site, and X and Y are screen coordinates in pixels. The State parameter specifies how the dragged object is moving over the control.Note: Within the OnDragOver event handler, the Accept parameter defaults to True. However, if an OnDragOver event handler is not supplied, the control rejects the dragged object, as if the Accept parameter were changed to False.property OnEndDrag: TEndDragEvent;DescriptionUse the OnEndDrag event handler to specify any special processing that occurs when dragging stops. 
Used for OnEndDrag and OnEndDock event handlers.UnitControlstype TEndDragEvent = procedure(Sender, Target: TObject; X, Y: Integer) of object;DescriptionThe Sender is the object being dragged, Target is the object Sender is dragged to, and X and Y are screen coordinates in pixels.The OnEndDrag event is received by Sender. If the dragged object was dropped or docked and accepted by a control, the Target parameter of the OnEndDrag event is set to the object that accepted the sender. If the object was not dropped successfully, the value of Target is nil.

解决方案 »

  1.   

    procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    var
      TargetNode, SourceNode: TTreeNode;
    begin
      TargetNode := TreeView1.GetNodeAt (X, Y);
      // accept dragging from itself
      if (Source = Sender) and (TargetNode <> nil) then
      begin
        Accept := True;
        // determines source and target
        SourceNode := TreeView1.Selected;
        // look up the target parent chain
        while (TargetNode.Parent <> nil) and
            (TargetNode <> SourceNode) do
          TargetNode := TargetNode.Parent;
        // if source is found
        if TargetNode = SourceNode then
          // do not allow dragging over a child
          Accept := False;
      end
      else
        Accept := False;
    end;procedure CopyNodeUnder (TreeView: TTreeView;
      SourceNode, TargetNode: TTreeNode);
    var
      NewNode: TTreeNode;
      I: Integer;
    begin
      // add a new node
      NewNode := TreeView.Items.AddChildFirst (TargetNode, '');
      // copy the source
      NewNode.Assign (SourceNode);
      // recursively copy subitems
      for I := SourceNode.Count - 1 downto 0 do
        CopyNodeUnder (TreeView, SourceNode.Item [I], NewNode);
      // delete source after copy (move)
      TreeView.Items.Delete (SourceNode);
    end;procedure TForm1.TreeView1DragDrop(Sender, Source: TObject;
      X, Y: Integer);
    var
      TargetNode, SourceNode: TTreeNode;
    begin
      TargetNode := TreeView1.GetNodeAt (X, Y);
      if TargetNode <> nil then
      begin
        SourceNode := TreeView1.Selected;
        SourceNode.MoveTo (TargetNode, naAddChildFirst);
        TargetNode.Expand (False);
        TreeView1.Selected := TargetNode;
      end;
    end;
      

  2.   

    type TDragDropEvent = procedure(Sender, Source: TObject; X, Y: Integer) of object;
    property OnDragDrop: TDragDropEvent;DescriptionUse the OnDragDrop event handler to specify what happens when the user drops an object. The Source parameter of the OnDragDrop event is the object being dropped, and the Sender is the control the object is being dropped on. The X and Y parameters are the coordinates of the mouse positioned over the control.procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);begin
      Accept := Source is TLabel;end;This OnDragDrop event handler implements the drop behavior.procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);begin
      if (Sender is TListBox) and (Source is TLabel) then
      begin
        with Sender as TListBox do
        begin
          Font := (Source as TLabel).Font;
          Color := (Source as TLabel).Color;
        end;
      end;
    end;