VirtualStringTree - 同一多行节点中不同格式的文本

问题描述 投票:0回答:1
我有一个

TVirtualStringTree

 的实例,它以两种不同的方式格式化节点的文本。该实现基于在 
toShowStaticText
 中使用 
StringOptions
,如本问题的接受答案中所述:
VirtualTreeView - 同一个节点中文本的不同颜色

在我为节点设置

MultiLine

 标志之前,一切都工作正常。现在,具有 
OnPaintText
TextType = ttStatic
 事件将不再被触发。

此行为的原因可以在

TCustomVirtualStringTree.DoPaintNode

 方法中找到,并且显然是有意为之:

// ... and afterwards the static text if not centered and the node is not multiline enabled. if (Alignment <> taCenter) and not (vsMultiline in PaintInfo.Node.States) and (toShowStaticText in TreeOptions.FStringOptions) then begin S := ''; with PaintInfo do DoGetText(Node, Column, ttStatic, S); if Length(S) > 0 then PaintStaticText(PaintInfo, TextOutFlags, S); end;

尽管如此,我希望在同一个

MultiLine

 节点中有两种不同的文本格式。我怎样才能做到这一点?

delphi virtualtreeview tvirtualstringtree
1个回答
0
投票
我通过使用

OnDrawText

 和这个辅助函数解决了这个问题:

class function TFontSizeHelper.TrueFontSize(fnt : TFont; const text : string): Winapi.Windows.TSize; var dc : hdc; begin dc := GetDC(0); SelectObject(DC, fnt.Handle); GetTextExtentPoint32(dc, PChar(text), Length(text), Result); ReleaseDC(0, DC); end; ... procedure TMyForm.VstDrawText(Sender : TBaseVirtualTree; TargetCanvas : TCanvas; Node : PVirtualNode; Column : TColumnIndex; const Text : string; const CellRect : TRect; var DefaultDraw : Boolean); const TREEVIEW_FONTSPACE=2; var ln1, ln2 : string; lineBegin : Integer; size : Winapi.Windows.TSize; begin case Column of 0 : begin DefaultDraw := False; lineBegin := Text.IndexOf(CRLF); ln1 := Text.Substring(0, lineBegin); TargetCanvas.TextOut(CellRect.Left, TREEVIEW_FONTSPACE, ln1); ln2 := Text.Substring(lineBegin + 2); TargetCanvas.Font.Color := clPurple; size := TFontSizeHelper.TrueFontSize(TargetCanvas.Font, ln1); // TargetCanvas.Font.style := [fsBold, fsUnderline]; TargetCanvas.TextOut(CellRect.Left, size.cy, ln2); end; end; end;

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.