自定义进度条类,Winform自定义控件,如果有兴趣,可以自己参考写一个WPF版的。当然,Winform的控件在WPF上也是可以使用的。
自定义用户控件
//CircularProgressBar.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace Insulin.Client.Circle
{
public class CircularProgressBar : Control
{
private Color mainColor = Color.LimeGreen;
public string Strtxt = "";
public int Num = 0;
public Color MainColor
{
get { return mainColor; }
set
{
mainColor = value;
this.Invalidate();
}
}
private double value = 20.00;
public double Value
{
get { return this.value; }
set
{
this.value = value;
this.Invalidate();
}
}
private int lastWidth = 0;
private int lastHeight = 0;
private Point lastLocation = Point.Empty;
public CircularProgressBar(string strtxt,int num)
{
Strtxt = strtxt;
Num = num;
this.SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.Selectable |
ControlStyles.ContainerControl |
ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.Opaque, false);
this.UpdateStyles();
this.BackColor = ColorTranslator.FromHtml("#FF87D68A");
}
protected override void OnHandleCreated(EventArgs e)
{
this.lastWidth = this.Width;
this.lastHeight = this.Height;
this.lastLocation = this.Location;
base.OnHandleCreated(e);
}
private bool isSizeChangeAble = true; //是否允许OnSizeChanged执行
protected override void OnSizeChanged(EventArgs e)
{
if (isSizeChangeAble)
{
isSizeChangeAble = false;
if (this.Width < this.lastWidth || this.Height < this.lastHeight)
{
this.Width = Math.Min(this.Width, this.Height);
this.Height = Math.Min(this.Width, this.Height);
this.lastWidth = this.Width;
this.lastHeight = this.Height;
base.OnSizeChanged(e);
isSizeChangeAble = true;
return;
}
if (this.Width > this.lastWidth || this.Height > this.lastHeight)
{
this.Width = Math.Max(this.Width, this.Height);
this.Height = Math.Max(this.Width, this.Height);
this.lastWidth = this.Width;
this.lastHeight = this.Height;
base.OnSizeChanged(e);
isSizeChangeAble = true;
return;
}
this.lastWidth = this.Width;
this.lastHeight = this.Height;
base.OnSizeChanged(e);
isSizeChangeAble = true;
return;
}
}
private int circularWidth = 25;
protected override void OnPaint(PaintEventArgs e)
{
if(Num == 35)
{
try
{
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
using (Pen p = new Pen(Brushes.LightGray, circularWidth))
{
p.LineJoin = LineJoin.Round;
e.Graphics.DrawEllipse(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)));
}
if (2 <= this.value && this.value <= 5)
{
this.mainColor = Color.LimeGreen;
using (Pen p = new Pen(new SolidBrush(this.mainColor), circularWidth))
{
p.LineJoin = LineJoin.Round;
e.Graphics.DrawArc(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)), -93, (float)((float)this.value * Num));
}
}
else if (5 < this.value && this.value < 7)
{
this.mainColor = Color.Orange;
using (Pen p = new Pen(new SolidBrush(this.mainColor), circularWidth))
{
p.LineJoin = LineJoin.Round;
e.Graphics.DrawArc(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)), -93, (float)((float)this.value * Num));
}
}
else if (this.value >= 7)
{
this.mainColor = Color.Red;
using (Pen p = new Pen(new SolidBrush(this.mainColor), circularWidth))
{
p.LineJoin = LineJoin.Round;
e.Graphics.DrawArc(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)), -93, (float)((float)this.value * Num));
}
}
else if (this.value < 2)
{
this.mainColor = Color.Blue;
using (Pen p = new Pen(new SolidBrush(this.mainColor), circularWidth))
{
p.LineJoin = LineJoin.Round;
e.Graphics.DrawArc(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)), -93, (float)((float)this.value * Num));
}
}
SizeF size = e.Graphics.MeasureString(this.value.ToString(), new Font("黑体", 15F, System.Drawing.FontStyle.Bold));
e.Graphics.DrawString(this.value.ToString(), new Font("黑体", 15F, System.Drawing.FontStyle.Bold), new SolidBrush(Color.DimGray), new Point(this.Width / 2 - (int)size.Width / 2 - 1, this.Height / 2 - (int)size.Height / 2 + 2));
e.Graphics.DrawString(Strtxt, new Font("仿宋", 9F, System.Drawing.FontStyle.Bold), new SolidBrush(Color.DimGray), new Point(this.Width / 2 + (int)size.Width / 2 - 8, this.Height / 2 - (int)size.Height + 8));
}
catch { }
}else
{
try
{
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
using (Pen p = new Pen(Brushes.LightGray, circularWidth))
{
p.LineJoin = LineJoin.Round;
e.Graphics.DrawEllipse(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)));
}
this.mainColor = Color.Blue;
using (Pen p = new Pen(new SolidBrush(this.mainColor), circularWidth))
{
p.LineJoin = LineJoin.Round;
e.Graphics.DrawArc(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)), -93, (float)((float)this.value * Num));
}
SizeF size = e.Graphics.MeasureString(this.value.ToString(), new Font("黑体", 15F, System.Drawing.FontStyle.Bold));
e.Graphics.DrawString(this.value.ToString(), new Font("黑体", 15F, System.Drawing.FontStyle.Bold), new SolidBrush(Color.DimGray), new Point(this.Width / 2 - (int)size.Width / 2 - 1, this.Height / 2 - (int)size.Height / 2 + 2));
e.Graphics.DrawString(Strtxt, new Font("仿宋", 9F, System.Drawing.FontStyle.Bold), new SolidBrush(Color.DimGray), new Point(this.Width / 2 + (int)size.Width / 2 - 8, this.Height / 2 - (int)size.Height + 8));
}catch
{
}
}
}
}
}
//CircularProgressBarEx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace Insulin.Client.Circle
{
public class CircularProgressBarEx : Control
{
public CircularProgressBar circularProgressBar = null;
private string completeText = "";
private string doingText = "";
public double Value
{
get { return this.circularProgressBar.Value; }
set
{
this.circularProgressBar.Value = value;
this.Invalidate();
}
}
public Color Color
{
get { return this.circularProgressBar.MainColor; }
set
{
this.circularProgressBar.MainColor = value;
this.Invalidate();
}
}
[LocalizableAttribute(true)]
public string CompleteText
{
get { return completeText; }
set
{
completeText = value;
this.Invalidate();
}
}
[LocalizableAttribute(true)]
public string DoingText
{
get { return doingText; }
set
{
doingText = value;
this.Invalidate();
}
}
public CircularProgressBarEx(string Strtxt,int num)
{
circularProgressBar= new CircularProgressBar(Strtxt,num);
//指定控件的样式和行为
this.SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.Selectable |
ControlStyles.ContainerControl |
ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.Opaque, false);
this.UpdateStyles();
this.BackColor = ColorTranslator.FromHtml("#FF87D68A");
circularProgressBar.Dock = DockStyle.Top;
this.Controls.Add(circularProgressBar);
}
protected override void OnPaint(PaintEventArgs e)
{
SizeF size = e.Graphics.MeasureString(this.doingText, new Font("微软雅黑", 12F));
e.Graphics.DrawString(this.doingText, new Font("微软雅黑", 12F), new SolidBrush(Color.Gray), new Point(this.Width / 2 - (int)size.Width / 2 - 1, this.Height - (int)size.Height - 5));
}
}
}
两个类可直接使用。
在WPF中使用时,在XMAL文件中需要添加DockPanel。
<DockPanel HorizontalAlignment="Left" Height="130" LastChildFill="False" Margin="180,140,0,0" VerticalAlignment="Top" Width="130">
<WindowsFormsHost Background="#0F59E860" x:Name="Temhost" Margin="0,0,0,0.2" Width="130"/>
</DockPanel>
具体实现:
CircularProgressBarEx circularProgressBarExTem = new CircularProgressBarEx("℃", 35);