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));
        }
	}
}
相关推荐
199909231 个月前
C#+Winform+SQLServer+图书管理系统
sqlserver·c#·winform
Crazy Struggle1 个月前
.NET 开发的分流抢票软件,不做广告、不收集隐私
.net·winform·分流抢票
_oP_i1 个月前
winform判断Windows 是否运行在 macOS 系统的虚拟机
windows·macos·winform
阿赵3D2 个月前
阿赵的MaxScript学习笔记分享十六《MaxScript和WinForm交互》
笔记·学习·交互·winform·dotnet·maxscript
Crazy Struggle2 个月前
WinForm 通用权限框架,简单实用支持二次开发
winform·前后端分离·权限管理系统
※※冰馨※※2 个月前
[C#与C++交互] 跨进程通信NamedPipes
开发语言·c#·winform
c#上位机3 个月前
winform跨线程更新界面
c#·winform
界面开发小八哥3 个月前
DevExpress WinForms中文教程:Data Grid - 使用服务器模式的大数据源和即时反馈?
.net·界面控件·winform·devexpress·ui开发
刘欣的博客3 个月前
.net6 WinForm EF7 sqlite 做成一个exe执行文件
.net·winform·sqlite 一个exe·ef7
xcLeigh3 个月前
C# Winform 2048小游戏源码
开发语言·c#·winform