Text over Progressbar - Question
-
Hi all, I found the following code, that allows one to write a string on a
ProgressBar
... Only thing is that I can't get it to work :confused::confused: Can anyone else make it work, and if so ... what am I doing wrong?// call the method SetProgressBarText(progressBar1, null, ProgressBarTextLocation.Centered, Color.Black, SystemFonts.DefaultFont); // Set the ProgressBar Text method private void SetProgressBarText(System.Windows.Forms.ProgressBar Target, //The target progress bar string Text, //The text to show in the progress bar ProgressBarTextLocation Location, //Where the text is to be placed System.Drawing.Color TextColor, //The color the text is to be drawn in System.Drawing.Font TextFont //The font we use to draw the text ) { //Make sure we didn't get a null progress bar if (Target == null) throw new ArgumentException("Null Target"); //Now we can get to the real code //Check to see if we are to add in the percent if (string.IsNullOrEmpty(Text)) { //We are to add in the percent meaning we got a null or empty Text //We give text a string value representing the percent int percent = (int)(((double)(Target.Value - Target.Minimum) / (double)(Target.Maximum - Target.Minimum)) * 100); Text = percent.ToString() + "%"; } //Now we can add in the text //gr will be the graphics object we use to draw on Target using (Graphics gr = Target.CreateGraphics()) { gr.DrawString(Text, TextFont, //The font we will draw it it (TextFont) new SolidBrush(TextColor), //The brush we will use to draw it //Where we will draw it new PointF( // X location (Center or Left) Location == ProgressBarTextLocation.Left ? 5 : //Left side progressBar1.Width / 2 - (gr.MeasureStri
-
Hi all, I found the following code, that allows one to write a string on a
ProgressBar
... Only thing is that I can't get it to work :confused::confused: Can anyone else make it work, and if so ... what am I doing wrong?// call the method SetProgressBarText(progressBar1, null, ProgressBarTextLocation.Centered, Color.Black, SystemFonts.DefaultFont); // Set the ProgressBar Text method private void SetProgressBarText(System.Windows.Forms.ProgressBar Target, //The target progress bar string Text, //The text to show in the progress bar ProgressBarTextLocation Location, //Where the text is to be placed System.Drawing.Color TextColor, //The color the text is to be drawn in System.Drawing.Font TextFont //The font we use to draw the text ) { //Make sure we didn't get a null progress bar if (Target == null) throw new ArgumentException("Null Target"); //Now we can get to the real code //Check to see if we are to add in the percent if (string.IsNullOrEmpty(Text)) { //We are to add in the percent meaning we got a null or empty Text //We give text a string value representing the percent int percent = (int)(((double)(Target.Value - Target.Minimum) / (double)(Target.Maximum - Target.Minimum)) * 100); Text = percent.ToString() + "%"; } //Now we can add in the text //gr will be the graphics object we use to draw on Target using (Graphics gr = Target.CreateGraphics()) { gr.DrawString(Text, TextFont, //The font we will draw it it (TextFont) new SolidBrush(TextColor), //The brush we will use to draw it //Where we will draw it new PointF( // X location (Center or Left) Location == ProgressBarTextLocation.Left ? 5 : //Left side progressBar1.Width / 2 - (gr.MeasureStri
Nevermind :doh: :sigh: .... just through the method call within a Timer Tick and everything works ....
The only programmers that are better C# programmers, are those who look like this -> :bob:
:java: Programm3r My Blog: ^_^
-
Hi all, I found the following code, that allows one to write a string on a
ProgressBar
... Only thing is that I can't get it to work :confused::confused: Can anyone else make it work, and if so ... what am I doing wrong?// call the method SetProgressBarText(progressBar1, null, ProgressBarTextLocation.Centered, Color.Black, SystemFonts.DefaultFont); // Set the ProgressBar Text method private void SetProgressBarText(System.Windows.Forms.ProgressBar Target, //The target progress bar string Text, //The text to show in the progress bar ProgressBarTextLocation Location, //Where the text is to be placed System.Drawing.Color TextColor, //The color the text is to be drawn in System.Drawing.Font TextFont //The font we use to draw the text ) { //Make sure we didn't get a null progress bar if (Target == null) throw new ArgumentException("Null Target"); //Now we can get to the real code //Check to see if we are to add in the percent if (string.IsNullOrEmpty(Text)) { //We are to add in the percent meaning we got a null or empty Text //We give text a string value representing the percent int percent = (int)(((double)(Target.Value - Target.Minimum) / (double)(Target.Maximum - Target.Minimum)) * 100); Text = percent.ToString() + "%"; } //Now we can add in the text //gr will be the graphics object we use to draw on Target using (Graphics gr = Target.CreateGraphics()) { gr.DrawString(Text, TextFont, //The font we will draw it it (TextFont) new SolidBrush(TextColor), //The brush we will use to draw it //Where we will draw it new PointF( // X location (Center or Left) Location == ProgressBarTextLocation.Left ? 5 : //Left side progressBar1.Width / 2 - (gr.MeasureStri
public class ProgressLabel: ProgressBar {
private static StringFormat sfCenter = new StringFormat() {
Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
private Color textColor = DefaultTextColor;
private string progressString;
public ProgressLabel() { SetStyle(ControlStyles.AllPaintingInWmPaint, true); }
protected override void OnCreateControl() {
progressString = null;
base.OnCreateControl();
}
protected override void WndProc(ref Message m) {
switch(m.Msg) {
case 15: if(HideBar) base.WndProc(ref m);
else {
ProgressBarStyle style = Style;
if(progressString == null) {
progressString = Text;
if(!HideBar && style != ProgressBarStyle.Marquee) {
int range = Maximum-Minimum;
int value = Value;
if(range > 42949672) { value = (int)((uint)value>>7); range = (int)((uint)range>>7); }
if(range > 0) progressString = string.Format(progressString.Length == 0 ? "{0}%" : "{1}: {0}%",
value*100/range, progressString);
}
}
if(progressString.Length == 0) base.WndProc(ref m);
else using(Graphics g = CreateGraphics()) {
base.WndProc(ref m);
OnPaint(new PaintEventArgs(g, ClientRectangle));
}
}
break;
case 0x402: goto case 0x406;
case 0x406: progressString = null;
base.WndProc(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
protected override void OnPaint(PaintEventArgs e) {
Rectangle cr = ClientRectangle;
RectangleF crF = new RectangleF(cr.Left, cr.Top, cr.Width, cr.Height);
using(Brush br = new SolidBrush(TextColor))
e.Graphics.DrawString(progressString, Font, br, crF, sfCenter);
base.OnPaint(e);
}
public bool HideBar {
get { return GetStyle(ControlStyles.UserPaint); }
set { if(HideBar != value) { SetStyle(ControlStyles.UserPaint, value); Refresh(); } }
}
public static Color DefaultTextColor {
get { return SystemColors.ControlText; }
}
public Color TextColor {
get { return textColor; }
set { textColor = value; }
}
public override string Text {
get { return base.Text; }
set { if(value != Text) { base.Text = value; progressString = null; } }
}
public override Font Font {
get { return base.Font; }
set { base.Font = value; }
}
} -
public class ProgressLabel: ProgressBar {
private static StringFormat sfCenter = new StringFormat() {
Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
private Color textColor = DefaultTextColor;
private string progressString;
public ProgressLabel() { SetStyle(ControlStyles.AllPaintingInWmPaint, true); }
protected override void OnCreateControl() {
progressString = null;
base.OnCreateControl();
}
protected override void WndProc(ref Message m) {
switch(m.Msg) {
case 15: if(HideBar) base.WndProc(ref m);
else {
ProgressBarStyle style = Style;
if(progressString == null) {
progressString = Text;
if(!HideBar && style != ProgressBarStyle.Marquee) {
int range = Maximum-Minimum;
int value = Value;
if(range > 42949672) { value = (int)((uint)value>>7); range = (int)((uint)range>>7); }
if(range > 0) progressString = string.Format(progressString.Length == 0 ? "{0}%" : "{1}: {0}%",
value*100/range, progressString);
}
}
if(progressString.Length == 0) base.WndProc(ref m);
else using(Graphics g = CreateGraphics()) {
base.WndProc(ref m);
OnPaint(new PaintEventArgs(g, ClientRectangle));
}
}
break;
case 0x402: goto case 0x406;
case 0x406: progressString = null;
base.WndProc(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
protected override void OnPaint(PaintEventArgs e) {
Rectangle cr = ClientRectangle;
RectangleF crF = new RectangleF(cr.Left, cr.Top, cr.Width, cr.Height);
using(Brush br = new SolidBrush(TextColor))
e.Graphics.DrawString(progressString, Font, br, crF, sfCenter);
base.OnPaint(e);
}
public bool HideBar {
get { return GetStyle(ControlStyles.UserPaint); }
set { if(HideBar != value) { SetStyle(ControlStyles.UserPaint, value); Refresh(); } }
}
public static Color DefaultTextColor {
get { return SystemColors.ControlText; }
}
public Color TextColor {
get { return textColor; }
set { textColor = value; }
}
public override string Text {
get { return base.Text; }
set { if(value != Text) { base.Text = value; progressString = null; } }
}
public override Font Font {
get { return base.Font; }
set { base.Font = value; }
}
}Thanks firda, been looking for that code all over.