在Delphi中的树视图中的节点的放置位置绘制线条

时间:2014-09-18 11:14:56

标签: c++ delphi delphi-xe delphi-xe5

enter image description here

如上图所示,当有人拖动树节点时,如何在树状视图中将放置位置显示为一行?

1 个答案:

答案 0 :(得分:3)

您可以在CustomDrawItem事件中绘制您的行,这是作为放置目标的节点。

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;

如果您另外从注释中想要隐藏选择,则必须将DefaultDraw设置为false并自行绘制节点文本。

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;