AABBbaby

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Winforms平台界面开发技巧分享:如何使用渐变绘制进度条

下载DevExpress v19.2完整版 

DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅、美观且易于使用的应用程序。

问题:

开发人员想要使用带有3中颜色的渐变来显示进度条,即开始、中间和末尾有3中不同的颜色,但目前只能使用property.startcolor和property.endcolor对两种颜色实现,没有可用的middlecolor属性。是否有办法或者可以使用那些控件来实现如下图所示的效果:

DevExpress Winforms使用技巧教程
解决办法:

如果要绘制一些文本,则必须在进度行下为其留出空间。 由于进度线始终占据整个空间,因此您需要更新所有必需矩形的高度。相关代码示例如下:

 

private void progressBarControl1_Paint(object sender, PaintEventArgs e) {
ProgressBarViewInfo vi = (ProgressBarViewInfo)this.progressBarControl1.GetViewInfo();
Padding bt = CalcBorderThickness(vi);
int progressHeight = 22;
int textIndent = 2;
vi.ProgressInfo.Bounds = UpdateHeight(vi.ProgressInfo.Bounds, progressHeight);
vi.ProgressInfo.EmptyBounds = UpdateHeight(vi.ProgressInfo.EmptyBounds, progressHeight);
vi.ProgressInfo.EmptyBounds2 = UpdateHeight(vi.ProgressInfo.EmptyBounds2, progressHeight);
vi.ProgressInfo.ProgressBounds = UpdateHeight(vi.ProgressInfo.ProgressBounds, progressHeight);
Rectangle all = vi.ProgressInfo.Bounds;
Rectangle filled = vi.ProgressInfo.ProgressBounds;
Rectangle part1 = new Rectangle(filled.X, filled.Y, filled.Width / 2 + 1, filled.Height);
Rectangle part2 = new Rectangle(part1.Right, filled.Y, filled.Right - part1.Right, filled.Height);
using (GraphicsCache cache = new GraphicsCache(e.Graphics)) {
cache.Graphics.Clear(this.progressBarControl1.Parent.BackColor);
Rectangle borderBounds = vi.ProgressInfo.Bounds;
borderBounds.Inflate(bt.Left, bt.Top);
vi.ProgressInfo.Cache = cache;
vi.BorderPainter.DrawObject(new ObjectInfoArgs() { Bounds = borderBounds, Cache = cache });
cache.FillRectangle(cache.GetGradientBrush(part1, Color.Red, Color.Yellow, LinearGradientMode.Horizontal), part1);
cache.FillRectangle(cache.GetGradientBrush(part2, Color.Yellow, Color.Green, LinearGradientMode.Horizontal), part2);
Size textSize = vi.ProgressAppearance.CalcTextSize(cache, vi.DisplayText, -1).ToSize();
int linePos = part2.Right >= all.Right - 1? all.Right -1: part2.Right;
cache.DrawLine(cache.GetPen(Color.Black), new Point(linePos, part2.Top), new Point(linePos, part2.Bottom + textSize.Height + textIndent));
Rectangle textRect = new Rectangle(new Point(linePos + textIndent, part2.Bottom + textIndent), textSize);
if (textRect.Right > all.Right)
textRect.X -= textRect.Width + textIndent * 2;
vi.ProgressAppearance.DrawString(cache, vi.DisplayText, textRect);
}
}
private Padding CalcBorderThickness(ProgressBarViewInfo vi) {
SkinProgressBarObjectPainter p = new SkinProgressBarObjectPainter(vi.LookAndFeel);
ProgressBarObjectInfoArgs e = new ProgressBarObjectInfoArgs(vi.Appearance);
Rectangle client = new Rectangle(0, 0, 100, 100);
Rectangle r = vi.BorderPainter.CalcBoundsByClientRectangle(vi.ProgressInfo, client);
Padding res = new Padding(client.X - r.X, client.Y - r. Y, r.Right - client.Right, r.Bottom - client.Bottom);
return res;
}
private Rectangle UpdateHeight(Rectangle bounds, int height) {
bounds.Height = height;
return bounds;
}

 

