WinForm 自定义控件 - RooF

由3个标签组成

WinForm 自定义控件 - RooF_第1张图片

 

 

 

直接代码

    public partial class Roof : UserControl
    {
        public Roof()
        {
            InitializeComponent();
        }

        private string text = "";//中间文字
        private float valueL = 0.00f;//默认值0.00
        private float valueR = 0.00f;
        private bool _isChecked = false;//是否选中
        private int roofId = 0;


        /// 
        /// RoofId可用于作为唯一标识
        /// 
        [Description("RoofId可用于作为唯一标识"), Category("自定义")]
        public int RoofId
        {
            get
            {
                return this.roofId;
            }
            set
            {
                this.roofId = value;
            }
        }
        /// 
        /// 显示内容
        /// 
        [Description("显示内容"), Category("自定义")]
        public string ShowText
        {
            get
            {
                return this.text;
            }
            set
            {
                this.text = value;
                lbl_TxT.Text = value;
            }
        }

        /// 
        /// 左边值
        /// 
        [Description("左边值"), Category("自定义")]
        public float ValueL
        {
            get
            {
                return this.valueL;
            }
            set
            {
                this.valueL = value;
                lbl_L.Text = value.ToString();
            }
        }


        /// 
        /// 右边值
        /// 
        [Description("右边值"), Category("自定义")]
        public float ValueR
        {
            get
            {
                return this.valueR;
            }
            set
            {
                this.valueR = value;
                lbl_R.Text = value.ToString();
            }
        }


        /// 
        /// 是否选中
        /// 
        [Description("是否选中"), Category("自定义")]
        public bool IsChecked
        {
            get
            {
                return this._isChecked;
            }
            set
            {
                this._isChecked = value;

                if (value) { lbl_TxT.BorderStyle = BorderStyle.FixedSingle; } else { lbl_TxT.BorderStyle = BorderStyle.None; }

            }
        }

        private void lbl_TxT_Click(object sender, EventArgs e)
        {
            if (_isChecked)
            {
                lbl_TxT.BorderStyle = BorderStyle.None;
                _isChecked = false;
            }
            else
            {

                lbl_TxT.BorderStyle = BorderStyle.FixedSingle;
                _isChecked = true;
            }


        }


        #region 定位各个控件位置

        private void roof_SizeChanged(object sender, EventArgs e)
        {


            AutoPosition();

        }

        private void AutoPosition()
        {
            var roofHeight = this.Height;
            var roofWidth = this.Width;
            lbl_L.Width = roofWidth / 3;
            lbl_R.Width = roofWidth / 3;
            lbl_TxT.Width = roofWidth / 3;
            lbl_L.Height = roofHeight - 1;
            lbl_R.Height = roofHeight - 1;
            lbl_TxT.Height = roofHeight - 1;

            //lbl_TxT.BackColor = Color.BlueViolet;
            //lbl_R.BackColor = Color.Red;
            //lbl_L.BackColor = Color.Blue;

            this.lbl_L.Location = new System.Drawing.Point(0, roofHeight / 2 - lbl_L.Height / 2);
            this.lbl_R.Location = new System.Drawing.Point(roofWidth - lbl_R.Width, roofHeight / 2 - lbl_R.Height / 2);
            this.lbl_TxT.Location = new System.Drawing.Point(roofWidth / 3, roofHeight / 2 - lbl_TxT.Height / 2);
        }

        #endregion



        /// 
        /// 控件加载出来
        /// 
        /// 
        /// 
        private void roof_Load(object sender, EventArgs e)
        {
            lbl_R.Text = valueR.ToString();
            lbl_L.Text = valueL.ToString();
            //默认背景颜色
            this.BackColor = System.Drawing.Color.FromArgb(255, 255, 128);
            AutoPosition();
        }
    }
Roof.cs
    partial class Roof
    {
        ///  
        /// 必需的设计器变量。
        /// 
        private System.ComponentModel.IContainer components = null;

        ///  
        /// 清理所有正在使用的资源。
        /// 
        /// 如果应释放托管资源,为 true;否则为 false。
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        ///  
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// 
        private void InitializeComponent()
        {
            this.lbl_L = new System.Windows.Forms.Label();
            this.lbl_R = new System.Windows.Forms.Label();
            this.lbl_TxT = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // lbl_L
            // 
            this.lbl_L.BackColor = System.Drawing.Color.Transparent;
            this.lbl_L.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.25F);
            this.lbl_L.Location = new System.Drawing.Point(0, 0);
            this.lbl_L.Name = "lbl_L";
            this.lbl_L.Size = new System.Drawing.Size(71, 29);
            this.lbl_L.TabIndex = 0;
            this.lbl_L.Text = "50.00";
            this.lbl_L.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.lbl_L.Click += new System.EventHandler(this.lbl_TxT_Click);
            // 
            // lbl_R
            // 
            this.lbl_R.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.25F);
            this.lbl_R.Location = new System.Drawing.Point(183, 0);
            this.lbl_R.Name = "lbl_R";
            this.lbl_R.Size = new System.Drawing.Size(71, 29);
            this.lbl_R.TabIndex = 3;
            this.lbl_R.Text = "51.00";
            this.lbl_R.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.lbl_R.Click += new System.EventHandler(this.lbl_TxT_Click);
            // 
            // lbl_TxT
            // 
            this.lbl_TxT.BackColor = System.Drawing.Color.Transparent;
            this.lbl_TxT.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.25F);
            this.lbl_TxT.Location = new System.Drawing.Point(101, 3);
            this.lbl_TxT.Name = "lbl_TxT";
            this.lbl_TxT.Size = new System.Drawing.Size(46, 22);
            this.lbl_TxT.TabIndex = 5;
            this.lbl_TxT.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.lbl_TxT.Click += new System.EventHandler(this.lbl_TxT_Click);
            
            // 
            // roof
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.lbl_TxT);
            this.Controls.Add(this.lbl_R);
            this.Controls.Add(this.lbl_L);
            this.Name = "roof";
            this.Size = new System.Drawing.Size(254, 29);
            this.Load += new System.EventHandler(this.roof_Load);
            this.SizeChanged += new System.EventHandler(this.roof_SizeChanged);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Label lbl_L;
  
        private System.Windows.Forms.Label lbl_R;
        private System.Windows.Forms.Label lbl_TxT;
    }
roof.Designer.cs

效果图

WinForm 自定义控件 - RooF_第2张图片

你可能感兴趣的:(WinForm 自定义控件 - RooF)