【问题标题】:TVirtualStringTree highlight of searching resultTVirtualStringTree 搜索结果高亮
【发布时间】:2015-07-08 19:55:05
【问题描述】:

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

有什么建议吗?

【问题讨论】:

  • 我不知道有任何内置功能可以突出显示任何文本。您可能需要使用所有者绘制方法 - OnBeforeCellPaint、OnPaintText 等。
  • 您是否需要支持带有换行文本的多行节点?
  • 既然您提到了包装文本,我将寻求一些帮助。在这一刻,我不知道如何处理这个问题,至少从哪里开始寻找......无论如何,如果你有一些可以帮助我的链接或信息是受欢迎的。提前谢谢!
  • 简单地说; VT 使用DrawText WinAPI 函数进行文本渲染(如果已分配则触发OnDrawText 事件)。那是我渲染文本背景的地方(例如,制作一种OnBeforeDrawText 事件可能很有用)。我这么说是因为在早期阶段,VT 对文本一无所知,您将重复在 OnBeforeCellPaint 等事件触发之后和文本实际呈现之前所做的事情。并且任务 itef 对于包装文本并不容易。而且它实际上与 VT 无关,而是作为通用 GDI 任务。

标签: delphi virtualtreeview tvirtualstringtree


【解决方案1】:

感谢 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 致以最良好的祝愿!

【讨论】:

  • 是否可以为 RightToLeft BidiMode 编辑此代码?
猜你喜欢
  • 2019-03-08
  • 2012-01-21
  • 1970-01-01
  • 2017-03-03
  • 2015-10-21
  • 1970-01-01
  • 2012-11-27
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多