How to change the color of the progress bar?

Asked

Viewed 1,109 times

4

I’d like to do two things on my progress bar.

  • Change color from green to red.
  • Remove the blocks and create a color.

I’m using Visual Studio Community 2019 on Windows 10.

1 answer

6


How To Do

First, include this if you don’t have:

using System.Runtime.InteropServices;

Second, you can create a new class or place your code in a static class existing nongeneric:

public static class ModificarCorProgressBar
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr w, IntPtr l);
    public static void SetState(this ProgressBar pBar, int state)
    {
        SendMessage(pBar.Handle, 1040, (IntPtr)state, IntPtr.Zero);
    }
}

How an extension method was created SetState we can call you directly this way:

progressBar1.SetState(2)

Remembering that the second parameter in SetState:

1 = normal (green)
2 = error (red)
3 = warning (yellow)

Results of the Sample

Using Windows 8 or higher with this method below, you can get something like this:

teste no Win8/10

Already in Windows Vista and Windows 7 the result will be a little different (honestly even better)

teste no WinVista/7

This is an adaptation of reply given by user1032613 in the OS .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.