如何下划线或突出显示节点标题的一部分

时间:2015-08-13 09:51:25

标签: delphi virtualtreeview

我想在virtualtreeview中实现搜索功能。我想强调或强调节点中搜索到的单词。

我该怎么做? 谢谢

2 个答案:

答案 0 :(得分:6)

我会为OnDrawText事件编写一个处理程序,因为它是唯一一个传递节点文本的事件(即此时),即将要呈现该文本的矩形以及画布准备这样的渲染。两个任务都有更多适当的事件(例如OnBeforeCellPaintOnAfterItemErase用于文本背景突出显示,OnAfterCellPaintOnAfterItemPaint用于文本下划线),只是没有提供任何文本将特定参数渲染为OnDrawText

如果您的节点不是多行的,并且您不关心文本对齐,阅读方向和字符串缩短,那么您的任务可能就像以下示例之一一样简单。

1。匹配文本背景颜色

procedure TForm1.VirtualTreeDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
  Node: PVirtualNode; Column: TColumnIndex; const Text: string; const CellRect: TRect;
  var DefaultDraw: Boolean);
var
  BackMode: Integer;
begin
  // if the just rendered node's Text starts with the text written in a TEdit control
  // called Edit, then...
  if StartsText(Edit.Text, Text) then
  begin
    // store the current background mode; we need to use Windows API here because the
    // VT internally uses it (so the TCanvas object gets out of sync with the DC)
    BackMode := GetBkMode(TargetCanvas.Handle);
    // setup the color and draw the rectangle in a width of the matching text
    TargetCanvas.Brush.Color := clYellow;
    TargetCanvas.FillRect(Rect(
      CellRect.Left,
      CellRect.Top + 1,
      CellRect.Left + TargetCanvas.TextWidth(Copy(Text, 1, Length(Edit.Text))),
      CellRect.Bottom - 1)
    );
    // restore the original background mode (as it likely was modified by setting the
    // brush color)
    SetBkMode(TargetCanvas.Handle, BackMode);
  end;
end;

示例视觉输出:

enter image description here

2。匹配文字下划线

procedure TForm1.VirtualTreeDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
  Node: PVirtualNode; Column: TColumnIndex; const Text: string; const CellRect: TRect;
  var DefaultDraw: Boolean);
begin
  // if the just rendered node's Text starts with the text written in a TEdit control
  // called Edit, then...
  if StartsText(Edit.Text, Text) then
  begin
    TargetCanvas.Pen.Color := clRed;
    TargetCanvas.MoveTo(CellRect.Left, CellRect.Bottom - 2);
    TargetCanvas.LineTo(
      CellRect.Left + TargetCanvas.TextWidth(Copy(Text, 1, Length(Edit.Text))),
      CellRect.Bottom - 2
    );
  end;
end;

示例视觉输出:

enter image description here

在实际代码中,我建议预先计算这些高光形状,并在OnDrawText事件中仅绘制,但优化我会留给你;我认为重点是事件本身。

答案 1 :(得分:0)

很少修改。注意if。

var
  BackMode: integer;
begin
  inherited;

  // if the just rendered node's Text starts with the text written in a TEdit control
  // called Edit, then...
  if StartsText(Sender.SearchBuffer, Text) and (Node = Sender.FocusedNode) then
  begin
    TargetCanvas.Pen.Color := clRed;
    TargetCanvas.MoveTo(CellRect.Left, CellRect.Bottom - 2);
    TargetCanvas.LineTo(
      CellRect.Left + TargetCanvas.TextWidth(Copy(Text, 1, Length(Sender.SearchBuffer))),
      CellRect.Bottom - 2
    );
  end;
end;