首先大家在使用微软自带的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">