首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有多种字体的矩形内的DrawString属性c#

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

Stack Overflow用户
提问于 2011-07-06 23:11:09
回答 1查看 3K关注 0票数 2

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

我基本上是试图在一个矩形内绘制一个字符串,该矩形中突出显示了字符串中的部分。在一行上打印时,这一切都很好,但在尝试将文本换行到矩形内的下一行时,我遇到了问题。

所使用的算法如下:

将字符串拆分为突出显示和不突出显示的集合。

代码语言:javascript
复制
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

我会把代码放进去,但是它又乱又长(我正在维护它)。它将打印出find文本是否为高亮显示的字符串,因为如果文本太长,它将在矩形的边界内对其进行换行。如果它是多重高亮显示,并且字符串比矩形大,它将在它的外部写入...这是因为“将X位置向前移动...”只是移动有问题的矩形!

如果需要换行,我希望将打印文本的点移动到原始矩形中,并将其打印到下一行。有人能帮上忙吗?这真让人头疼!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-11 16:27:04

我已经设法通过让我的函数一次处理一个字符来对其进行排序。

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

代码语言:javascript
复制
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位置向下移动一点。

代码语言:javascript
复制
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提供了一些常量,它们应该是不言而喻的!有一些自定义函数可以绘制字符串,但无论如何,它应该是有意义的,希望它能对其他人有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6598714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档