When overriding OnPaint()
and OnPaintBackground()
events as follows :
protected override void OnPaint( PaintEventArgs e ) {
base.OnPaint( e );
try {
switch ( TextVisibility ) {
case ProgressBarTextVisibility.None:
return;
case ProgressBarTextVisibility.CustomText:
if ( progressText=="" ) { return; }
break;
case ProgressBarTextVisibility.Percentage:
progressText = Value.ToString()+"%";
break;
}
SizeF charSize = e.Graphics.MeasureString( progressText, ProgressFont );
e.Graphics.DrawString(
progressText,
ProgressFont,
new SolidBrush(ProgressTextColor),
new PointF(
Width/2-( charSize.Width/2.0F ),
Height/2-( charSize.Height/2.0F )
)
);
} catch ( Exception ee ) {
}
}
protected override void OnPaintBackground( PaintEventArgs pevent ) {
base.OnPaintBackground( pevent );
}
The result is this (progressbar is at 50%) [ pointing out that the background is not drawn at all. ] :
I get the same result as depicted above if i create a new Label
control and set it's background to Color.Transparent
and set dock to Full
.
There are plenty of examples on how to add text AND draw a custom progress bar style, however I wish to keep the original progress bar visual in-tact without re-inventing the wheel. The progress portion works fine and I am happy with the native style and do not wish to redraw my own custom adaptation of it. I only wish to overlay text on top.
I have considered a Timer
control / etc, however I also wish to keep this using as few resources as possible.
Here are some of the resources I have already been through :
- How do I put text on ProgressBar?
- Add The Percent Into A Progress Bar (Updated)
- How do put text on ProgressBar?
- ... and many more similar to above
I am using c# and the control is a custom control that extends the .NET native ProgressBar control.
UPDATE
I will use this section to post ongoing research as I find more relevant data that is different.
- Using Visual Styles with Custom and Owner-Drawn Controls - this is not in .NET
- OwnerDraw ComboBox is not “styled” anymore - tried this, but progressbar doen't support
DrawMode
- Enabling Visual Styles - this is more for how to enable styling of controls for applications compiled that don't already do this natively (for example, Visual Basic 6, Visual C++ 6, etc)
- VisualStyleElement.ProgressBar.Bar Class - seems to be warm. no full example on implementation so will play the guessing game to try this.
- TabControl OwnerDraw and keep the windows theme... - not a whole lot of information here, but this is how I kind of guessed at the
VisualStyleElement.ProgressBar.Bar
class as possibly existing.