Winform中实现会旋转的Label控件

csharp 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UserCtrlLib
{
    public class RotateLabel:Label
    {
		public RotateLabel() {
			this.AutoSize = false;
            this.BackColor = Color.Transparent;
        }
		private float rotateAngle;
        [Description("旋转角度")]
        [Category("自定义外观")]
        public float RotateAngle
		{
			get { return rotateAngle; }
			set 
			{ 
				rotateAngle = value;
				Invalidate();
			}
		}

        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(RotateAngle);
            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));
        }
	}
}
相关推荐
light blue bird6 天前
MES/ERP大数据报表条件索引查询组件
数据库·.net·winform·t-sql·大数据报表
香煎三文鱼7 天前
winform读取不到App.config配置文件中的配置信息
winform
Aevget8 天前
界面控件DevExpress WinForms中文教程:Data Grid - 数据绑定(二)
.net·界面控件·winform·devexpress·ui开发
小曹要微笑8 天前
WinForms 验证码类的实现
c#·验证码·winform·验证码类
我本梁人8 天前
Winform实现多语言切换
winform
light blue bird25 天前
产线多并发客户端指令操作场景组件
jvm·oracle·.net·winform
初级代码游戏1 个月前
套路化编程 C# winform 自适应缩放布局
开发语言·c#·winform·自动布局·自动缩放
Traced back2 个月前
WinForms 线程安全三剑客详解
安全·c#·winform
PfCoder2 个月前
WinForm真入门(23)---PictureBox 控件详细用法
开发语言·windows·c#·winform
PfCoder2 个月前
C#中定时器之System.Timers.Timer
c#·.net·visual studio·winform