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));
        }
	}
}
相关推荐
何以解忧唯有撸码3 天前
C# 视频录制监控系统
c#·winform
何以解忧唯有撸码14 天前
玩转控件:封装个带图片的Label控件
c#·winform·自定义控件·封装控件·控件扩展
czhc114007566318 天前
winform 4 12 winform自绘控件
winform
CSharp精选营23 天前
都是微软亲儿子,WPF凭啥干不掉WinForm?这3个场景说明白了
c#·wpf·跨平台·winform
小贺儿开发1 个月前
Unity3D 家居视频遥控效果演示
unity·udp·人机交互·网络通信·winform·远程·photon
czhc11400756631 个月前
winform 330 跨线程 异步
wpf·线程·winform
light blue bird1 个月前
原生控件GDI完成作业协同界面
jvm·数据库·.net·winform·gdi+界面
light blue bird1 个月前
MES/ERP大数据报表条件索引查询组件
数据库·.net·winform·t-sql·大数据报表
香煎三文鱼2 个月前
winform读取不到App.config配置文件中的配置信息
winform
Aevget2 个月前
界面控件DevExpress WinForms中文教程:Data Grid - 数据绑定(二)
.net·界面控件·winform·devexpress·ui开发