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));
        }
	}
}
相关推荐
Crazy Struggle6 天前
告别手动更新!WinForm 应用轻松集成自动升级功能 (HHUpdateApp)
c#·winform·自动更新
LZQqqqqo9 天前
WinForm 中 ListView 控件的实战应用与功能拓展
开发语言·microsoft·c#·winform
LZQqqqqo11 天前
WinForm 对话框的 Show 与 ShowDialog:阻塞与非阻塞的抉择
服务器·windows·microsoft·winform
weixin_4471035811 天前
winform中的listbox实现拖拽功能
c#·winform
R-G-B19 天前
【28】C# WinForm入门到精通 ——多文档窗体MDI【属性、方法、实例、源码】【多窗口重叠、水平平铺、垂直平铺、窗体传值】
c#·winform·多文档窗体mdi·多窗口重叠·水平平铺·垂直平铺·窗体传值
Crazy Struggle2 个月前
.NET 8 gRPC 实现高效100G大文件断点续传工具
文件上传·winform·.net 8.0
开箱测评小汪2 个月前
WinForms视频播放开发实战指南
winform
刚子编程2 个月前
C# WinForms 实现打印监听组件
开发语言·c#·winform·打印监听组件
CSharp精选营2 个月前
C# WinForms 实现打印监听组件
c#·winform·打印监听组件
我本梁人2 个月前
WinForms中实现Adobe PDF Reader实现旋转PDF功能
pdf·winform