6

I would like to highlight the text from a node of VirtualStringTree according to search criteria like example from bellow:

enter image description here

Any suggestion please?

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
REALSOFO
  • 852
  • 9
  • 37
  • 3
    I am not aware of any build-in feature to highlight any text. You will probably need to use owner-draw methods - OnBeforeCellPaint, OnPaintText etc. – smooty86 Apr 29 '15 at 13:44
  • Do you need to have support for multiline nodes with wrapped text ? – TLama May 18 '15 at 12:20
  • Now since you mention about wrapped text, I will search for some help about. In this moment I have no idea about how to handle this, at least from where to start to looking for... Anyhow if you have some links or info that can help me is welcome. Thank you in advance! – REALSOFO May 19 '15 at 10:09
  • 1
    Briefly; VT uses `DrawText` WinAPI function for text rendering (and fires the `OnDrawText` event if assigned). That's the place where I would render the text background (e.g. making a sort of `OnBeforeDrawText` event might be useful). I'm saying so, because in earlier stages, the VT does not know anything about the text and you would be repeating what is done after the events like `OnBeforeCellPaint` fires and before the text is actually rendered. And the task itsef is not easy for wrapped text. And it's not actually related to VT, but as a generic GDI task. – TLama May 19 '15 at 11:27
  • So, do you need to have a support for multiline nodes with wrapped text ? – TLama May 20 '15 at 10:57
  • Next week I will start to work on this subject. I will dig base on the info you provide to me. If I still can find a solution, I will let you know. – REALSOFO May 20 '15 at 12:44
  • 3
    Possible duplicate of http://stackoverflow.com/q/31984898/960757 – TLama Aug 14 '15 at 00:22

1 Answers1

3

Thanks to TLama answer (How to underline or highlight a part of node caption) I adjust a bit the code in order to highlight the text also in the middle.

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;

Best wishes to TLama!

Community
  • 1
  • 1
REALSOFO
  • 852
  • 9
  • 37