123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- 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();
- }
- }
- }
- }
|