«« Site Rescued »»

While the site is still going to move from its current host, a new site will now take its place. More Info.

The new site may have less content, but the core will now remain. And it will now play nicely with phones! Keep an eye on the DelphiDabbler Blog for news.

» Help

How to change the drag cursor image when dragging in a TTreeView

   
Question
When I start a drag and drop operation in a TTreeView the cursor changes into the image of the treenode dragged. How can I change this image?

There is a virtual protected method called GetDragImages in the TCustomTreeView class. You could override it and return your copy of the drag images list (TDragImageList), or just use an existing one, but make some changes to its default image. Below is an example of how you could solve it. It's a TTreeView descendant which provides custom drag images.

{ ... }
  TMyTreeView = class(TTreeView)
  protected
    function GetDragImages: TDragImageList; override;
  end;
{ ... }

function TMyTreeView.GetDragImages: TDragImageList;
var
  XNode: TTreeNode;
  XImage: TBitMap;
begin
  Result := inherited GetDragImages;
  if Assigned(Result) and Assigned(Images) then
  begin
    XImage := TBitMap.Create;
    try
      Result.GetBitmap(0, XImage);
      Result.Clear;
      Result.Height := 100;
      Result.Width := 100;
      XImage.Height := Result.Height;
      XImage.Width := Result.Width;
      XImage.Canvas.Brush.Color := clRed;
      XImage.Canvas.FillRect(Rect(0, 0, 100, 100));
      XNode := Selected;
      if Assigned(XNode) then
        XImage.Canvas.TextOut(40, 40, XNode.Text);
      Result.Add(XImage, nil);
    finally
      XImage.Free;
    end;
  end;
end;
Original resource: The Delphi Pool
Author: Serge Gubenko
Added: 2009-10-28
Last updated: 2009-10-28

« Return to contents »