使用 C# 打印时,如何在单个边界矩形内正确间隔多个字符串片段?

发布于 2024-08-06 14:25:14 字数 1333 浏览 4 评论 0原文

我正在编写一个函数,在打印字符串时将特殊格式应用于预定关键字。例如,在字符串中 - “Why won't this work?”我可能需要用蓝色下划线打印“为什么”一词。

我必须分段实现这一点,通过单独的打印调用来打印字符串的每个段。这种方法可以解决一个问题——打印字符串时我无法获得正确的间距。我的关键字打印在之前的默认文本之上,并依次与随后打印的文本重叠。

我使用边界矩形将字符串放置在打印页面上。

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);               
}

I am writing a function that applies special formatting to predetermined keywords when printing a string. For example, in the string - "Why won't this work?" I might need to print the word "Why" underlined and in blue.

I've had to implement this in pieces, printing each segment of a string with a separate call to print. This approach works with one problem - I cannot get the spacing correct when printing the strings. My keywords print over top of previous default text and are overlapped in turn by text printed afterward.

I am using bounding rectangles to place my strings on the printed page.

RectangleF rectKeywordBounds = new RectangleF( 60.0,  60.0, 550.0, 1200.0);

Once I've printed a segment of the string, I modify the size of the rectangle by the number of characters drawn and I print the next segment of the string.

EArgs.Graphics.DrawString(strFragment, fontBlueItalics, Brushes.Blue, rectKeywordBounds );
iLastPrintIndex = strFragment.Length + iLastPrintIndex;

I've used this method to change the print position of the new string segment:

rectKeywordBounds = new Rectangle(rectKeywordBounds .X + iLastPrintIndex, rectKeywordBounds .Y, rectKeywordBounds .Width, rectKeywordBounds .Height);

And I've used this one:

properSpacing = new SizeF(-((float)iLastPrintIndex), 0.0f);
rectKeywordBounds .Inflate(properSpacing);

Both methods result in the same overlapping of segments. The following code advances a bounding rectangle in the fashion I expect, so why doesn't the concept work when printing text within the rectangle?

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);               
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋心╮凉 2024-08-13 14:25:14

您没有说明如何计算矩形前进的距离 - 您似乎正在使用字符串的长度,这不起作用,因为这是字符计数而不是坐标值。

相反,您应该使用 Graphics.MeasureString 方法来计算打印当前段需要多少空间,并将矩形前进那么多。如果您对所有段使用相同的字体,您也可以使用 Graphics.MeasureCharacterRanges 一次性完成这一切。

顺便说一句,如果您可以选择使用 WPF 而不是 GDI+,那么 TextBlock 类将为您处理所有测量和布局。

You don't say how you are calculating how far to advance the rectangle -- you seem to be using the length of the string, which won't work as this is a character count rather than a coordinate value.

Instead, you should use the Graphics.MeasureString method to calculate how much space is required to print the current segment, and advance the rectangle that much. You may also be able to do this all in one go using Graphics.MeasureCharacterRanges if you are using the same font for all segments.

By the way, if you have the option to use WPF instead of GDI+, then the TextBlock class will take care of all measurement and layout for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文