【问题标题】:How to draw subsection of text using .net graphics如何使用 .net 图形绘制文本的小节
【发布时间】:2008-12-31 22:53:08
【问题描述】:

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

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

//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;

【问题讨论】:

    标签: .net graphics


    【解决方案1】:

    我认为我不太了解您打算拥有的视觉效果。该项目的文本是否应该重叠多个单元格或剪辑到单个单元格?如果它应该被剪切到单元格,您可以使用 Graphics.Clip 设置剪切区域以剪切到指定的矩形。

    如果问题与由于未清除缓冲区而导致的拖尾有关,您可以使用 FillRectangle 清除区域比绘制文本更便宜。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 2021-02-24
      • 2018-07-28
      相关资源
      最近更新 更多