merlinzjl

导航

Winform 重绘控件 得到一个竖直显示的label标签

    public class ZJLLabel:Control
    {
        private string _textContent = "";

        [Description("文本内容"), Category("自定义")]
        public string TextContent
        {
            get { return _textContent; }
            set
            {
                _textContent = value;
                Refresh();
            }
        }

        public ZJLLabel()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;
            SolidBrush brush = new SolidBrush(ForeColor);
            int controlW = Width;
            int controlH = Height;

            //逆时针旋转90度,即坐标系被逆时针旋转了90度
            Matrix matrix = g.Transform;
            matrix.RotateAt(-90, new PointF(0, 0));
            g.Transform = matrix;

            //
            int xOffset = 0 - controlH;
            int yOffset = 0;

            g.DrawString(_textContent, Font, brush, xOffset, yOffset);
            brush.Dispose();
        }
    }

设置控件属性的"文本内容"为需要显示的内容即可。

显示效果:

 

posted on 2020-12-18 13:19  merlinzjl  阅读(617)  评论(0编辑  收藏  举报