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

发布于 2024-11-18 21:17:41 字数 643 浏览 3 评论 0原文

我正在使用 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

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

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

I'm using C# WinForms and GDI+ to do something I hoped wouldn't be too much problem but...

I'm basically trying to draw a string within a rectangle that has highlighted sections within the string. This all works fine when printing on one line, but I have issues when trying to wrap the text onto the next line within the rectangle.

The algorithm used is as follows: -

Split strings into a collection of highlight and not highlight.

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

I would put the code in, but it's messy and long (i'm maintaining it). It'll print out find if the text is one string of either highlighting or not, as it'll wrap it within the bounds of the rectangle without issue if it's too long. If it's multiple highlighting and the string is bigger than the rectangle, it'll write outside of it... this is because the "move X position forward..." just moves the rectangle on which is a problem!

I want to essentially move the point the text is printed within the original rectangle and print it on the next line if wrapping is required. Can anyone assist with this? It's a real pain!

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

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

发布评论

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

评论(1

残疾 2024-11-25 21:17:41

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

为此,我创建了一个函数来获取布尔值数组(即字符串本身的长度),该数组已将所有突出显示的字符设置为 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 的常量,这些常量应该是不言自明的!有自定义函数来绘制字符串,但它仍然应该有意义,希望它对其他人有帮助。

I've managed to sort this by having to make my function do one character at a time.

To do this, I made a function to get an array (which is the length of the string itself) of boolean values which have set any highlighted characters to 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() is a function that will look in the string and return the matches found into a collection).

I then loop this array to draw it out to screen but keeping track of my rectangle border width. When it reduces to the relevant size, I reset the position of drawing back to the start and then move the starting Y position down a bit.

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

Hopefully someone else will find this useful :) I've got some constants in there for spaceCharacter and hypenCharacter which should be self explanatory! There are custom functions to draw the string, but it should make sense nonetheless, hope it helps anyone else.

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