效果展示:
1.首先創建一個自定義控件
2.復制如下代碼,更改參數
<strong>private int _Max;//進度條的最大值
private int _Mth;//進度條現在的值
public int MaxValue
{
get
{
return this._Max;
}
set
{
this._Max = value;
}
}
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());
}
}</strong>