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));
        }
	}
}
相关推荐
初级代码游戏7 小时前
套路化编程 C# winform 自适应缩放布局
开发语言·c#·winform·自动布局·自动缩放
Traced back2 天前
WinForms 线程安全三剑客详解
安全·c#·winform
PfCoder5 天前
WinForm真入门(23)---PictureBox 控件详细用法
开发语言·windows·c#·winform
PfCoder6 天前
C#中定时器之System.Timers.Timer
c#·.net·visual studio·winform
时光追逐者8 天前
使用 NanUI 快速创建具有现代用户界面的 WinForm 应用程序
ui·c#·.net·winform
老骥伏枥~9 天前
WinForm中的DataGridView 控件
winform
刘欣的博客13 天前
c# winform 控件dock 停造位置、摆放顺序问题
c#·winform·dock停靠问题
故事不长丨20 天前
C#log4net详解:从入门到精通,配置、实战与框架对比
c#·.net·wpf·log4net·日志·winform·日志系统
Lv117700824 天前
WinForm常用控件功能介绍及使用模板
笔记·c#·visual studio·winform
Aevget25 天前
DevExpress WinForms中文教程:Data Grid - 如何自定义行预览部分?
界面控件·winform·devexpress·ui开发·.net 10