Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么使用CellPaint方法向datagridview单元格添加彩色矩形会使单元格中的文本看起来模糊?_C#_Datagridview_Graphics - Fatal编程技术网

C# 为什么使用CellPaint方法向datagridview单元格添加彩色矩形会使单元格中的文本看起来模糊?

C# 为什么使用CellPaint方法向datagridview单元格添加彩色矩形会使单元格中的文本看起来模糊?,c#,datagridview,graphics,C#,Datagridview,Graphics,为了突出显示带有彩色背景的datagridview单元格中的单词,我找到了一些使用datagridview的“CellPaint”方法的代码(可能在这里)。它在文本中找到单词,并在其后面放置彩色背景。 这很好,但最近我修改了代码,这样它就可以突出显示多个单词(每个单词都有不同的颜色)。这同样有效,但我注意到,当我有更多高亮显示的单词时,文本会变得模糊(见图) 我似乎不知道为什么。这是性能问题吗?还是文本打印为双。。。我似乎不知道为什么 下面我将代码包含在CellPaint方法中 编辑:多亏了S

为了突出显示带有彩色背景的datagridview单元格中的单词,我找到了一些使用datagridview的“CellPaint”方法的代码(可能在这里)。它在文本中找到单词,并在其后面放置彩色背景。 这很好,但最近我修改了代码,这样它就可以突出显示多个单词(每个单词都有不同的颜色)。这同样有效,但我注意到,当我有更多高亮显示的单词时,文本会变得模糊(见图)

我似乎不知道为什么。这是性能问题吗?还是文本打印为双。。。我似乎不知道为什么

下面我将代码包含在CellPaint方法中

编辑:多亏了Stefan的回答,我错看了我的书!“e.PaintContent(e.CellBounds);”应该在for循环之外。这是我的意图,但放错地方了。下面的代码已更改,现在它可以正常工作而不模糊。我在下面的代码中留下了一行错误的注释

private void dataGridView1\u CellPainting(对象发送方,DataGridViewCellPaintingEventArgs e)
{
//强光和搜索应用于网格的选择性区域。
如果(e.RowIndex>-1和&e.ColumnIndex>-1)
{
//检查数据以进行搜索
if(HighlightText?.Count>0&&HighlightColor?.Count>0&&HighlightColor.Count>=HighlightText.Count)
{
字符串gridCellValue=e.FormattedValue.ToString();
int startIndexInCellValue;
//将搜索文本的索引检查到网格单元格中。
bool BackgroundPainted=假;
for(int i=0;i=0)
{
如果(背景色==假)
{
e、 已处理=正确;
e、 绘画背景(如CellBounds,真);
背景彩绘=真实;
}
//高光矩形
矩形hl_rect=新矩形();
hl_rect.Y=e.CellBounds.Y+2;
hl_rect.Height=e.CellBounds.Height-5;
//在网格单元数据中查找搜索词之前的文本大小。
字符串sbeforearchword=gridCellValue.Substring(0,startIndexInCellValue);
//网格单元数据中搜索词的大小
字符串sSearchWord=gridCellValue.Substring(startIndexInCellValue,HighlightText[i].Length);
Size s1=TextRenderer.MeasureText(e.Graphics,sbeforeResearchWord,e.CellStyle.Font,e.CellBounds.Size);
大小s2=TextRenderer.MeasureText(e.Graphics,ssearch-word,e.CellStyle.Font,e.CellBounds.Size);
如果(s1.宽度>5)
{
hl_rect.X=e.CellBounds.X+s1.Width-5;
hl_rect.Width=s2.Width-6;
}
其他的
{
hl_rect.X=e.CellBounds.X+2;
hl_rect.Width=s2.Width-6;
}
SolidBrush hl_brush=新的SolidBrush(HighlightColor[i]);
//在搜索词后面绘制背景
e、 图形.圆角矩形(hl_笔刷,hl_矩形);
hl_刷。处置();
}
//这是错误的立场
//if(BackgroundPainted){e.PaintContent(e.cellbunds);}
}
//这是正确的位置
if(BackgroundPainted){e.PaintContent(e.cellbunds);}
}
}
}

是的,我认为通过调用

e.PaintContent(e.CellBounds)
循环的每一次迭代。这很可能会绘制文本和高光。因此,当突出显示两个部分时,文本也会被绘制两次


解决方法是将文本单独绘制一次。

在设置它之前是否测试
startIndexInCellValue
?我设置它并在同一行中测试它。啊,对。绝对是糟糕的编码风格。它不会赢得任何美容奖。我不记得我为什么那样做了。可能Q&D在修补代码时会发生变化……因此解决方案是将行
if(BackgroundPainted){e.PaintContent(e.cellbunds);}
移动到上一个/内部循环中,直接在Dispose.IMHO之后。解决方案是拆分绘制文本和绘制背景。但它已经拆分了!移动那条线可以消除所有的双重绘画。天哪!你完全正确。
if(BackgroundPainted){e.PaintContent(e.cellbunds);}
需要向下移动一个“}”。它应该在for循环之外。这是我的意图,但我把它放错地方了!我应该看到这个。我将编辑我的问题代码,指出这一点!谢谢你的回复!