6
votes

I am quite new to the Dragging and Dropping System in Delphi for ListView. I found a simple solution on the internet to drag and drop items in ListView. The problem is that the code shows only dragging the first column and I want to show and drag the entire row.

You can view in the following picture what I get and what I want to get.

Dragging and Dropping in Delphi

procedure TForm1.ListView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  DragItem, DropItem, CurrentItem, NextItem: TListItem;
begin
  if Sender = Source then
    with TListView(Sender) do
    begin
      DropItem    := GetItemAt(X, Y);
      CurrentItem := Selected;
      while CurrentItem <> nil do
      begin
        NextItem := GetNextItem(CurrentItem, SdAll, [IsSelected]);
        if DropItem = nil then DragItem := Items.Add
        else
          DragItem := Items.Insert(DropItem.Index);
        DragItem.Assign(CurrentItem);
        CurrentItem.Free;
        CurrentItem := NextItem;
      end;
    end;

end;

procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := Sender = ListView1;
end;

self.ListView1.DragMode := dmAutomatic;
1

1 Answers

0
votes

I don't know how you get a snapshot of selected current row, but the drag-drop part of it is like this:

// you need a TDragControlObject:
  TPlainDragControlObject = class(TDragControlObject)
  protected
    function GetDragImages:  TDragImageList; override;
  End;
.....

Implementation

function TPlainDragControlObject.GetDragImages: TDragImageList;
var
  images : TDragImageList;
begin
  images := TDragImageList.create(self);
  // ToDo: add images - how the drag object will look like

  Result := images; // you can return Nil here if you want just the drag cursor with no image at all
end;

procedure TMainForm.lvStartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
  If Sender = ListView1 Then Begin
    DragObject := TPlainDragControlObject.Create(Sender as TListView);
  End;
end;

You can create a bitmap and manually draw the item in it.

Or here is how to make a screenshot of the whole list view (or any other component): http://delphidabbler.com/tips/24 You can figure out the item coordinates and copy it from the screenshot into a new bitmap.