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));
        }
	}
}
相关推荐
神秘的MT4 天前
C# WinForm Socket 类 常用方法 (C# .NET,TCP 场景)
服务器·网络·学习·tcp/ip·c#·winform
C囧囧5 天前
利用SharpShell为属性添加标签页
c#·winform·扩展属性·sharpshell
张人玉17 天前
C# WinForms——工厂管理系统(C# WinForms)
数据库·sqlite·c#·winform
神秘的MT24 天前
SQL Server 分页查询 ROW_NUMBER () OFFSET...FETCH游标(Cursor) 数据插入
数据库·sql·学习·c#·winform
源之缘-OFD先行者2 个月前
破界渲染:WinForm下的FFmpeg+Vortice极速推流引擎
ffmpeg·winform·推流·h264
焚 城2 个月前
Winform双语实现
c#·winform
light blue bird2 个月前
组装工序资源功能工序路径菜单组件
jvm·数据库·winform·gdi+界面·多节点端
何以解忧唯有撸码3 个月前
C# 视频录制监控系统
c#·winform
何以解忧唯有撸码4 个月前
玩转控件:封装个带图片的Label控件
c#·winform·自定义控件·封装控件·控件扩展
czhc11400756634 个月前
winform 4 12 winform自绘控件
winform