具有多个字体的矩形中的DrawString属性c#

具有多个字体的矩形中的DrawString属性c#,c#,winforms,gdi+,gdi,system.drawing,C#,Winforms,Gdi+,Gdi,System.drawing,我正在使用C#WinForms和GDI+做一些我希望不会有太大问题的事情,但是 我基本上是在一个矩形中画一个字符串,该矩形在字符串中有突出显示的部分。当在一行上打印时,这一切都很好,但是当我试图将文本包装到矩形内的下一行时,会遇到问题 使用的算法如下所示:- 将字符串拆分为高亮显示和非高亮显示的集合 Do If Highlightedtext Then DrawString(HighLightedText); Move X position forward to next

我正在使用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位置…”只会移动矩形,这是一个问题


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

我通过让函数一次只处理一个字符来对其进行排序

为此,我创建了一个函数来获取布尔值的数组(即字符串本身的长度),该数组将所有高亮显示的字符设置为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;
    }
private bool[]获取字符数组(字符串文本)
{
//声明数组的长度,全部设置为false
bool[]字符=新bool[text.Length];
//获取匹配点
列出单词locs=FindMatchingTerms(文本);
排序(点比较);
int位置=0;
foreach(点位置以wordLocs表示)
{
//我们只为匹配点设置数组
对于(位置=loc.X;位置(bounds.X+bounds.Width-1))
{
我们在一个词中间吗?
字符串预输出=空格字符;
字符串postOutput=空格字符;
//获取前一个字符和后一个字符
预输出=文本。子字符串(位置-1,1);
如果((位置+1)(边界X+边界宽度))
{
面积.X=边界.X;
面积.Y+=fontSize.Height+2;
}
}
}
最后
{
fr.Dispose();
fi.Dispose();
}
}

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

如何按照常规字符串的方式管理正确的单词包装?我没有看到任何与文字包装相关的代码?抱歉延迟,我的帐户登录失败!我会根据给定的边界进行换行,所以它应该这样做。
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();
                }
            }