0

Is is possible to disable Application.EnableVisualStyles(); programmatically? I wanted to turn off the visual styles for a certain part of my application where I could have a colored progress bar. I know you can use System.Drawing to draw it on, but this is much more simpler if I could turn it off temporarily. Is this possible or am I going to have to draw it?

leppie
  • 115,091
  • 17
  • 196
  • 297
Alex Diamond
  • 586
  • 3
  • 27

2 Answers2

0

Credits go to GreatJobBob for linking me the MSDN page, the following achieved what I was looking for.

   using System.Windows.Forms.VisualStyles;

    Application.VisualStyleState = VisualStyleState.NonClientAreaEnabled;

This let me change the color of my progress bar without changing the rest of my controls and form.

Alex Diamond
  • 586
  • 3
  • 27
0

Create your own progress bar class. Disabling Application.EnableVisualStyles will cause problems in other UIs such as the MessageBox. Here's a basic class to get you started, just change the forecolor to what you want it to be.

using System;
using System.Drawing;
using System.Windows.Forms;

class MyProgressBar : Control 
{
public MyProgressBar() 
{
    this.SetStyle(ControlStyles.ResizeRedraw, true);
    this.SetStyle(ControlStyles.Selectable, false);
    Maximum = 100;
    this.ForeColor = Color.Red; //This is where you choose your color
    this.BackColor = Color.White;
}
public decimal Minimum { get; set; }
public decimal Maximum { get; set; }

private decimal mValue;
public decimal Value 
{
    get { return mValue; }
    set { mValue = value; Invalidate(); }
}

protected override void OnPaint(PaintEventArgs e) 
{
    var rc = new RectangleF(0, 0, (float)(this.Width * (Value - Minimum) / Maximum), this.Height);
    using (var br = new SolidBrush(this.ForeColor)) 
    {
        e.Graphics.FillRectangle(br, rc);
    }
    base.OnPaint(e);
}
}