winform 自定义label

来源:互联网 发布:mysql数据库登陆 编辑:程序博客网 时间:2024/06/01 07:44
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TomWinform.CustomerControl
{
    public partial class VistaLabel : Control
    {
        private StringAlignment verticalAlignment = StringAlignment.Center;
        private StringAlignment horizontalAlignment = StringAlignment.Center;
        private Color lineColor = Color.Black;
        private Color leftLineColor = Color.Black;
        private Color RightLineColor = Color.Black;
        private Color TopLineColor = Color.Black;
        private Color BottomLineColor = Color.Black;

        public VistaLabel()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            StringFormat format = new StringFormat();
            format.Alignment = horizontalAlignment;
            format.LineAlignment = verticalAlignment;
            SizeF fontSize = pe.Graphics.MeasureString(this.Text, this.Font);
            this.Height = (int)fontSize.Height + 20;
            this.Width = (int)fontSize.Width + 20;
            pe.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), this.ClientRectangle, format);
            DrawBorder(pe);
            base.OnPaint(pe);
            
        }

        //画边框
        private void DrawBorder(PaintEventArgs pe)
        {
            Graphics g = pe.Graphics;
            Pen pen = new Pen(LineColor);
            Point[] point = new Point[4];
            point[0] = new Point(0, 0);
            point[1] = new Point(Width - 1, 0);
            point[2] = new Point(Width - 1, Height - 1);
            point[3] = new Point(0, Height - 1);
            g.DrawPolygon(pen, point);
        }

        //画左边框
        private void DrawLeftBorder(PaintEventArgs pe)
        {
            Graphics g = pe.Graphics;
            Pen pen = new Pen(leftLineColor);
            Point[] point = new Point[2];
            point[0] = new Point(0, 0);
            point[1] = new Point(0, this.Height - 1);
            g.DrawPolygon(pen, point);
        }

        //画右边框
        private void DrawRightBorder(PaintEventArgs pe)
        {

        }

        [Description("文本垂直对齐方式"), Category("自定义属性")]
        public StringAlignment VerticalAlignment
        {
            get
            {
                return verticalAlignment;
            }
            set
            {
                verticalAlignment = value;
                this.Invalidate();
            }
        }

        [Description("文本水平对齐方式"), Category("自定义属性")]
        public StringAlignment HorizontalAlignment
        {
            get
            {
                return horizontalAlignment;
            }
            set
            {
                horizontalAlignment = value;
                this.Invalidate();
            }
        }

        [Description("边框颜色"), Category("自定义属性")]
        public Color LineColor
        {
            get
            {
                return lineColor;
            }
            set
            {
                lineColor = value;
                this.Invalidate();
            }
        }



    }
}

原创粉丝点击