using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace QVCopier.Components.ProgressBarLabel { public class ProgressBarLabelControl : ProgressBar { private readonly StringFormat _stringFormat = new() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; private ContentAlignment _textAlign = ContentAlignment.MiddleCenter; [DefaultValue(ContentAlignment.MiddleCenter), Browsable(true)] public virtual ContentAlignment TextAlign { get => _textAlign; set { _textAlign = value; switch (value) { case ContentAlignment.TopLeft: _stringFormat.LineAlignment = StringAlignment.Near; _stringFormat.Alignment = StringAlignment.Near; break; case ContentAlignment.TopCenter: _stringFormat.LineAlignment = StringAlignment.Near; _stringFormat.Alignment = StringAlignment.Center; break; case ContentAlignment.TopRight: _stringFormat.LineAlignment = StringAlignment.Near; _stringFormat.Alignment = StringAlignment.Far; break; case ContentAlignment.MiddleLeft: _stringFormat.LineAlignment = StringAlignment.Center; _stringFormat.Alignment = StringAlignment.Near; break; case ContentAlignment.MiddleCenter: _stringFormat.LineAlignment = StringAlignment.Center; _stringFormat.Alignment = StringAlignment.Center; break; case ContentAlignment.MiddleRight: _stringFormat.LineAlignment = StringAlignment.Center; _stringFormat.Alignment = StringAlignment.Far; break; case ContentAlignment.BottomLeft: _stringFormat.LineAlignment = StringAlignment.Far; _stringFormat.Alignment = StringAlignment.Near; break; case ContentAlignment.BottomCenter: _stringFormat.LineAlignment = StringAlignment.Far; _stringFormat.Alignment = StringAlignment.Center; break; case ContentAlignment.BottomRight: _stringFormat.LineAlignment = StringAlignment.Far; _stringFormat.Alignment = StringAlignment.Far; break; default: throw new ArgumentOutOfRangeException(nameof(value), value, null); } Invalidate();//redraw component after change value from VS Properties section } } [Browsable(true)] public override string Text { get => base.Text; set => base.Text = value; } public ProgressBarLabelControl() { Value = Minimum; FixComponentBlinking(); } private void FixComponentBlinking() { SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true); } protected override void OnPaint(PaintEventArgs e) { var g = e.Graphics; using var brush = new SolidBrush(ForeColor); var rect = ClientRectangle; ProgressBarRenderer.DrawHorizontalBar(g, rect); rect.Inflate(-3, -3); var progressBarRect = new Rectangle(rect.X, rect.Y, (int)Math.Round((float)Value / Maximum * rect.Width), rect.Height); if (Value > 0) { g.FillRectangle(brush, progressBarRect); } if (false == string.IsNullOrWhiteSpace(Text)) { void DrawText(Color c) { using var br = new SolidBrush(c); g.DrawString(Text, Font, br, rect, _stringFormat); } g.SetClip(rect); DrawText(ForeColor); g.ResetClip(); g.SetClip(progressBarRect); DrawText(BackColor); g.ResetClip(); } } } }