简体   繁体   中英

ProgressBar disappears when displaying print preview

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 ?

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"

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 :

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM