Exemplo n.º 1
0
            // Calculates wrapping and places in WrappedMessage.
            public void WrapMessage(SlimDX.Direct3D9.Font overlayFont, int width)
            {
                try
                {
                    // Rectangle to store the dimensions in.
                    Rectangle rect = new Rectangle();

                    // Measure the sender string.
                    overlayFont.MeasureString(null, Sender, DrawTextFormat.SingleLine, ref rect);
                    SenderWidth = rect.Width;
                    width -= SenderWidth;

                    // List containing the split lines.
                    List<String> lines = new List<String>();

                    // The start index for the measured substring.
                    int start = 0;

                    // The length of the measured substring.
                    int length = 1;

                    // The last location of a space char.
                    int lastSpace = -1;

                    // The current substring.
                    String text;

                    // Split the message into lines.
                    while (start + length - 1 < Content.Length)
                    {
                        // Retrieve a substring of the message.
                        text = Content.Substring(start, length);

                        // Check if the last char is whitespace.
                        char c = Content[start + length - 1];
                        if (c == ' ')
                            lastSpace = length;

                        // Measure the substring.
                        overlayFont.MeasureString(null, text, DrawTextFormat.SingleLine, ref rect);

                        if (rect.Width > width)
                        {
                            // Split the message.

                            // Attempt to avoid splitting words in the middle.
                            if (length > 1)
                            {
                                if (lastSpace != -1)
                                    length = lastSpace;
                                else
                                    length--;
                            }

                            lines.Add(Content.Substring(start, length));
                            start += length;
                            length = 1;
                            lastSpace = -1;

                            continue;
                        }
                        else if (start + length >= Content.Length)
                        {
                            // Add the last line.

                            lines.Add(text);
                            break;
                        }

                        length++;
                    }

                    // Store the lines.
                    WrappedMessage = lines.ToArray();

                    // Don't re-attempt the wrapping.
                    FontUsed = overlayFont;
                }
                // Don't interrupt the drawing of the other messages.
                catch
                {
                    // Do not re-attempt.
                    WrappedMessage = new String[0];
                    FontUsed = overlayFont;
                }
            }