1

I created a custom ProgressBar by fallowing this example.

In my application, when a particualr event occurs, a page is printed and the print preview is displayed.

When the print preview appears, I add an exception whith my ProgressBar.

Code line :

ProgressBarRenderer.DrawHorizontalBar(g, rect);

Exception :

Visual styles are disabled by the user in the operating system.

I added a test to avoid the exception :

if (!ProgressBarRenderer.IsSupported)
    return;

Now I don't have the exception, but instead the test failed and it always return. So the bar is not painted and never appears again.

What can I do to keep my ProgressBar always painted ?

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
  • "the test failed" seems to be the important detail. This code requires the app to be initialized correctly with a call to Application.EnableVisualStyles(). That always happens in a boilerplate Winforms app, right at the start of Main(). In a test, hmm, probably not. – Hans Passant Sep 19 '17 at 19:05
  • I've try to `Application.EnableVisualStyles()` but nothing changes. But first time the `OnPaint` method is called, `ProgressBarRenderer.IsSupported` is true. And it stay true until the preview appears. After that it is always false, event if I enable visual styles. – A.Pissicat Sep 20 '17 at 07:25

2 Answers2

1

It not a real solution to this problem but I fount a workaround. Instead of using ProgressBarRenderer, I draw the rectangles.

The OnPaint method is now :

protected override void OnPaint(PaintEventArgs e)
{
    Rectangle rect = ClientRectangle;

    using (SolidBrush brush = new SolidBrush(BackColor))
        e.Graphics.FillRectangle(brush, rect);
    e.Graphics.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width - 2, rect.Height - 2);
    rect.Inflate(-3, -3);

    if (Value > 0)
    {
        Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)Value / Maximum) * rect.Width), rect.Height);
        using (SolidBrush brush = new SolidBrush(ForeColor))
            e.Graphics.FillRectangle(brush, clip);
    }

    using (Font f = new Font(sgvDesigner.FontFamily, 12))
    {
        String text = getDisplayText();
        SizeF len = e.Graphics.MeasureString(text, f);
        Point location = new Point(Convert.ToInt32((Width / 2) - len.Width / 2), Convert.ToInt32((Height / 2) - len.Height / 2));
        e.Graphics.DrawString(text, f, TextColor, location);
    }

    return;
}

The progressBar looks like this :

enter image description here

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
0

This is not something that can be fixed programmatically. You need to configure you Operating System to allow this functionality. You can do this by navigating to Performance Options > Visual Effects and enabling the "Use visual styles on windows and buttons" feature.

If you are having difficulty finding Performance options you might trying typing the following into Start: "Adjust the appearance and performance of Windows"

tonythewest
  • 456
  • 3
  • 13