[英]Show Text on ProgressBar in StatusStrip
I am new in windows programming . 我是Windows编程的新手。 In C# I was working with using Visual Studio 2017. Now, Im stuck with a problem. 在C#中,我正在使用Visual Studio2017。现在,我遇到了一个问题。 The Problem is that, I am trying to display some text (the progress value) in ProgressBar
in the StatusStrip
but can't find a proper working way to do that. 问题是,我试图在StatusStrip
ProgressBar
中显示一些文本(进度值),但是找不到合适的工作方式来做到这一点。 :-( :-(
Can anyone please provide me some ideas or solution to this problem? 谁能为我提供一些解决此问题的想法或解决方案? I shall be glad and thankful to you ! 我会很高兴和感谢你! Your answers will be appreciated greatly. 您的回答将不胜感激。 :-) :-)
ToolStripProgressBar
component uses a ProgressBar
to show the progress and the control doesn't show text. ToolStripProgressBar
组件使用ProgressBar
来显示进度,而控件不显示文本。 To be able to render a text for control, you need to paint the value yourself. 为了能够呈现文本以进行控制,您需要自己绘制值。 To do so, you can create a custom item deriving from ToolStripProgressBar
. 为此,您可以创建一个从ToolStripProgressBar
派生的自定义项目。 Then you can use NativeWindow
to assign a custom code to WM_PAINT
message of the control: 然后,您可以使用NativeWindow
将自定义代码分配给控件的WM_PAINT
消息:
using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
public class MyToolStripProgressBar : ToolStripProgressBar
{
public MyToolStripProgressBar() : base()
{
this.Control.HandleCreated += Control_HandleCreated;
}
private void Control_HandleCreated(object sender, EventArgs e)
{
var s = new ProgressBarHelper((ProgressBar)this.Control);
}
}
public class ProgressBarHelper : NativeWindow
{
ProgressBar c;
public ProgressBarHelper(ProgressBar progressBar)
{
c = progressBar;
this.AssignHandle(c.Handle);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xF /*WM_PAINT*/)
{
using (var g = c.CreateGraphics())
TextRenderer.DrawText(g, c.Value.ToString(),
c.Font, c.ClientRectangle, c.ForeColor);
}
}
}
progressBar1.CreateGraphics().DrawString(progressBar1.Value.ToString(), new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));
I had the same question just a jew days ago an ran into this little piece of code that does the job perfectly :) 就在几天前,我遇到了一个同样的问题,一个小巧的代码运行得很好:)
Got the code and explanation from this Website 获得了本网站的代码和解释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.