Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Coloring ProgressBar

Coloring ProgressBar

Scheduled Pinned Locked Moved C#
question
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eyalbi007
    wrote on last edited by
    #1

    Hi All, I'm trying to chenge the color of my ProgressBar without building a new component from scratch. I tried inheriting from System.Windows.Forms.ProgressBar and use the code below (which I found in several forums), but it's not working (if relevant, I'm working on vista 64). Any ideas?

    class MyProgressBar : ProgressBar
    {
    [DllImport("User32.Dll")]
    public static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);

    public const int PBM_SETBKCOLOR = 0x2001;
    public const int PBM_SETBARCOLOR = 0x409;

    public void SetProgressBackColor(Color c)
    {/// set the back color of the bar
    int a = Convert.ToInt32(c.R.ToString());
    int b = Convert.ToInt32(c.G.ToString());
    int d = Convert.ToInt32(c.B.ToString());
    int tot = Convert.ToInt32(ColorTranslator.ToOle(Color.FromArgb(a, b, d)).ToString());
    int j = this.Handle.ToInt32();
    SendMessage(j, PBM_SETBKCOLOR, 0, tot);
    }

    public void SetProgressForeColor(Color c)
    {/// set the forecolor of the bar
    int a = Convert.ToInt32(c.R.ToString());
    int b = Convert.ToInt32(c.G.ToString());
    int d = Convert.ToInt32(c.B.ToString());
    int tot = Convert.ToInt32(ColorTranslator.ToOle(Color.FromArgb(a, b, d)).ToString());
    int j = this.Handle.ToInt32();
    SendMessage(j, PBM_SETBARCOLOR, 0, tot);
    }
    }

    Thanks!

    L M L 3 Replies Last reply
    0
    • E eyalbi007

      Hi All, I'm trying to chenge the color of my ProgressBar without building a new component from scratch. I tried inheriting from System.Windows.Forms.ProgressBar and use the code below (which I found in several forums), but it's not working (if relevant, I'm working on vista 64). Any ideas?

      class MyProgressBar : ProgressBar
      {
      [DllImport("User32.Dll")]
      public static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);

      public const int PBM_SETBKCOLOR = 0x2001;
      public const int PBM_SETBARCOLOR = 0x409;

      public void SetProgressBackColor(Color c)
      {/// set the back color of the bar
      int a = Convert.ToInt32(c.R.ToString());
      int b = Convert.ToInt32(c.G.ToString());
      int d = Convert.ToInt32(c.B.ToString());
      int tot = Convert.ToInt32(ColorTranslator.ToOle(Color.FromArgb(a, b, d)).ToString());
      int j = this.Handle.ToInt32();
      SendMessage(j, PBM_SETBKCOLOR, 0, tot);
      }

      public void SetProgressForeColor(Color c)
      {/// set the forecolor of the bar
      int a = Convert.ToInt32(c.R.ToString());
      int b = Convert.ToInt32(c.G.ToString());
      int d = Convert.ToInt32(c.B.ToString());
      int tot = Convert.ToInt32(ColorTranslator.ToOle(Color.FromArgb(a, b, d)).ToString());
      int j = this.Handle.ToInt32();
      SendMessage(j, PBM_SETBARCOLOR, 0, tot);
      }
      }

      Thanks!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      eyalbi007 wrote:

      int a = Convert.ToInt32(c.R.ToString());

      Seriously, why would you do that? Its effect is equivalent to that of int a = c.R;, but a lot slower. Oh and your SendMessage signature is incorrect. Use:

      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
      static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);

      M 1 Reply Last reply
      0
      • E eyalbi007

        Hi All, I'm trying to chenge the color of my ProgressBar without building a new component from scratch. I tried inheriting from System.Windows.Forms.ProgressBar and use the code below (which I found in several forums), but it's not working (if relevant, I'm working on vista 64). Any ideas?

        class MyProgressBar : ProgressBar
        {
        [DllImport("User32.Dll")]
        public static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);

        public const int PBM_SETBKCOLOR = 0x2001;
        public const int PBM_SETBARCOLOR = 0x409;

        public void SetProgressBackColor(Color c)
        {/// set the back color of the bar
        int a = Convert.ToInt32(c.R.ToString());
        int b = Convert.ToInt32(c.G.ToString());
        int d = Convert.ToInt32(c.B.ToString());
        int tot = Convert.ToInt32(ColorTranslator.ToOle(Color.FromArgb(a, b, d)).ToString());
        int j = this.Handle.ToInt32();
        SendMessage(j, PBM_SETBKCOLOR, 0, tot);
        }

        public void SetProgressForeColor(Color c)
        {/// set the forecolor of the bar
        int a = Convert.ToInt32(c.R.ToString());
        int b = Convert.ToInt32(c.G.ToString());
        int d = Convert.ToInt32(c.B.ToString());
        int tot = Convert.ToInt32(ColorTranslator.ToOle(Color.FromArgb(a, b, d)).ToString());
        int j = this.Handle.ToInt32();
        SendMessage(j, PBM_SETBARCOLOR, 0, tot);
        }
        }

        Thanks!

        M Offline
        M Offline
        musefan
        wrote on last edited by
        #3

        Just make your own control, you can prob do in less code then you using to change it lol

        Life goes very fast. Tomorrow, today is already yesterday.

        1 Reply Last reply
        0
        • L Lost User

          eyalbi007 wrote:

          int a = Convert.ToInt32(c.R.ToString());

          Seriously, why would you do that? Its effect is equivalent to that of int a = c.R;, but a lot slower. Oh and your SendMessage signature is incorrect. Use:

          [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
          static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          I think this would be the best option... int a = (int)Double.Parse(Convert.ToInt32(c.R.ToString())).ToString(); ... Actually that mind need a little work still :laugh: (wheres the 'ive wet myself with laughter' smiley???)

          Life goes very fast. Tomorrow, today is already yesterday.

          E 1 Reply Last reply
          0
          • M musefan

            I think this would be the best option... int a = (int)Double.Parse(Convert.ToInt32(c.R.ToString())).ToString(); ... Actually that mind need a little work still :laugh: (wheres the 'ive wet myself with laughter' smiley???)

            Life goes very fast. Tomorrow, today is already yesterday.

            E Offline
            E Offline
            eyalbi007
            wrote on last edited by
            #5

            Give me a break, it's just a code I copied fom the web... :doh:

            L 1 Reply Last reply
            0
            • E eyalbi007

              Give me a break, it's just a code I copied fom the web... :doh:

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Well, change it.. It needs to be changed anyhow, because of the required IntPtr's. They are actually required here because you trying to run it in 64 bit mode which is expecting 64 bit arguments and you're passing 32 bit ones. I wouldn't trust such badly written code at all

              E 1 Reply Last reply
              0
              • L Lost User

                Well, change it.. It needs to be changed anyhow, because of the required IntPtr's. They are actually required here because you trying to run it in 64 bit mode which is expecting 64 bit arguments and you're passing 32 bit ones. I wouldn't trust such badly written code at all

                E Offline
                E Offline
                eyalbi007
                wrote on last edited by
                #7

                Well, still not working...

                L 1 Reply Last reply
                0
                • E eyalbi007

                  Well, still not working...

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Well, show us the codez.. and tell us what it's doing (if anything)

                  E 1 Reply Last reply
                  0
                  • E eyalbi007

                    Hi All, I'm trying to chenge the color of my ProgressBar without building a new component from scratch. I tried inheriting from System.Windows.Forms.ProgressBar and use the code below (which I found in several forums), but it's not working (if relevant, I'm working on vista 64). Any ideas?

                    class MyProgressBar : ProgressBar
                    {
                    [DllImport("User32.Dll")]
                    public static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);

                    public const int PBM_SETBKCOLOR = 0x2001;
                    public const int PBM_SETBARCOLOR = 0x409;

                    public void SetProgressBackColor(Color c)
                    {/// set the back color of the bar
                    int a = Convert.ToInt32(c.R.ToString());
                    int b = Convert.ToInt32(c.G.ToString());
                    int d = Convert.ToInt32(c.B.ToString());
                    int tot = Convert.ToInt32(ColorTranslator.ToOle(Color.FromArgb(a, b, d)).ToString());
                    int j = this.Handle.ToInt32();
                    SendMessage(j, PBM_SETBKCOLOR, 0, tot);
                    }

                    public void SetProgressForeColor(Color c)
                    {/// set the forecolor of the bar
                    int a = Convert.ToInt32(c.R.ToString());
                    int b = Convert.ToInt32(c.G.ToString());
                    int d = Convert.ToInt32(c.B.ToString());
                    int tot = Convert.ToInt32(ColorTranslator.ToOle(Color.FromArgb(a, b, d)).ToString());
                    int j = this.Handle.ToInt32();
                    SendMessage(j, PBM_SETBARCOLOR, 0, tot);
                    }
                    }

                    Thanks!

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    eyalbi007 wrote:

                    int j = this.Handle.ToInt32();

                    wrong

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                    1 Reply Last reply
                    0
                    • L Lost User

                      Well, show us the codez.. and tell us what it's doing (if anything)

                      E Offline
                      E Offline
                      eyalbi007
                      wrote on last edited by
                      #10

                      Here's the code (that indeed does nothing). What I was hopint it'll do is paint the prorgesss bar in different color than green (the Windows default color), by pressing button2. button1 simply advances the bar.

                          \[DllImport("user32.dll", CharSet = CharSet.Auto)\]
                          private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
                      
                          public const Int32 PBM\_SETBKCOLOR = 0x2001;
                          public const Int32 PBM\_SETBARCOLOR = 0x0409;
                      
                          public void SetProgressBackColor(Color c)
                          {/// set the back color of the bar
                              int a = c.R;
                              int b = c.G;
                              int d = c.B;
                              int tot = ColorTranslator.ToOle(Color.FromArgb(a, b, d));
                              IntPtr j = this.progressBar1.Handle;
                              SendMessage(j, PBM\_SETBKCOLOR, 0, tot);
                          }
                      
                          public void SetProgressForeColor(Color c)
                          {/// set the forecolor of the bar
                              int a = c.R;
                              int b = c.G;
                              int d = c.B;
                              int tot = ColorTranslator.ToOle(Color.FromArgb(a, b, d));
                              IntPtr j = this.progressBar1.Handle;
                              SendMessage(j, PBM\_SETBKCOLOR, 0, tot);
                          }
                      
                          private void button1\_Click(object sender, EventArgs e)
                          {
                              this.progressBar1.Minimum = 1;
                              this.progressBar1.Maximum = 10000;
                              this.progressBar1.Step = 1;
                              this.progressBar1.Value = 1;
                              for (int i = progressBar1.Minimum; i <= progressBar1.Maximum; i++)
                              {
                                  progressBar1.PerformStep();
                              }
                          }
                      
                          private void button2\_Click(object sender, EventArgs e)
                          {
                              SetProgressBackColor(System.Drawing.Color.BlueViolet);
                              SetProgressForeColor(System.Drawing.Color.Red);
                          }
                      
                      L 1 Reply Last reply
                      0
                      • E eyalbi007

                        Here's the code (that indeed does nothing). What I was hopint it'll do is paint the prorgesss bar in different color than green (the Windows default color), by pressing button2. button1 simply advances the bar.

                            \[DllImport("user32.dll", CharSet = CharSet.Auto)\]
                            private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
                        
                            public const Int32 PBM\_SETBKCOLOR = 0x2001;
                            public const Int32 PBM\_SETBARCOLOR = 0x0409;
                        
                            public void SetProgressBackColor(Color c)
                            {/// set the back color of the bar
                                int a = c.R;
                                int b = c.G;
                                int d = c.B;
                                int tot = ColorTranslator.ToOle(Color.FromArgb(a, b, d));
                                IntPtr j = this.progressBar1.Handle;
                                SendMessage(j, PBM\_SETBKCOLOR, 0, tot);
                            }
                        
                            public void SetProgressForeColor(Color c)
                            {/// set the forecolor of the bar
                                int a = c.R;
                                int b = c.G;
                                int d = c.B;
                                int tot = ColorTranslator.ToOle(Color.FromArgb(a, b, d));
                                IntPtr j = this.progressBar1.Handle;
                                SendMessage(j, PBM\_SETBKCOLOR, 0, tot);
                            }
                        
                            private void button1\_Click(object sender, EventArgs e)
                            {
                                this.progressBar1.Minimum = 1;
                                this.progressBar1.Maximum = 10000;
                                this.progressBar1.Step = 1;
                                this.progressBar1.Value = 1;
                                for (int i = progressBar1.Minimum; i <= progressBar1.Maximum; i++)
                                {
                                    progressBar1.PerformStep();
                                }
                            }
                        
                            private void button2\_Click(object sender, EventArgs e)
                            {
                                SetProgressBackColor(System.Drawing.Color.BlueViolet);
                                SetProgressForeColor(System.Drawing.Color.Red);
                            }
                        
                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Well for one thing, the SendMessage signature still isn't correct. You absolutely have to use those annoying IntPtr's otherwise it won't work. But then there's something else, I once heard (not sure how true it is) that progress bars in Vista may only be green yellow or red and that they have a special (different) message to change it. May be worth investigating.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups