gpt4 book ai didi

c# - 具有多种字体的矩形内的 DrawString 属性 c#

转载 作者:太空宇宙 更新时间:2023-11-03 14:15:52 26 4
gpt4 key购买 nike

我正在使用 C# WinForms 和 GDI+ 来做一些我希望不会有太大问题的事情,但是......

我基本上是在尝试在一个矩形内绘制一个字符串,该字符串中的部分已突出显示。在一行上打印时一切正常,但在尝试将文本换行到矩形内的下一行时出现问题。

使用的算法如下:-

将字符串拆分为高亮和不高亮的集合。

Do

If Highlightedtext Then

DrawString(HighLightedText);
Move X position forward to next character space

Else

DrawString(NormalText);
Move X position forward to next character space

End If

Loop

我会把代码放进去,但它又乱又长(我正在维护它)。它会打印出查找文本是否是突出显示或不突出显示的字符串,因为如果文本太长,它会将其包裹在矩形的边界内而不会出现问题。如果它是多重突出显示并且字符串比矩形大,它会写在它的外面......这是因为“向前移动 X 位置......”只是移动了有问题的矩形!

我想基本上移动原始矩形内打印文本的点,如果需要换行,则将其打印在下一行。有人可以帮忙吗?这真的很痛苦!

最佳答案

我已经设法通过让我的函数一次处理一个字符来解决这个问题。

为此,我创建了一个函数来获取一个 bool 值数组(它是字符串本身的长度),这些 bool 值已将所有突出显示的字符设置为 true。

private bool[] Get_CharacterArray(string text)
{
// Declare the length of the array, all set to false
bool[] characters = new bool[text.Length];

// Get the matching points
List<Point> wordLocs = FindMatchingTerms(text);
wordLocs.Sort(PointComparison);

int position = 0;
foreach (Point loc in wordLocs)
{
// We're only setting the array for matched points
for (position = loc.X; position <= loc.Y; position++)
{
characters[position] = true;
}
}

// Return the array
return characters;
}

(FindMatchingTerms() 是一个函数,它将查找字符串并将找到的匹配项返回到集合中)。

然后我循环此数组以将其绘制到屏幕上,但会跟踪我的矩形边框宽度。当它缩小到相应的尺寸时,我将绘图的位置重新设置回开始,然后将开始的Y位置向下移动一点。

private void RenderFormattedText(Graphics g, RectangleF bounds, string text, string matchText, Font font, Color colour, bool alignTextToTop)
{
const string spaceCharacter = " ";
const string hyphenCharacter = "-";
Font fr = null;
Font fi = null;
try
{
// Get teh matching characters.
bool[] charactersMatched = Get_CharacterArray(text);

// Setup the fonts and bounds.
fr = new Font(font.FontFamily, font.Size, FontStyle.Regular);
fi = new Font(font.FontFamily, font.Size, FontStyle.Bold | FontStyle.Underline);
SizeF fontSize = g.MeasureString(text, fi, 0, StringFormat.GenericTypographic);
RectangleF area = bounds;

// Loop all the characters of the phrase
for (int pos = 0; pos < charactersMatched.Length; pos++)
{
// Draw the character in the appropriate style.
string output = text.Substring(pos, 1);
if (charactersMatched[pos])
{
area.X += DrawFormattedText(g, area, output, fi, colour);
}
else
{
area.X += DrawFormattedText(g, area, output, fr, colour);
}

// Are we towards the end of the line?
if (area.X > (bounds.X + bounds.Width - 1))
{
// are we in the middle of a word?
string preOutput = spaceCharacter;
string postOutput = spaceCharacter;

// Get at the previous character and after character
preOutput = text.Substring(pos - 1, 1);
if ((pos + 1) <= text.Length)
{
postOutput = text.Substring(pos + 1, 1);
}

// Are we in the middle of a word? if so, hyphen it!
if (!preOutput.Equals(spaceCharacter) && !postOutput.Equals(spaceCharacter))
{
if (charactersMatched[pos])
{
area.X += DrawFormattedText(g, area, hyphenCharacter, fi, colour);
}
else
{
area.X += DrawFormattedText(g, area, hyphenCharacter, fr, colour);
}
}
}

// Are we at the end of the line?
if (area.X > (bounds.X + bounds.Width))
{
area.X = bounds.X;
area.Y += fontSize.Height + 2;
}
}
}
finally
{
fr.Dispose();
fi.Dispose();
}
}

希望其他人会发现这很有用 :) 我有一些 spaceCharacter 和 hypenCharacter 的常量,它们应该是不言自明的!有自定义函数来绘制字符串,但它仍然应该有意义,希望它能帮助其他人。

关于c# - 具有多种字体的矩形内的 DrawString 属性 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6598714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com