ProgressBarControl控件不支持此功能,为了实现您的目标,建议使用ProgressBarControl.Paint事件并编写代码来绘制渐变进度条,示例如下:

 

private void progressBarControl1_Paint(object sender, PaintEventArgs e) {
ProgressBarViewInfo vi = (ProgressBarViewInfo)this.progressBarControl1.GetViewInfo();
Padding bt = CalcBorderThickness(vi);
int progressHeight = 22;
int textIndent = 2;
vi.ProgressInfo.Bounds = UpdateHeight(vi.ProgressInfo.Bounds, progressHeight);
vi.ProgressInfo.EmptyBounds = UpdateHeight(vi.ProgressInfo.EmptyBounds, progressHeight);
vi.ProgressInfo.EmptyBounds2 = UpdateHeight(vi.ProgressInfo.EmptyBounds2, progressHeight);
vi.ProgressInfo.ProgressBounds = UpdateHeight(vi.ProgressInfo.ProgressBounds, progressHeight);
Rectangle all = vi.ProgressInfo.Bounds;
Rectangle filled = vi.ProgressInfo.ProgressBounds;
Rectangle part1 = new Rectangle(filled.X, filled.Y, filled.Width / 2 + 1, filled.Height);
Rectangle part2 = new Rectangle(part1.Right, filled.Y, filled.Right - part1.Right, filled.Height);
using (GraphicsCache cache = new GraphicsCache(e.Graphics)) {
cache.Graphics.Clear(this.progressBarControl1.Parent.BackColor);
Rectangle borderBounds = vi.ProgressInfo.Bounds;
borderBounds.Inflate(bt.Left, bt.Top);
vi.ProgressInfo.Cache = cache;
vi.BorderPainter.DrawObject(new ObjectInfoArgs() { Bounds = borderBounds, Cache = cache });
cache.FillRectangle(cache.GetGradientBrush(part1, Color.Red, Color.Yellow, LinearGradientMode.Horizontal), part1);
cache.FillRectangle(cache.GetGradientBrush(part2, Color.Yellow, Color.Green, LinearGradientMode.Horizontal), part2);
Size textSize = vi.ProgressAppearance.CalcTextSize(cache, vi.DisplayText, -1).ToSize();
int linePos = part2.Right >= all.Right - 1? all.Right -1: part2.Right;
cache.DrawLine(cache.GetPen(Color.Black), new Point(linePos, part2.Top), new Point(linePos, part2.Bottom + textSize.Height + textIndent));
Rectangle textRect = new Rectangle(new Point(linePos + textIndent, part2.Bottom + textIndent), textSize);
if (textRect.Right > all.Right)
textRect.X -= textRect.Width + textIndent * 2;
vi.ProgressAppearance.DrawString(cache, vi.DisplayText, textRect);
}
}
private Padding CalcBorderThickness(ProgressBarViewInfo vi) {
SkinProgressBarObjectPainter p = new SkinProgressBarObjectPainter(vi.LookAndFeel);
ProgressBarObjectInfoArgs e = new ProgressBarObjectInfoArgs(vi.Appearance);
Rectangle client = new Rectangle(0, 0, 100, 100);
Rectangle r = vi.BorderPainter.CalcBoundsByClientRectangle(vi.ProgressInfo, client);
Padding res = new Padding(client.X - r.X, client.Y - r. Y, r.Right - client.Right, r.Bottom - client.Bottom);
return res;
}
private Rectangle UpdateHeight(Rectangle bounds, int height) {
bounds.Height = height;
return bounds;
}

 


DevExpress Dashboard控件实操公开课4月即将开启,专家名师在线直播,免费听课名额先到先得~

DevExpress技术交流群:540330292      欢迎一起进群讨论

扫描关注DevExpress中文网微信公众号,及时获取最新动态及最新资讯

DevExpress中文网微信

posted on   AABBbaby  阅读(202)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示