From 853bde8e6a92d03438975c554ef8004e6e2b37ce Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 26 Sep 2022 13:58:54 +0200 Subject: [PATCH] Defined an own progress bar --- .../Components/NXProgressBar.Designer.cs | 48 ++++++++++++ .../UI WinForms/Components/NXProgressBar.cs | 73 +++++++++++++++++++ .../UI WinForms/Components/NXProgressBar.resx | 60 +++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 I18N Commander/UI WinForms/Components/NXProgressBar.Designer.cs create mode 100644 I18N Commander/UI WinForms/Components/NXProgressBar.cs create mode 100644 I18N Commander/UI WinForms/Components/NXProgressBar.resx diff --git a/I18N Commander/UI WinForms/Components/NXProgressBar.Designer.cs b/I18N Commander/UI WinForms/Components/NXProgressBar.Designer.cs new file mode 100644 index 0000000..bc99865 --- /dev/null +++ b/I18N Commander/UI WinForms/Components/NXProgressBar.Designer.cs @@ -0,0 +1,48 @@ +namespace UI_WinForms.Components +{ + sealed partial class NXProgressBar + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.SuspendLayout(); + // + // NXProgressBar + // + this.AutoScaleDimensions = new System.Drawing.SizeF(120F, 120F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; + this.BackColor = System.Drawing.Color.LightSkyBlue; + this.Font = new System.Drawing.Font("Segoe UI", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Margin = new System.Windows.Forms.Padding(0); + this.Name = "NXProgressBar"; + this.Size = new System.Drawing.Size(702, 41); + this.ResumeLayout(false); + + } + + #endregion + } +} diff --git a/I18N Commander/UI WinForms/Components/NXProgressBar.cs b/I18N Commander/UI WinForms/Components/NXProgressBar.cs new file mode 100644 index 0000000..6b3bc7d --- /dev/null +++ b/I18N Commander/UI WinForms/Components/NXProgressBar.cs @@ -0,0 +1,73 @@ +using System.ComponentModel; +using SolidBrush = System.Drawing.SolidBrush; + +namespace UI_WinForms.Components; + +public sealed partial class NXProgressBar : UserControl +{ + private static readonly SolidBrush BACKGROUND_NOT_FILLED = new(SystemColors.Control); + + private readonly SolidBrush backgroundFilled; + private readonly SolidBrush textBrush; + + public NXProgressBar() + { + this.InitializeComponent(); + this.backgroundFilled = new SolidBrush(this.BackColor); + this.textBrush = new SolidBrush(this.ForeColor); + } + + [Category("Settings"), Description("Text to show inside the progress bar.")] + public string InformationText { get; set; } = string.Empty; + + [Category("Settings"), Description("The current percentage [0,100] of the progress bar.")] + public int PercentInt + { + get => (int) (this.PercentFloat * 100); + set => this.PercentFloat = value / 100f; + } + + [Category("Settings"), Description("The current percentage [0,1] of the progress bar.")] + public float PercentFloat { get; set; } = 0; + + [Category("Settings"), Description("When true, appends the percentage to the text.")] + public bool AppendPercentageToText { get; set; } = true; + + protected override void OnPaintBackground(PaintEventArgs e) + { + // Do not paint the background. + } + + // Draw the progress bar by filling the background of the label with color: + protected override void OnPaint(PaintEventArgs e) + { + base.OnPaint(e); + + var width = (int) MathF.Ceiling((this.Width - this.Margin.Horizontal) * this.PercentFloat); + var height = this.Height - this.Margin.Vertical; + + // Erase the background: + e.Graphics.FillRectangle(BACKGROUND_NOT_FILLED, this.Margin.Left, this.Margin.Top, this.Width - this.Margin.Horizontal, height); + + // Draw the filled background: + e.Graphics.FillRectangle(this.backgroundFilled, this.Margin.Left, this.Margin.Top, width, height); + + // Calculate the text x position by using the margin: + var textX = this.Margin.Left + this.Padding.Left; + + // Calculate the text y position by considering the margin: + var textY = (int) MathF.Ceiling((this.Height - this.Font.Height) / 2f) + this.Margin.Top + this.Padding.Top; + + // Draw the text horizontally at the beginning, and vertically centered: + e.Graphics.DrawString(this.GetTextForRendering(), this.Font, this.textBrush, textX, textY); + } + + private string GetTextForRendering() + { + var text = this.InformationText; + if (this.AppendPercentageToText) + text += $" ({this.PercentInt}%)"; + + return text; + } +} \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Components/NXProgressBar.resx b/I18N Commander/UI WinForms/Components/NXProgressBar.resx new file mode 100644 index 0000000..b5ae26c --- /dev/null +++ b/I18N Commander/UI WinForms/Components/NXProgressBar.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file