DevExpress Winforms使用技巧教程:ProgressBarControl - 如何使用漸變繪制進度條
下載DevExpress v19.2完整版 DevExpress v19.2漢化資源獲取
DevExpress Winforms Controls 內(nèi)置140多個UI控件和庫,完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序。想要體驗?點擊下載>>
問題:
開發(fā)人員想要使用帶有3中顏色的漸變來顯示進度條,即開始、中間和末尾有3中不同的顏色,但目前只能使用property.startcolor和property.endcolor對兩種顏色實現(xiàn),沒有可用的middlecolor屬性。是否有辦法或者可以使用那些控件來實現(xiàn)如下圖所示的效果:
解決辦法:
如果要繪制一些文本,則必須在進度行下為其留出空間。 由于進度線始終占據(jù)整個空間,因此您需要更新所有必需矩形的高度。相關(guān)代碼示例如下:
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控件不支持此功能,為了實現(xiàn)您的目標(biāo),建議使用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技術(shù)交流群:540330292 歡迎一起進群討論
掃描關(guān)注DevExpress中文網(wǎng)微信公眾號,及時獲取最新動態(tài)及最新資訊