在椭圆内正确显示文本

时间:2018-10-03 14:04:11

标签: c# .net winforms system.drawing

我需要在c#中正确显示绘制的椭圆内的文本,目前这是我正在使用的语言。

e.Graphics.DrawString(this.Text, base.Font, new SolidBrush(_NormalColorA), this.ClientRectangle, GetStringFormat(this.TextAlign));  

GetStringFormat函数

static internal StringFormat GetStringFormat(ContentAlignment ctrlalign)
        {
            StringFormat strFormat = new StringFormat();
            switch (ctrlalign)
            {
                case ContentAlignment.MiddleCenter:
                    strFormat.LineAlignment = StringAlignment.Center;
                    strFormat.Alignment = StringAlignment.Center;
                    break;
                case ContentAlignment.MiddleLeft:
                    strFormat.LineAlignment = StringAlignment.Center;
                    strFormat.Alignment = StringAlignment.Near;
                    break;
                case ContentAlignment.MiddleRight:
                    strFormat.LineAlignment = StringAlignment.Center;
                    strFormat.Alignment = StringAlignment.Far;
                    break;
                case ContentAlignment.TopCenter:
                    strFormat.LineAlignment = StringAlignment.Near;
                    strFormat.Alignment = StringAlignment.Center;
                    break;
                case ContentAlignment.TopLeft:
                    strFormat.LineAlignment = StringAlignment.Near;
                    strFormat.Alignment = StringAlignment.Near;
                    break;
                case ContentAlignment.TopRight:
                    strFormat.LineAlignment = StringAlignment.Near;
                    strFormat.Alignment = StringAlignment.Far;
                    break;
                case ContentAlignment.BottomCenter:
                    strFormat.LineAlignment = StringAlignment.Far;
                    strFormat.Alignment = StringAlignment.Center;
                    break;
                case ContentAlignment.BottomLeft:
                    strFormat.LineAlignment = StringAlignment.Far;
                    strFormat.Alignment = StringAlignment.Near;
                    break;
                case ContentAlignment.BottomRight:
                    strFormat.LineAlignment = StringAlignment.Far;
                    strFormat.Alignment = StringAlignment.Far;
                    break;
            }

            strFormat.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None;

            return strFormat;
        }

在下面的图片中,您可以看到问题和预期的想法吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

由于椭圆在几何上等同于线性缩放的圆,并且由于您的长轴和短轴始终与X轴和Y轴对齐,因此可以通过以下方式找到使文本可用区域最大化的内部矩形:只需将椭圆的总宽度和高度乘以sqrt(2)/ 2。

给出描述椭圆的矩形,此函数应为您的文本计算正确的布局矩形:

private static RectangleF GetEllipseInnerRect(RectangleF ellipse) {
    const float HalfSqrt2 = 0.707107f;
    var innerSize = new SizeF(ellipse.Width * HalfSqrt2, ellipse.Height * HalfSqrt2);
    return new RectangleF(
        ellipse.Left + 0.5f * (ellipse.Width - innerSize.Width),
        ellipse.Top + 0.5f * (ellipse.Height - innerSize.Height),
        innerSize.Width,
        innerSize.Height);
}