【发布时间】: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;
【问题讨论】: