Coloring ProgressBar
-
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!
-
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!
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); -
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!
-
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);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.
-
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.
-
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
-
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
-
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!
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
-
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); }
-
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); }
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.