Exemplo n.º 1
0
        public static void MeasureTexts(
            this Graphics g, TextWithInfo[] texts, Rectangle bounds)
        {
            if (g == null)
                throw new ArgumentNullException("g");
            if (texts == null)
                throw new ArgumentNullException("texts");
            if (texts.Length == 0)
                return;

            // Shrink the bounding rectangle to exclude the leading and
            // trailing paddings when rendering text.
            int padding = g.GetTextPadding(texts[0].Font);
            bounds = new Rectangle(
                bounds.Left + padding,
                bounds.Top,
                bounds.Width - padding * 2,
                bounds.Height);

            TextFormatFlags flags =
                TextFormatFlags.NoPadding |
                TextFormatFlags.SingleLine |
                TextFormatFlags.PreserveGraphicsClipping |
                //TextFormatFlags.GlyphOverhangPadding |
                //TextFormatFlags.ExternalLeading |
                TextFormatFlags.VerticalCenter;

            for (int i = 0; i < texts.Length; i++)
            {
                Size size = TextRenderer.MeasureText(
                    g, texts[i].Text, texts[i].Font, bounds.Size, flags);

                texts[i].Bounds = new Rectangle(
                    bounds.X,
                    bounds.Y + (bounds.Height - size.Height) / 2,
                    Math.Min(size.Width, bounds.Width),
                    size.Height);

                bounds = new Rectangle(
                    bounds.X + size.Width,
                    bounds.Y,
                    Math.Max(0, bounds.Width - size.Width),
                    bounds.Height);
            }
        }
Exemplo n.º 2
0
        public static void DrawTexts(this Graphics g, TextWithInfo[] texts)
        {
            if (g == null)
                throw new ArgumentNullException("g");
            if (texts == null)
                throw new ArgumentNullException("texts");

            TextFormatFlags flags =
                TextFormatFlags.NoPadding |
                TextFormatFlags.SingleLine |
                TextFormatFlags.PreserveGraphicsClipping |
                //TextFormatFlags.GlyphOverhangPadding |
                //TextFormatFlags.ExternalLeading |
                TextFormatFlags.VerticalCenter;

            for (int i = 0; i < texts.Length; i++)
            {
                if (texts[i].Bounds.Width > 0)
                {
                    TextRenderer.DrawText(g, texts[i].Text, texts[i].Font, texts[i].Bounds, texts[i].Color, flags);
                }
            }
        }