Ejemplo n.º 1
0
        /// <summary>
        /// Draws the string.
        /// </summary>
        /// <param name="graphics">The graphics.</param>
        /// <param name="text">The text.</param>
        /// <param name="rect">The rect.</param>
        /// <param name="font">The font.</param>
        /// <param name="brush">The brush.</param>
        /// <param name="textAlign">The text align.</param>
        public static void DrawString(Graphics graphics, string text, Rectangle rect, Font font, Brush brush, ContentAlignment textAlign)
        {
            Size textSize = DataGridViewCell.MeasureTextSize(graphics, text, font, TextFormatFlags.SingleLine);

            float x = rect.X + 3;
            float y = rect.Y;

            switch (textAlign)
            {
            case ContentAlignment.BottomCenter:
                x += (rect.Width - textSize.Width) / 2;
                y += rect.Height - textSize.Height;
                break;

            case ContentAlignment.BottomLeft:
                y += rect.Height - textSize.Height;
                break;

            case ContentAlignment.BottomRight:
                x += rect.Width - textSize.Width;
                y += rect.Height - textSize.Height;
                break;

            case ContentAlignment.MiddleCenter:
                x += (rect.Width - textSize.Width) / 2;
                y += (rect.Height - textSize.Height) / 2;
                break;

            case ContentAlignment.MiddleLeft:
                y += (rect.Height - textSize.Height) / 2;
                break;

            case ContentAlignment.MiddleRight:
                x += rect.Width - textSize.Width;
                break;

            case ContentAlignment.TopCenter:
                x += (rect.Width - textSize.Width) / 2;
                break;

            case ContentAlignment.TopLeft:
                break;

            case ContentAlignment.TopRight:
                x += rect.Width - textSize.Width;
                break;

            default:
                break;
            }

            graphics.DrawString(text, font, brush, x, y);
        }