delphi - TVirtualStringTree 搜索结果高亮

标签 delphi virtualtreeview tvirtualstringtree

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

enter image description here

请问有什么建议吗?

最佳答案

感谢 TLama 的回答(How to underline or highlight a part of node caption),我稍微调整了代码,以便突出显示中间的文本。

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;

向TLama致以最美好的祝愿!

关于delphi - TVirtualStringTree 搜索结果高亮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29945274/

相关文章:

delphi - 印地 9 和印地 10 有什么区别?

Delphi:首次单击后获取VirtualStringTree的编辑模式

delphi - TVirtualStringTree,树结构的位置

c++ - TVirtualStringGrid CopyToClipboard 问题 Embarcadero Seattle C++

delphi - 接口(interface)和属性

delphi - Delphi 中重载记录的隐式转换作为 const 数组中的参数

delphi - 当我在自己的 OnClick 处理程序中销毁按钮时,为什么我的程序会崩溃?

delphi - 虚拟 TreeView 拖动

delphi - 无限滚动 VirtualTreeView

delphi - 如何让TVirtualStringTree在禁用状态下显示图标?