我想在我的virtualtreeview中实现搜索功能。我想在节点中突出显示或加下划线。
我怎样才能做到这一点?谢谢
我将为该OnDrawText
事件编写一个处理程序,因为(这是此时)您将传递节点文本,将要渲染该文本的矩形以及为此渲染准备的画布,这是唯一的事件。两项任务都有更多适当的事件(例如OnBeforeCellPaint
,或OnAfterItemErase
用于文本背景突出显示和/OnAfterCellPaint
或OnAfterItemPaint
用于文本下划线),只是它们中没有一个提供特定于文本呈现的参数OnDrawText
。
如果您的节点不是多行的,并且您不关心文本对齐,阅读方向或字符串缩短,那么您的任务可能与以下示例之一一样简单。
procedure TForm1.VirtualTreeDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
Node: PVirtualNode; Column: TColumnIndex; const Text: string; const CellRect: TRect;
var DefaultDraw: Boolean);
var
BackMode: Integer;
begin
// if the just rendered node's Text starts with the text written in a TEdit control
// called Edit, then...
if StartsText(Edit.Text, Text) 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,
CellRect.Top + 1,
CellRect.Left + TargetCanvas.TextWidth(Copy(Text, 1, Length(Edit.Text))),
CellRect.Bottom - 1)
);
// restore the original background mode (as it likely was modified by setting the
// brush color)
SetBkMode(TargetCanvas.Handle, BackMode);
end;
end;
视觉输出示例:
procedure TForm1.VirtualTreeDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas;
Node: PVirtualNode; Column: TColumnIndex; const Text: string; const CellRect: TRect;
var DefaultDraw: Boolean);
begin
// if the just rendered node's Text starts with the text written in a TEdit control
// called Edit, then...
if StartsText(Edit.Text, Text) then
begin
TargetCanvas.Pen.Color := clRed;
TargetCanvas.MoveTo(CellRect.Left, CellRect.Bottom - 2);
TargetCanvas.LineTo(
CellRect.Left + TargetCanvas.TextWidth(Copy(Text, 1, Length(Edit.Text))),
CellRect.Bottom - 2
);
end;
end;
以及示例视觉输出:
在实际代码中,我建议您预先计算那些高光形状,OnDrawText
以防万一只能画图,但是优化将由您自己决定;我想重点是事件本身。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句