如何下划线或突出显示节点标题的一部分 [英] How to underline or highlight a part of node caption
问题描述
我该怎么做?
谢谢
我将为 OnDrawText
事件,因为它是唯一的事件(在这个时候),你将被传递节点文本,文本即将被渲染的矩形以及为这样的渲染准备的画布。对于文本背景高亮,两个任务(如 OnBeforeCellPaint
或 OnAfterItemErase
有更多的适当事件,而 OnAfterCellPaint
或 OnAfterItemPaint
用于文本下划线),只是没有一个提供特定于文本的参数作为 OnDrawText
一个。
如果您的节点不会是多行的,并且不关心文本对齐,阅读方向和字符串缩短,那么您的任务可能与以下示例之一一样简单。
1。匹配文本背景颜色
过程TForm1.VirtualTreeDrawText(发件人:TBaseVirtualTree; TargetCanvas:TCanvas;
节点:PVirtualNode;列:TColumnIndex; const Text:string; const CellRect:TRect;
var DefaultDraw:Boolean);
var
BackMode:Integer;
begin
//如果刚刚呈现的节点的Text以TEdit控件
//编写的文本开头,那么...
如果StartsText(Edit.Text,文本)然后
begin
//存储当前的后台模式;我们需要使用Windows API,因为
// VT内部使用它(因此TCanvas对象与DC不同步)
BackMode:= GetBkMode(TargetCanvas.Handle);
//设置颜色并在匹配文本的宽度中绘制矩形
TargetCanvas.Brush.Color:= clYellow;
TargetCanvas.FillRect(Rect(
CellRect.Left,
CellRect.Top + 1,
CellRect.Left + TargetCanvas.TextWidth(Copy(Text,1,Length(Edit。文本))),
CellRect.Bottom - 1)
);
//恢复原始背景模式(因为可能通过设置
//刷子颜色进行修改)
SetBkMode(TargetCanvas.Handle,BackMode);
结束
结束
一个示例视觉输出:
< a href =https://i.stack.imgur.com/oXIs5.png =nofollow noreferrer>
2。匹配文本下划线
程序TForm1.VirtualTreeDrawText(发件人:TBaseVirtualTree; TargetCanvas:TCanvas;
节点:PVirtualNode;列: TColumnIndex; const Text:string; const CellRect:TRect;
var DefaultDraw:Boolean);
begin
//如果刚刚呈现的节点的Text以TEdit控件
//编写的文本开头,那么...
如果StartsText(Edit.Text,文本)然后
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
);
结束
结束
另一个示例视觉输出:
在实际代码中,我建议预先计算这些高亮形状,并在 OnDrawText
事件只绘制,但优化我会离开你;主要的一点是事件本身,我想。
I want to implement a search function in my virtualtreeview. And I want to highlight or underline the searched word in the nodes.
How can I do this? Thank you
I would write a handler for the OnDrawText
event because it's the only event (at this time) where you'll get passed the node text, the rectangle where that text is about to be rendered as well as the canvas prepared for such rendering. There are more proper events for both tasks (like OnBeforeCellPaint
, or OnAfterItemErase
for text background highlighting, and OnAfterCellPaint
or OnAfterItemPaint
for text underlining), just none of them provide text rendering specific parameters as the OnDrawText
one.
If your nodes won't be multiline and you don't care about text alignment, reading orientation, nor string shortening, then your task might be as easy as one of the following examples.
1. Matching text background color
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;
An example visual output:
2. Matching text underline
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;
And an example visual output:
In real code I'd suggest pre-calculating those highlight shapes and in the OnDrawText
event only draw, but optimization I would leave on you; the main point is the event itself, I think.
这篇关于如何下划线或突出显示节点标题的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!