TVirtualStringTree搜索结果的重点

REA*_*OFO 6 delphi virtualtreeview tvirtualstringtree

我想根据搜索条件突出显示VirtualStringTree节点中的文本,例如来自bellow的示例:

在此输入图像描述

有什么建议吗?

REA*_*OFO 3

感谢 TLama 的回答(如何下划线或突出显示节点标题的一部分),我稍微调整了代码,以便突出显示中间的文本。

procedure Tform_main.vt_mainDrawText(Sender: TBaseVirtualTree;
  TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  const Text: string; const CellRect: TRect; var DefaultDraw: Boolean);
var
  BackMode, position: Integer;
begin
  // if the just rendered node's Text contain the text written in a TEdit control
  // called Edit, then...
  position:= Pos(AnsiLowerCase(edit_search.Text), AnsiLowerCase(text));
  if position > 0 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 + TargetCanvas.TextWidth(Copy(Text, 1, position-1)),
      CellRect.Top + 3,
      CellRect.Left  + TargetCanvas.TextWidth(Copy(Text, 1, position-1)) + TargetCanvas.TextWidth(Copy(Text, position, Length(edit_search.Text))),
      CellRect.Bottom - 3)
    );
    // restore the original background mode (as it likely was modified by setting the
    // brush color)
    SetBkMode(TargetCanvas.Handle, BackMode);
  end;
end;
Run Code Online (Sandbox Code Playgroud)

祝TLama一切顺利!