/// <summary> /// 绘制进度条 /// </summary> /// <param name="g">画板</param> /// <param name="rect">需要绘制的矩形区域</param> /// <param name="backgroundColor">进度条的背景颜色</param> /// <param name="borderColor">边框颜色</param> /// <param name="innerBorderColor">内边框颜色</param> /// <param name="roundStyle">绘制圆角的样式</param> /// <param name="roundWidth">圆角宽度</param> /// <param name="alphaPosition">透明度位置</param> /// <param name="drawBorder">是否绘制边框</param> /// <param name="drawGlass">是否绘制进度传输的颜色</param> /// <param name="mode">指定渐变颜色的方向</param> public static void RenderProgressBarBackground(Graphics g, Rectangle rect, Color backgroundColor, Color borderColor, ERoundStyle roundStyle, int roundWidth, float alphaPosition, bool drawBorder, bool drawGlass, LinearGradientMode mode) { if (drawBorder) { rect.Width--; rect.Height--; } if (rect.Width <= 0 || rect.Height <= 0) { return; } using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Transparent, Color.Transparent, mode)) { Color[] colors = new Color[4]; colors[0] = ColorHelper.GetColor(backgroundColor, 0, 35, 24, 9); colors[1] = ColorHelper.GetColor(backgroundColor, 0, 13, 8, 3); colors[2] = backgroundColor; colors[3] = ColorHelper.GetColor(backgroundColor, 0, 68, 69, 54); ColorBlend blend = new ColorBlend(); blend.Positions = new float[] { 0.0f, alphaPosition, alphaPosition + 0.05f, 1.0f }; blend.Colors = colors; brush.InterpolationColors = blend; if (roundStyle != ERoundStyle.None) { using (GraphicsPath path = GraphicsPathHelper.CreatePath(rect, roundWidth, roundStyle, false)) { g.FillPath(brush, path); } if (drawBorder) { using (GraphicsPath path = GraphicsPathHelper.CreatePath(rect, roundWidth, roundStyle, false)) { using (Pen pen = new Pen(borderColor)) { g.DrawPath(pen, path); } } } } else { g.FillRectangle(brush, rect); if (backgroundColor.A > 80) { Rectangle rectTop = rect; if (mode == LinearGradientMode.Vertical) { rectTop.Height = (int)(rectTop.Height * alphaPosition); } else { rectTop.Width = (int)(rect.Width * alphaPosition); } using (SolidBrush brushAlpha = new SolidBrush(Color.FromArgb(80, 255, 255, 255))) { g.FillRectangle(brushAlpha, rectTop); } } if (drawGlass) { RectangleF glassRect = rect; if (mode == LinearGradientMode.Vertical) { glassRect.Y = rect.Y + rect.Height * alphaPosition; glassRect.Height = (rect.Height - rect.Height * alphaPosition) * 2; } else { glassRect.X = rect.X + rect.Width * alphaPosition; glassRect.Width = (rect.Width - rect.Width * alphaPosition) * 2; } DrawGlass(g, glassRect, 200, 0); } if (drawBorder) { using (Pen pen = new Pen(borderColor)) { g.DrawRectangle(pen, rect); } } } } }
public static GraphicsPath CreatePath(Rectangle rect, int radius, ERoundStyle style, bool correction) { GraphicsPath path = new GraphicsPath(); int num = correction ? 1 : 0; switch (style) { case ERoundStyle.None: path.AddRectangle(rect); break; case ERoundStyle.TopLeft: path.AddArc(rect.X, rect.Y, radius, radius, 180f, 90f); path.AddLine(rect.Right - num, rect.Y, rect.Right - num, rect.Bottom - num); path.AddLine(rect.Right - num, rect.Bottom - num, rect.X, rect.Bottom - num); break; case ERoundStyle.TopRight: path.AddArc((rect.Right - radius) - num, rect.Y, radius, radius, 270f, 90f); path.AddLine(rect.Right - num, rect.Bottom - num, rect.X, rect.Bottom - num); path.AddLine(rect.X, rect.Bottom - num, rect.X, rect.Y); break; case ERoundStyle.Top: path.AddArc(rect.X, rect.Y, radius, radius, 180f, 90f); path.AddArc((rect.Right - radius) - num, rect.Y, radius, radius, 270f, 90f); path.AddLine(rect.Right - num, rect.Bottom - num, rect.X, rect.Bottom - num); break; case ERoundStyle.BottomLeft: path.AddArc(rect.X, (rect.Bottom - radius) - num, radius, radius, 90f, 90f); path.AddLine(rect.X, rect.Y, rect.Right - num, rect.Y); path.AddLine(rect.Right - num, rect.Y, rect.Right - num, rect.Bottom - num); break; case ERoundStyle.Left: path.AddArc(rect.X, rect.Y, radius, radius, 180f, 90f); path.AddLine(rect.Right - num, rect.Y, rect.Right - num, rect.Bottom - num); path.AddArc(rect.X, (rect.Bottom - radius) - num, radius, radius, 90f, 90f); break; case ERoundStyle.BottomRight: path.AddArc((rect.Right - radius) - num, (rect.Bottom - radius) - num, radius, radius, 0f, 90f); path.AddLine(rect.X, rect.Bottom - num, rect.X, rect.Y); path.AddLine(rect.X, rect.Y, rect.Right - num, rect.Y); break; case ERoundStyle.Right: path.AddArc((rect.Right - radius) - num, rect.Y, radius, radius, 270f, 90f); path.AddArc((rect.Right - radius) - num, (rect.Bottom - radius) - num, radius, radius, 0f, 90f); path.AddLine(rect.X, rect.Bottom - num, rect.X, rect.Y); break; case ERoundStyle.Bottom: path.AddArc((rect.Right - radius) - num, (rect.Bottom - radius) - num, radius, radius, 0f, 90f); path.AddArc(rect.X, (rect.Bottom - radius) - num, radius, radius, 90f, 90f); path.AddLine(rect.X, rect.Y, rect.Right - num, rect.Y); break; case ERoundStyle.All: path.AddArc(rect.X, rect.Y, radius, radius, 180f, 90f); path.AddArc((rect.Right - radius) - num, rect.Y, radius, radius, 270f, 90f); path.AddArc((rect.Right - radius) - num, (rect.Bottom - radius) - num, radius, radius, 0f, 90f); path.AddArc(rect.X, (rect.Bottom - radius) - num, radius, radius, 90f, 90f); break; } path.CloseFigure(); return(path); }