天天看點

winform 自定義label

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();

            }

        }

    }

}

繼續閱讀