c#窗体中的标签对齐方式

时间:2016-04-21 07:34:39

标签: c# label windows-forms-designer text-alignment

我有一个标签,我想要自上而下对齐

像这样:

enter image description here

是否可以将标签文本设为上下对齐?

1 个答案:

答案 0 :(得分:4)

C#中的纵向文本并不难。只需将RotateTransform应用于Graphics对象并使用DrawString绘制文本。

using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

namespace Vertical_label
{
    public partial class Vertical_label: Form
    {
        public Vertical_label()
        {
            InitializeComponent();
        }
  private void label1_Paint(object sender, PaintEventArgs e)
        {

            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

            using (Font the_font = new Font("Microsoft Sans Serif", 10 , FontStyle.Bold))
            {
                int x = 5, y = 50;
                DrawRotatedTextAt(e.Graphics, -90, "DATA", x, y, the_font, Brushes.Black);

            }
        }
 private void DrawRotatedTextAt(Graphics gr, float angle, string txt, int x, int y, Font the_font, Brush the_brush)
        {
            // Save the graphics state.
            GraphicsState state = gr.Save();
            gr.ResetTransform();

            // Rotate.
            gr.RotateTransform(angle);

            // Translate to desired position. Be sure to append
            // the rotation so it occurs after the rotation.
            gr.TranslateTransform(x, y, MatrixOrder.Append);

            // Draw the text at the origin.
            gr.DrawString(txt, the_font, the_brush, 0, 0);

            // Restore the graphics state.
            gr.Restore(state);
        }
     }
   }
 }

<强>结果:

enter image description here