Azad
Azad

Reputation: 5274

Draw line at the drop position of a node in treeview in Delphi

enter image description here

As illustrated above, when someone drags a tree node how can I show the drop position as a line in the tree view?

Upvotes: 2

Views: 827

Answers (1)

bummi
bummi

Reputation: 27377

You might paint your line in then CustomDrawItem Event for Nodes which are drop targets.

procedure TMyForm.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
var
 y:Integer;
begin
   if Node.DropTarget then
    begin
      y := Node.DisplayRect(true).Bottom;
      Sender.Canvas.MoveTo(0,y-1);
      Sender.Canvas.LineTo(Sender.Width,y-1);
      Sender.Canvas.Font.Size :=  10;
    end;
end;

If you in addition as taken, from the comments, want to hide the selection you will have to set DefaultDraw to false and paint the text of the node on your own.

procedure TMyForm.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
Const
  C_WishedAdditional = 10; // addtional lenght to the Node.DisplayRect - width
var
  r: TRect;
begin
  if Node.DropTarget then
  begin
    r := Node.DisplayRect(true);
    Sender.Canvas.MoveTo(r.Left, r.bottom - 2); // start of the line
    Sender.Canvas.Pen.Width := 3; // adjust line width
    Sender.Canvas.Pen.Color := clMaroon; // adjust line color
    Sender.Canvas.LineTo(r.Right + C_WishedAdditional, r.bottom - 2); // end of the line
    Sender.Canvas.Font.Color := clBlack;
    SetBkMode(Sender.Canvas.Handle,TRANSPARENT); // prevent text background from overpainting
    Sender.Canvas.TextOut(r.Left + 2, r.top + 1 , Node.Text);
    DefaultDraw := false;
  end;
end;

Upvotes: 3

Related Questions