在StatusStrip中的ProgressBar上显示文本 [英] Show Text on ProgressBar in StatusStrip
问题描述
我是 Windows编程中的新手.在C#中,我正在使用Visual Studio2017.现在,我遇到了一个问题.问题是,我试图在StatusStrip
的ProgressBar
中显示一些文本(进度值),但是找不到合适的工作方式来做到这一点. :-(
I am new in windows programming. In C# I was working with using Visual Studio 2017. Now, Im stuck with a problem. 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. :-(
有人可以向我提供一些解决此问题的想法或解决方案吗?我将为您感到高兴和感谢! 您的回答将不胜感激. :-)
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
组件使用ProgressBar
显示进度,而控件不显示文本.为了能够呈现文本以进行控制,您需要自己绘制值.为此,您可以创建一个源自ToolStripProgressBar
的自定义项目.然后,您可以使用NativeWindow
将自定义代码分配给控件的WM_PAINT
消息:
ToolStripProgressBar
component uses a ProgressBar
to show the progress and the control doesn't show text. 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
. Then you can use NativeWindow
to assign a custom code to WM_PAINT
message of the control:
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);
}
}
}
这篇关于在StatusStrip中的ProgressBar上显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!