public Matrix TextMatrix(ProgressBarPath mp) { //Scale the Path to fit the Rectangle var _with3 = mp; RectangleF text_rectf = _with3.Path.GetBounds(); PointF[] target_pts = { new PointF(_with3.Rect.Left, _with3.Rect.Top), new PointF(_with3.Rect.Right, _with3.Rect.Top), new PointF(_with3.Rect.Left, _with3.Rect.Bottom) }; return new Matrix(text_rectf, target_pts); }
public ProgressBarPath GetPath(Rectangle PathRect, bool IsBorder, short ShowDotBorder = 0) { ProgressBarPath pPath = new ProgressBarPath(); pPath.Path = new GraphicsPath(); switch (Shape) { case eShape.Rectangle: if (IsBorder) { pPath.Rect = new Rectangle(PathRect.X, PathRect.Y, PathRect.Width - 1, PathRect.Height - 1); } else { pPath.Rect = new Rectangle(PathRect.X, PathRect.Y, PathRect.Width - ShowDotBorder, PathRect.Height - ShowDotBorder); } if (CornersApply == eCornersApply.Bar) { pPath.Path.AddRectangle(pPath.Rect); } else { pPath.Path = GetRoundedRectPath(pPath.Rect, Corners); } break; case eShape.Ellipse: if (IsBorder) { pPath.Rect = new Rectangle(1, 1, PathRect.Width - 2, PathRect.Height - 2); } else { pPath.Rect = new Rectangle(PathRect.X + ShowDotBorder, PathRect.Y + ShowDotBorder, PathRect.Width - ShowDotBorder * 2, PathRect.Height - ShowDotBorder * 2); } pPath.Path.AddEllipse(pPath.Rect); break; case eShape.TriangleLeft: pPath.Rect = new Rectangle(PathRect.X, PathRect.Y, PathRect.Width, PathRect.Height); Point[] myArray = null; if (IsBorder) { myArray = new Point[] { new Point(pPath.Rect.Left, Convert.ToInt32(pPath.Rect.Height / 2)), new Point(pPath.Rect.Right, 1), new Point(pPath.Rect.Right, pPath.Rect.Bottom - 1) }; } else { myArray = new Point[] { new Point(pPath.Rect.Left + ShowDotBorder, Convert.ToInt32(pPath.Rect.Height / 2)), new Point(Convert.ToInt32(pPath.Rect.Right - ShowDotBorder / 2), 0 + ShowDotBorder), new Point(Convert.ToInt32(pPath.Rect.Right - ShowDotBorder / 2), pPath.Rect.Bottom - ShowDotBorder) }; } pPath.Path.AddPolygon(myArray); break; case eShape.TriangleRight: pPath.Rect = new Rectangle(PathRect.X, PathRect.Y, PathRect.Width, PathRect.Height); myArray = null; if (IsBorder) { myArray = new Point[] { new Point(0, 1), new Point(0, pPath.Rect.Bottom - 1), new Point(pPath.Rect.Right, Convert.ToInt32(pPath.Rect.Height / 2)) }; } else { myArray = new Point[] { new Point(0, 0 + ShowDotBorder), new Point(0, pPath.Rect.Bottom - ShowDotBorder), new Point(pPath.Rect.Right - ShowDotBorder, Convert.ToInt32(pPath.Rect.Height / 2)) }; } pPath.Path.AddPolygon(myArray); break; case eShape.TriangleUp: pPath.Rect = new Rectangle(PathRect.X, PathRect.Y, PathRect.Width, PathRect.Height); myArray = null; if (IsBorder) { myArray = new Point[] { new Point(Convert.ToInt32(pPath.Rect.Width / 2), pPath.Rect.Top), new Point(pPath.Rect.Left + 2, pPath.Rect.Bottom - 1), new Point(pPath.Rect.Right - 2, pPath.Rect.Bottom - 1) }; } else { myArray = new Point[] { new Point(Convert.ToInt32(pPath.Rect.Width / 2), Convert.ToInt32(pPath.Rect.Top + ShowDotBorder / 2)), new Point(pPath.Rect.Left + ShowDotBorder * 2, pPath.Rect.Bottom - ShowDotBorder), new Point(pPath.Rect.Right - ShowDotBorder * 2, pPath.Rect.Bottom - ShowDotBorder) }; } pPath.Path.AddPolygon(myArray); break; case eShape.TriangleDown: pPath.Rect = new Rectangle(PathRect.X, PathRect.Y, PathRect.Width, PathRect.Height); myArray = null; if (IsBorder) { myArray = new Point[] { new Point(Convert.ToInt32(pPath.Rect.Width / 2), pPath.Rect.Bottom), new Point(pPath.Rect.Left + 1, 0), new Point(pPath.Rect.Right - 1, 0) }; } else { myArray = new Point[] { new Point(Convert.ToInt32(pPath.Rect.Width / 2), Convert.ToInt32(pPath.Rect.Bottom - ShowDotBorder / 2)), new Point(pPath.Rect.Left + ShowDotBorder, 0), new Point(pPath.Rect.Right - ShowDotBorder, 0) }; } pPath.Path.AddPolygon(myArray); break; case eShape.Text: if (IsBorder) { pPath.Rect = new Rectangle(1, 1, PathRect.Width - 2, PathRect.Height - 2); } else { pPath.Rect = new Rectangle(PathRect.X + 1, PathRect.Y + 1, Convert.ToInt32(PathRect.Width - 2 - ShowDotBorder / 2), PathRect.Height - 2 - ShowDotBorder); } // Make the StringFormat. StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; // Add the text to the GraphicsPath. pPath.Path.AddString(ShapeText, ShapeTextFont.FontFamily, Convert.ToInt32(FontStyle.Bold), pPath.Rect.Height, new PointF(0, 0), sf); sf.Dispose(); break; } return pPath; }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; ProgressBarPath MyPath = new ProgressBarPath(); MyPath = GetPath(this.DisplayRectangle, true); if ((this.BackgroundImage != null)) { e.Graphics.DrawImage(this.BackgroundImage, this.DisplayRectangle); } //Call the appropriate Paint Method to draw the bar switch (BarType) { case eBarType.Bar: if (Value > 0) { PaintBar(e); } break; case eBarType.CylonBar: PaintCylonBar(e); break; case eBarType.CylonGlider: PaintCylonGlider(e); break; } //Create the Border Graphicspath and Draw it if (Border) { Pen MyPen = new Pen(BorderColor, BorderWidth); MyPen.Alignment = PenAlignment.Inset; var _with1 = MyPath; if (Shape == eShape.Text) { if (ShapeTextRotate != eRotateText.None) { Matrix mtrx = new Matrix(); mtrx.Rotate(GetRotateAngle(ShapeTextRotate)); _with1.Path.Transform(mtrx); } e.Graphics.Transform = TextMatrix(MyPath); } e.Graphics.DrawPath(MyPen, _with1.Path); e.Graphics.ResetTransform(); MyPen.Dispose(); } //Make a Region from the Graphicspath to clip the shape this.Region = null; if (Border) if (BorderWidth == 1) MyPath = GetPath(this.DisplayRectangle, false); Region mRegion = null; if (Shape == eShape.Text) { if (ShapeTextRotate != eRotateText.None) { Matrix mtrx = new Matrix(); mtrx.Rotate(GetRotateAngle(ShapeTextRotate)); MyPath.Path.Transform(mtrx); } mRegion = new Region(MyPath.Path); mRegion.Transform(TextMatrix(MyPath)); } else { mRegion = new Region(MyPath.Path); } this.Region = mRegion; mRegion.Dispose(); //Add the Text if (this.TextShow != eTextShow.None & this.TextPlacement == eTextPlacement.OverBar) PutText(e, this.DisplayRectangle); }