C# GDI小熊进度条
1、添加自定义控件winform
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace xiaoxiong
{
public partial class CustomControl1 : Control
{
private int progress;
private const int BearWidth = 50;
private const int BearHeight = 50;
public int Progress
{
get { return progress; }
set
{
if (value < 0) value = 0;
if (value > 100) value = 100;
progress = value;
this.Invalidate(); // Force a repaint
}
}
public CustomControl1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// Draw the progress bar background
Rectangle progressBarRect = new Rectangle(0, 0, this.Width, this.Height);
g.FillRectangle(Brushes.LightGray, progressBarRect);
// Calculate the width of the filled part of the progress bar
int filledWidth = (int)((double)this.Width * progress / 100);
// Draw the filled part of the progress bar
g.FillRectangle(Brushes.CornflowerBlue, new Rectangle(0, 0, filledWidth, this.Height));
// Draw the bear (simplified representation)
int bearX = this.Width - BearWidth - 10; // Position the bear to the right of the progress bar
int bearY = (this.Height - BearHeight) / 2;
g.FillEllipse(Brushes.Brown, bearX, bearY, BearWidth, BearHeight); // Bear's head
g.FillEllipse(Brushes.Black, bearX + BearWidth / 4, bearY + BearHeight / 4, BearWidth / 2, BearHeight / 2); // Bear's left eye
g.FillEllipse(Brushes.Black, bearX + BearWidth * 3 / 4, bearY + BearHeight / 4, BearWidth / 2, BearHeight / 2); // Bear's right eye
g.DrawArc(Pens.Black, bearX + BearWidth / 4, bearY + BearHeight / 2, BearWidth / 2, BearHeight / 2, 0, 180); // Bear's left ear
g.DrawArc(Pens.Black, bearX + BearWidth * 3 / 4, bearY + BearHeight / 2, BearWidth / 2, BearHeight / 2, 0, -180); // Bear's right ear
}
}
}
2、编译后添加winform窗口并找到上面的CustomControl1 控件,根据定时器即可使用该控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace xiaoxiong
{
public partial class Form1 : Form
{
private CustomControl1 bearProgressBar;
public Form1()
{
InitializeComponent();
bearProgressBar = new CustomControl1
{
Location = new Point(10, 10),
Size = new Size(300, 50),
Progress = 10 // Set initial progress
};
this.Controls.Add(bearProgressBar);
}
public int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
i=i+1;
bearProgressBar.Progress = i;
}
}
}
源码:
https://download.csdn.net/download/weixin_43050480/90091764