【发布时间】:2010-12-01 05:49:33
【问题描述】:
我正在编写一个函数,该函数在打印字符串时将特殊格式应用于预先确定的关键字。例如,在字符串中 - “为什么这不起作用?”我可能需要用蓝色下划线打印“为什么”这个词。
我不得不分段实现这一点,通过单独的 print 调用打印字符串的每一段。这种方法适用于一个问题 - 打印字符串时我无法获得正确的间距。我的关键字打印在以前的默认文本之上,并与之后打印的文本依次重叠。
我正在使用边界矩形将字符串放置在打印页面上。
RectangleF rectKeywordBounds = new RectangleF( 60.0, 60.0, 550.0, 1200.0);
打印一段字符串后,我根据绘制的字符数修改矩形的大小,然后打印字符串的下一段。
EArgs.Graphics.DrawString(strFragment, fontBlueItalics, Brushes.Blue, rectKeywordBounds );
iLastPrintIndex = strFragment.Length + iLastPrintIndex;
我已经用这个方法改变了新字符串段的打印位置:
rectKeywordBounds = new Rectangle(rectKeywordBounds .X + iLastPrintIndex, rectKeywordBounds .Y, rectKeywordBounds .Width, rectKeywordBounds .Height);
我用过这个:
properSpacing = new SizeF(-((float)iLastPrintIndex), 0.0f);
rectKeywordBounds .Inflate(properSpacing);
这两种方法都会导致相同的片段重叠。以下代码以我期望的方式推进一个边界矩形,那么为什么在矩形内打印文本时这个概念不起作用?
Rectangle rectKeywordBounds = new Rectangle(90, 90, 800, 100);
for (int x = 0; x < 6; x++)
{
EventArgs.Graphics.DrawRectangle(Pens.BlueViolet, rectKeywordBounds );
rectKeywordBounds = new Rectangle(rectKeywordBounds .X + 15, rectKeywordBounds .Y + 200, rectKeywordBounds .Width, rectKeywordBounds .Height);
}
【问题讨论】:
标签: c# printing string-formatting bounding-box