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));
        }
	}
}
相关推荐
界面开发小八哥6 小时前
DevExpress WinForms v24.1新版亮点:升级的HTML & CSS支持
css·html·界面控件·winform·devexpress·用户界面
界面开发小八哥7 天前
界面控件DevExpress中文教程:如何PDF图形对象的可见性?
ui·pdf·wpf·界面控件·winform·devexpress
大浪淘沙胡10 天前
Winform实现石头剪刀布小游戏
winform·enum·石头剪刀布
Crazy Struggle10 天前
.NET 多版本 WinForm 开源控件库 SunnyUI
c#·winform·blazor ui组件库·sunnyui
Crazy Struggle16 天前
.NET 多版本兼容的精美 WinForm UI控件库
.net·winform·控件库
界面开发小八哥17 天前
DevExpress WinForms中文教程:Data Grid - 如何自定义绘制?
ui·winform·devexpress·用户界面·c# winform
小白鼠零号19 天前
记录|自建Form窗口,实现消息提示+自动消失
c#·visual studio·winform
界面开发小八哥20 天前
DevExpress WinForms v24.1亮点- TreeList、折叠组件全新升级
.net·界面控件·winform·devexpress·ui开发
小白鼠零号20 天前
记录|Form1中嵌套Form2时的频闪问题解决[不同于常见的三部曲]
c#·visual studio·winform
小码编匠22 天前
.NET 多版本兼容的精美 WinForm UI控件库
ui·.net·winform·realtaiizor·控件库