c# 自定义控件——已进度条为例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiexiangyan/article/details/78538157

c# 自定义控件——已进度条为例

第一次自定义控件,记录一下:

  • 新建项目

  • 编辑内容

  • 发布控件

新建项目

打开VS软件,点击File-new-windows-windowsformscontrollibrary,将控件名与地址设置好如下图所示:

这里写图片描述

新建好以后会出现空白的界面,可以任意添加控件进去,我这次是直接在控件中自己写事件。

这里写图片描述

添加事件代码

找到控件界面,点击属性(properties),为控件添加paint事件:

    private float m_Value = 0;
        private float m_Maximum = 100;
        private  SolidBrush m_ProgressBarFillBrush;
        private SolidBrush m_ProgressBarbackColor;
        private float text_Value =100.0f;
        private bool text_target =false;
    /// <summary>
        /// 重绘进度条控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl1_Paint(object sender, PaintEventArgs e)
        {

            Graphics dc = e.Graphics;
            Pen pn = new Pen(ForeColor);
            SolidBrush br = new SolidBrush(m_ProgressBarbackColor.Color);
            SolidBrush br1 = new SolidBrush(m_ProgressBarFillBrush.Color);
            Brush bush = new SolidBrush(ForeColor);//填充的颜色

            initCoordinates(dc, br);
            DrawProgressBar(dc, m_Value, br1);
            if (text_target)
            {
            initDrawString(dc, bush, text_Value);
            }
        }

        /// <summary>
        /// 绘制进度条背景色
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="br"></param>
        public void initCoordinates(Graphics dc, SolidBrush br)
        {
            dc.FillRectangle(br, 0, this.Height / 2, this.Width, this.Height / 2);//画矩形
        }

        /// <summary>
        /// 绘制进度条前景色
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="num"></param>
        /// <param name="br1"></param>
        private void DrawProgressBar(Graphics dc, float num, SolidBrush br1)
        {
            dc.FillRectangle(br1, 0, this.Height / 2, (this.Width) / m_Maximum * num, this.Height / 2);//画矩形
        }

        /// <summary>
        /// 绘制进度条文字值
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="bush"></param>
        /// <param name="i"></param>
        public void initDrawString(Graphics dc, Brush bush, float i)
        {
            dc.DrawString(((i / 5F) *0).ToString(), Font, bush, 0, this.Height / this.Font.Size);
            dc.DrawString((i / 5F).ToString(), Font, bush, this.Width / 5F - 15, this.Height / this.Font.Size);
            dc.DrawString(((i / 5F)*2).ToString(), Font, bush, (this.Width / 5F) * 2-15, this.Height / this.Font.Size);
            dc.DrawString(((i / 5F) * 3).ToString(), Font, bush, (this.Width / 5F) * 3-15, this.Height / this.Font.Size);
            dc.DrawString(((i / 5F) * 4).ToString(), Font, bush, (this.Width / 5F) * 4-15, this.Height / this.Font.Size);
            dc.DrawString(i.ToString() , Font, bush, (this.Width / 5F) * 5 - 35, this.Height / this.Font.Size);
        }

添加完画图事件以后,设置画好控件的各种属性:
(1).添加颜色:

       [Category("进度条颜色")]
        [Description("进度条前景色")]
        [DefaultValue(typeof(Color), "Coral")]//要定义初始值,避免null错误
        public Color ProgressBarFillColor
        {
            get
            {
                return m_ProgressBarFillBrush.Color;
            }

            set
            {
                if (m_ProgressBarFillBrush.Color != value)
                {
                    m_ProgressBarFillBrush.Color = value;
                    this.Invalidate();
                }
            }
        }

        [Category("进度条颜色")]
        [Description("进度条背景色")]
        [DefaultValue(typeof(Color), "Coral")]
        public Color ProgressBarbackColor
        {
            get
            {
                return m_ProgressBarbackColor.Color;
            }

            set
            {
                if (m_ProgressBarbackColor.Color != value)
                {
                    m_ProgressBarbackColor.Color = value;
                    this.Invalidate();
                }
            }
        }

(2)添加文字:

      [Category("进度条值")]
        [Description("进度条实时值")]
        [DefaultValue(0)]
        public float Value
        {
            get { return m_Value; }
            set
            {
                if (value < 0)
                {
                    value = 0;
                }
                else if (value > m_Maximum)
                {
                    m_Value = m_Maximum;
                }
                else
                {
                    m_Value = value;
                }

                this.Invalidate();
                this.Update();
            }
        }




        [Category("进度条值")]
        [Description("进度条最大值")]
        [DefaultValue(100)]
        public float Maximum
        {
            get { return m_Maximum; }
            set
            {
                if (m_Maximum != value)
                {
                    if (value < 1)
                    {
                        m_Maximum = 1;
                    }
                    else
                    {
                        m_Maximum = value;
                    }
                    if (m_Maximum < m_Value)
                    {
                        m_Value = m_Maximum;
                    }

                    this.Invalidate();
                    this.Update();
                }
            }
        }


        [Category("进度条值")]
        [Description("进度条文字值")]
        [DefaultValue(0)]
        public float  Valuetext
        {
            get { return text_Value; }
            set
            {  
                text_Value = value;
                text_target = true;
                this.Invalidate();
                this.Update();
            }
        }

发布控件

运行好了以后,找到源文件目录找到bin\Debug中的.dll文件,然后在新建的项目中将控件添加进去即可。

这里写图片描述

控件样式如下

这里写图片描述

猜你喜欢

转载自blog.csdn.net/xiexiangyan/article/details/78538157