自定义可旋转的Label控件

using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace T.UI
{
    public  class TLabel : Label
    {
        private float _angle;
        [Browsable(true)]
        [Description("The angle to rotate the text"), Category("Appearance"), DefaultValue("0")]
        public float Angle { get { return _angle; } set { _angle = value; Invalidate(); } }

        public TLabel ()
        {
	    this.AutoSize = false;
            this.BackColor = Color.Transparent;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            float w = Width;
            float h = Height;
            //将graphics坐标原点移到矩形中心点
            g.TranslateTransform(w / 2, h / 2);
            g.RotateTransform(Angle);
            SizeF sz = g.MeasureString(Text, this.Font);
            float x = -sz.Width / 2;
            float y = -sz.Height/ 2;
            Brush brush = new SolidBrush(this.ForeColor);
            g.DrawString(Text, this.Font, brush, new PointF(x, y));
        }
    }
}


相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页