Skip to content

Instantly share code, notes, and snippets.

@CamelCaseName
Created July 24, 2023 18:12
Show Gist options
  • Save CamelCaseName/fe893e8c80d3f008012cf64cc75356dc to your computer and use it in GitHub Desktop.
Save CamelCaseName/fe893e8c80d3f008012cf64cc75356dc to your computer and use it in GitHub Desktop.
C# Winforms Progressbar without Animation and custom color
using System.Drawing;
using System.Windows.Forms;
namespace CustomComponents
{
/// <summary>
/// A extended WinForms ProgressBar which has a different colour and removes the animation.
/// </summary>
public class NoAnimationBar : ProgressBar
{
/// <summary>
/// The constructor for said Progressbar, mimics the WinForm constructor.
/// </summary>
public NoAnimationBar()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
}
/// <summary>
/// A override for the OnPaint method to draw the bar without an animation and a custom colour.
/// </summary>
/// <param name="e">The PaintEventArgs to manipulate.</param>
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(ForeColor), new Rectangle(0, 0, (int)(Width * (float)Value / Maximum), Height));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment