C#TreeView OnDrawNode运行缓慢,正在创建工件
我正在制作一个自定义的C#TreeView,我想做一些自定义绘制来突出显示节点名称中出现的关键字 我做到了:C#TreeView OnDrawNode运行缓慢,正在创建工件,c#,.net,windows,winforms,treeview,C#,.net,Windows,Winforms,Treeview,我正在制作一个自定义的C#TreeView,我想做一些自定义绘制来突出显示节点名称中出现的关键字 我做到了: DrawMode = TreeViewDrawMode.OwnerDrawText; 在自定义树视图的构造函数中,查看并覆盖OnDrawNode: protected override void OnDrawNode(DrawTreeNodeEventArgs e) { if (!e.Node.IsVisible) { return; } if
DrawMode = TreeViewDrawMode.OwnerDrawText;
在自定义树视图的构造函数中,查看并覆盖OnDrawNode:
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
if (!e.Node.IsVisible) { return; }
if (e.Node.Bounds.IsEmpty) { return; }
e.DrawDefault = false;
...draw calls...
但在我这样编码之后,它工作起来很奇怪,感知到的行为包括:
string pattern = keyword;
if (!string.IsNullOrWhiteSpace(pattern))
{
Regex regularExpressionnew = Regex(pattern);
Match match = regularExpression.Match(e.Node.Text);
while (match.Success)
{
CaptureCollection captureCollection = match.Groups[0].Captures;
foreach (Capture capture in captureCollection)
{
int highlightStartIndex = capture.Index;
int highlightEndIndex = capture.Index + pattern.Length;
e.Graphics.FillRectangle(nodeHightLightColor, GetTextBoundsBetweenIndex(e.Graphics, e.Node.Text, highlightStartIndex, highlightEndIndex, e.Bounds));
}
match = match.NextMatch();
}
Brush drawBrush = new SolidBrush(Color.Black);
e.Graphics.DrawString(e.Node.Text, Font, drawBrush, e.Bounds);
GetTextBoundsBetweenIndex本质上是计算覆盖highlightStartIndex和HighlightdIndex之间字符的平方面积
但是,如果正则表达式被注释掉,只剩下文本呈现,则会出现延迟和重叠。Post代码重现了问题。