C# 如何沿圆形路径固定进度条渐变?
现在,我正试图创建一个沿圆形路径渐变的进度条,但渐变不是实心的,颜色之间的间距很小,如。。。那么问题出在哪里?如何解决C# 如何沿圆形路径固定进度条渐变?,c#,winforms,progress-bar,gradient,C#,Winforms,Progress Bar,Gradient,现在,我正试图创建一个沿圆形路径渐变的进度条,但渐变不是实心的,颜色之间的间距很小,如。。。那么问题出在哪里?如何解决 public ExtProgressBar() { this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.U
public ExtProgressBar()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint,
true);
_gradientColorList.Add(Color.Violet);
_gradientColorList.Add(Color.Indigo);
_gradientColorList.Add(Color.Blue);
_gradientColorList.Add(Color.DodgerBlue);
CreateGradientBrushList(ref _gradientColorList);
}
protected override void OnPaint(PaintEventArgs e)
{
Image bmp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
SolidBrush brushBase = new SolidBrush(this.BaseColor); //custom property
SolidBrush brushBack = new SolidBrush(this.BackColor);
SolidBrush brushFore = new SolidBrush(this.ForeColor);
//base ellipse
g.FillEllipse(brushBase, GetRectangle(0));
//fallowing code block creates circular gradient
int i = 0;
float endAngle = 360f * this.Value / 100 - 90f;
float sweepAngle = 360f / _gradientBrushList.Count;
for (float startAngle = -90f; startAngle < endAngle; startAngle += sweepAngle)
{
if (i < _gradientBrushList.Count)
{
g.FillPie(_gradientBrushList[i++], GetRectangle(0), startAngle, sweepAngle);
}
}
//back ellipse
g.FillEllipse(brushBack, GetRectangle(this.Thickness));
//draw value string in center of p-bar
string value = this.Value.ToString();
float fontSize = (float)(Math.Min(this.Width - this.Thickness * 2, this.Height - this.Thickness * 2) / 2);
Font font = new Font("Calibri", (fontSize > 0 ? fontSize : 1), FontStyle.Bold, GraphicsUnit.Pixel);
SizeF strLen = g.MeasureString(value, font);
Point strLoc = new Point((int)((this.Width / 2) - (strLen.Width / 2)), (int)((this.Height / 2) - (strLen.Height / 2)));
g.DrawString(value, font, brushFore, strLoc);
e.Graphics.DrawImage(bmp, Point.Empty);
}
private Rectangle GetRectangle(int offset)
{
return new Rectangle(offset, offset, this.Width - offset * 2, this.Height - offset * 2);
}
private List<SolidBrush> _gradientBrushList = new List<SolidBrush>();
private void CreateGradientBrushList(ref List<Color> _gradientColorList)
{
List<Color> gradientColorList = new List<Color>();
foreach (Color color in _gradientColorList)
gradientColorList.Add(color);
for (int subdivideCount = 0; subdivideCount <= 3; subdivideCount++) //Smoothness
{
int i = 0;
int gradientColorListCount = gradientColorList.Count - 1;
while (i < gradientColorListCount)
{
gradientColorList.Insert(i + 1, GetMiddleColor(gradientColorList[i], gradientColorList[i + 1]));
i += 2;
gradientColorListCount++;
}
}
foreach (Color color in gradientColorList)
_gradientBrushList.Add(new SolidBrush(color));
}
private List<Color> _gradientColorList = new List<Color>();
public List<Color> GradientColorList
{
get
{
return _gradientColorList;
}
set
{
try
{
if (value == null)
throw new ArgumentNullException();
if (value == null || value.Count < 2)
throw new Exception("Gradient color list count is less than 2.");
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, "Exception caught", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
_gradientColorList = value;
CreateGradientBrushList(ref _gradientColorList);
}
}
}
public ExtProgressBar()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer|
ControlStyles.AllPaintingWimPaint|
ControlStyles.UserPaint,
正确的);
_gradientColorList.Add(Color.Violet);
_gradientColorList.Add(Color.Indigo);
_gradientColorList.Add(Color.Blue);
_gradientColorList.Add(Color.DodgerBlue);
CreateGradientBrushList(参考gradientColorList);
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
图像bmp=新位图(this.Width,this.Height);
Graphics g=Graphics.FromImage(bmp);
g、 SmoothingMode=SmoothingMode.AntiAlias;
g、 PixelOffsetMode=PixelOffsetMode.HighQuality;
SolidBrush brushBase=新的SolidBrush(this.BaseColor);//自定义属性
SolidBrush brushBack=新的SolidBrush(此.BackColor);
SolidBrush brushFore=新的SolidBrush(此.ForeColor);
//基椭圆
g、 FillEllipse(brushBase,GetRectangle(0));
//衰减代码块创建圆形渐变
int i=0;
浮动端角=360f*此值/100-90f;
浮动扫描角度=360f/_gradientBrushList.Count;
用于(浮动星形缠结=-90f;星形缠结<端角;星形缠结+=扫掠角)
{
如果(i<\u gradientBrushList.Count)
{
g、 FillPie(_gradientBrushList[i++],GetRectangle(0),startAngle,sweepAngle);
}
}
//后椭圆
g、 FillEllipse(刷回,GetRectangle(this.this.Thickness));
//在p形条的中心绘制值字符串
字符串值=this.value.ToString();
float fontSize=(float)(数学最小值(this.Width-this.this.Thickness*2,this.Height-this.Thickness*2)/2);
Font Font=新字体(“Calibri”,(fontSize>0?fontSize:1),FontStyle.Bold,GraphicsUnit.Pixel);
SizeF strLen=g.MeasureString(值,字体);
点strLoc=新点((int)((this.Width/2)-(strLen.Width/2)),(int)((this.Height/2)-(strLen.Height/2));
g、 抽绳(数值、字体、刷头、strLoc);
e、 Graphics.DrawImage(bmp,Point.Empty);
}
私有矩形GetRectangle(整数偏移)
{
返回新矩形(偏移,偏移,this.Width-offset*2,this.Height-offset*2);
}
私有列表_gradientBrushList=新列表();
私有void CreateGradientBrushList(参考列表_gradientColorList)
{
List gradientColorList=新列表();
foreach(渐变颜色列表中的颜色)
gradientColorList.Add(颜色);
对于(int subsectdecount=0;subsectdecount也可与FillPie
成对使用DrawPie
。
这是因为FillPie
方法只填充饼图部分的内部,而不填充边框。
守则:
protected override void OnPaint(PaintEventArgs e)
{
Image bmp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
SolidBrush brushBase = new SolidBrush(this.BaseColor); //custom property
SolidBrush brushBack = new SolidBrush(this.BackColor);
SolidBrush brushFore = new SolidBrush(this.ForeColor);
//base ellipse
g.FillEllipse(brushBase, GetRectangle(0));
//fallowing code block creates circular gradient
int i = 0;
float endAngle = 360f * this.Value / 100 - 90f;
float sweepAngle = 360f / _gradientBrushList.Count;
for (float startAngle = -90f; startAngle < endAngle; startAngle += sweepAngle)
{
if (i < _gradientBrushList.Count)
{
g.DrawPie(_gradientPenList[i++], GetRectangle(0), startAngle, sweepAngle);
g.FillPie(_gradientBrushList[i++], GetRectangle(0), startAngle, sweepAngle);
}
}
//back ellipse
g.FillEllipse(brushBack, GetRectangle(this.Thickness));
//draw value string in center of p-bar
string value = this.Value.ToString();
float fontSize = (float)(Math.Min(this.Width - this.Thickness * 2, this.Height - this.Thickness * 2) / 2);
Font font = new Font("Calibri", (fontSize > 0 ? fontSize : 1), FontStyle.Bold, GraphicsUnit.Pixel);
SizeF strLen = g.MeasureString(value, font);
Point strLoc = new Point((int)((this.Width / 2) - (strLen.Width / 2)), (int)((this.Height / 2) - (strLen.Height / 2)));
g.DrawString(value, font, brushFore, strLoc);
e.Graphics.DrawImage(bmp, Point.Empty);
}
private Rectangle GetRectangle(int offset)
{
return new Rectangle(offset, offset, this.Width - offset * 2, this.Height - offset * 2);
}
private List<SolidBrush> _gradientBrushList = new List<SolidBrush>();
private List<Pen> _gradientPenList = new List<Pen>();
private void CreateGradientBrushList(ref List<Color> _gradientColorList)
{
List<Color> gradientColorList = new List<Color>();
foreach (Color color in _gradientColorList)
gradientColorList.Add(color);
for (int subdivideCount = 0; subdivideCount <= 3; subdivideCount++) //Smoothness
{
int i = 0;
int gradientColorListCount = gradientColorList.Count - 1;
while (i < gradientColorListCount)
{
gradientColorList.Insert(i + 1, GetMiddleColor(gradientColorList[i], gradientColorList[i + 1]));
i += 2;
gradientColorListCount++;
}
}
foreach (Color color in gradientColorList)
{
_gradientBrushList.Add(new SolidBrush(color));
_gradientPenList.Add(new Pen(color));
}
}
protected override void OnPaint(PaintEventArgs e)
{
图像bmp=新位图(this.Width,this.Height);
Graphics g=Graphics.FromImage(bmp);
g、 SmoothingMode=SmoothingMode.AntiAlias;
g、 PixelOffsetMode=PixelOffsetMode.HighQuality;
SolidBrush brushBase=新的SolidBrush(this.BaseColor);//自定义属性
SolidBrush brushBack=新的SolidBrush(此.BackColor);
SolidBrush brushFore=新的SolidBrush(此.ForeColor);
//基椭圆
g、 FillEllipse(brushBase,GetRectangle(0));
//衰减代码块创建圆形渐变
int i=0;
浮动端角=360f*此值/100-90f;
浮动扫描角度=360f/_gradientBrushList.Count;
用于(浮动星形缠结=-90f;星形缠结<端角;星形缠结+=扫掠角)
{
如果(i<\u gradientBrushList.Count)
{
g、 DrawPie(_gradientPenList[i++],GetRectangle(0),startAngle,sweepAngle);
g、 FillPie(_gradientBrushList[i++],GetRectangle(0),startAngle,sweepAngle);
}
}
//后椭圆
g、 FillEllipse(刷回,GetRectangle(this.this.Thickness));
//在p形条的中心绘制值字符串
字符串值=this.value.ToString();
float fontSize=(float)(数学最小值(this.Width-this.this.Thickness*2,this.Height-this.Thickness*2)/2);
Font Font=新字体(“Calibri”,(fontSize>0?fontSize:1),FontStyle.Bold,GraphicsUnit.Pixel);
SizeF strLen=g.MeasureString(值,字体);
点strLoc=新点((int)((this.Width/2)-(strLen.Width/2)),(int)((this.Height/2)-(strLen.Height/2));
g、 抽绳(数值、字体、刷头、strLoc);
e、 Graphics.DrawImage(bmp,Point.Empty);
}
私有矩形GetRectangle(整数偏移)
{
返回新矩形(偏移,偏移,this.Width-offset*2,this.Height-offset*2);
}
私有列表_gradientBrushList=新列表();
私有列表_gradientPenList=新列表();
私有void CreateGradientBrushList(参考列表_gradientColorList)
{
List gradientColorList=新列表();
foreach(渐变颜色列表中的颜色)
gradientColorList.Add(颜色);
对于(int subsectdecount=0;subsectdecount)