[英]How to display text on a progress bar control, and keep original background
When overriding OnPaint()
and OnPaintBackground()
events as follows : 如下重写OnPaint()
和OnPaintBackground()
事件时:
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. 结果就是这样(进度条为50%)[指出根本没有绘制背景。 ] : ]:
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
. 如果创建一个新的Label
控件并将其背景设置为Color.Transparent
并将dock设置为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. 我考虑过Timer
控件/ etc,但是我也希望尽可能少地使用它。
Here are some of the resources I have already been through : 这是我已经使用过的一些资源:
I am using c# and the control is a custom control that extends the .NET native ProgressBar control. 我正在使用c#,并且该控件是扩展.NET本机ProgressBar控件的自定义控件。
I will use this section to post ongoing research as I find more relevant data that is different. 当我发现更多不同的相关数据时,我将使用这一部分来发布正在进行的研究。
DrawMode
OwnerDraw ComboBox不再“设置样式” -尝试过此方法,但是progressbar不支持DrawMode
VisualStyleElement.ProgressBar.Bar
class as possibly existing. TabControl OwnerDraw并保留Windows主题...-这里没有很多信息,但这是我对VisualStyleElement.ProgressBar.Bar
类可能存在的猜测。 Override OnLoad and call SetStyle(ControlStyles.AllPaintingInWmPaint, false) and SetStyle(ControlStyles.UserPaint, true). 重写OnLoad并调用SetStyle(ControlStyles.AllPaintingInWmPaint,false)和SetStyle(ControlStyles.UserPaint,true)。 That will separate background and control detail drawing. 那将分开背景和控制细节图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.