C# GDI创建自定义进度条

12 篇文章 0 订阅

   首先大家在使用微软自带的ProgressBar控件的话,效果太差,找不到自己想要的效果,现在我来教大家如何创建自定义的控件,废话不多说贴教程;

1.首先创建一个自定义控件

2.复制如下代码,更改参数

	private int _Max;//进度条的最大值

        private int _Mth;//进度条现在的值
	
	public int MaxValue
        {
            get
            {
                return this._Max;
            }
            set
            {
                this._Max = value;
                this.SetBlood();
            }
        }

        public int MthValue
        {
            get
            {
                return this._Mtn;
            }
            set
            {
                this._Mtn = value;
                this.SetBlood();
            }
        }

        private IContainer components = null;

        public Blood()
        {
            this.InitializeComponent();
            base.Load += new EventHandler(this.Blood_Load);
        }

        private void Blood_Load(object sender, EventArgs e)
        {
            this.SetBlood();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            base.SuspendLayout();
            base.AutoScaleMode = AutoScaleMode.None;
            this.BackColor = Color.Transparent;
            this.BackgroundImage = "//放在底层的图片";
            this.BackgroundImageLayout = ImageLayout.Center;
            this.DoubleBuffered = true;
            base.Name = "Blood";
            base.Size = new Size(int, int);//尺寸
            base.Load += new EventHandler(this.Blood_Load);
            base.ResumeLayout(false);
        }

        public void SetBlood()
        {
            try
            {
                if (base.IsHandleCreated)
                {
                    if (this.BackgroundImage != null)
                    {
                        this.BackgroundImage.Dispose();
                        this.BackgroundImage = new Bitmap("图片");
                    }
                    using (Graphics graphics = Graphics.FromImage(this.BackgroundImage))
                    {
                        if (this._Max != 0)
                        {
                            double num = ((double) this._Min) / (this._Max * 1.0);//计算位置
                            graphics.DrawImage("覆盖上去的图片", new Rectangle(0, 0, Convert.ToInt32((double) (211.0 * num)), int//尺寸), new Rectangle(0, 0, Convert.ToInt32((double) (211.0 * num)), int//), GraphicsUnit.Pixel);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

本段代码,感谢DY兄友情提醒。

原文地址点击

</pre><pre code_snippet_id="184498" snippet_file_name="blog_20140211_2_4601318" name="code" class="csharp">        

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值