Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/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
如何使用.net图形绘制文本的分段_.net_Graphics - Fatal编程技术网

如何使用.net图形绘制文本的分段

如何使用.net图形绘制文本的分段,.net,graphics,.net,Graphics,我在datagridview单元格中进行自定义绘图,我有可以垂直跨越多个单元格的项。一个项目显示文本,当前的问题是如何仅绘制文本的单元格部分?我有项目的矩形和单元格边界 目前,我正在绘制每个单元格上的所有文本,即,我正在绘制的单元格不是当前绘制的单元格。这需要我清除前面的文本(这样它就不会变得模糊和粗体)…所以我实际上在每个单元格绘制两次字符串。效率不高 //get the actual bounds of this entire item spanning across multiple c

我在datagridview单元格中进行自定义绘图,我有可以垂直跨越多个单元格的项。一个项目显示文本,当前的问题是如何仅绘制文本的单元格部分?我有项目的矩形和单元格边界

目前,我正在绘制每个单元格上的所有文本,即,我正在绘制的单元格不是当前绘制的单元格。这需要我清除前面的文本(这样它就不会变得模糊和粗体)…所以我实际上在每个单元格绘制两次字符串。效率不高

//get the actual bounds of this  entire item spanning across multiple cells
RectangleF sRectF = GetItemRectF(startX + leftMargin + 2, widthForItem, cellBounds, calItem);

//we clear it out first, otherwise the text looks bolded if we keep drawing a black string over and over
//todo should figure out how to only draw this cells section? cellBounds subsection of sRectF somehow
graphics.DrawString(calItem.Description, new Font("Tahoma", 8), new SolidBrush(itemBackColor), sRectf);
graphics.DrawString(calItem.Description, new Font("Tahoma", 8), new SolidBrush(Color.Black), sRectF);
我可以在一些临时图形上绘制字符串,然后取出单元格边界部分并在实际图形上绘制吗?有更好的办法吗

谢谢

回答

Region tempRegion = graphics.Clip;
graphics.Clip = new Region(cellBounds);
graphics.DrawString(calItem.Description, new Font("Tahoma", 8), new SolidBrush(Color.Black), sRectF);
graphics.Clip = tempRegion;

我想我不太理解你想要的视觉效果。项目的文本应该重叠多个单元格还是剪切到单个单元格?如果要剪裁到单元格,可以使用Graphics.Clip设置剪裁区域,以剪裁到指定的矩形

如果问题与由于未清除缓冲区而导致的涂抹有关,则可以使用FillRectangle清除比图形文本更便宜的区域