请问如何将进步百分比同时作为文字显示在进度条上?
------解决思路----------------------
重写可以参照这里
这里显示的文字是进度条百分百,想修改成其他的话,修改string s的值即可
------解决思路----------------------
楼上那位仁兄提供的代码,测试有效,效果很赞,已翻译为vb.net
Class textProgressBar
Inherits System.Windows.Forms.ProgressBar
<System.Runtime.InteropServices.DllImport("user32.dll ")> _
Private Shared Function GetWindowDC(hWnd As IntPtr) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("user32.dll ")> _
Private Shared Function ReleaseDC(hWnd As IntPtr, hDC As IntPtr) As Integer
End Function
Private _TextColor As System.Drawing.Color = System.Drawing.Color.Black
Private _TextFont As New System.Drawing.Font("SimSun ", 12)
Public Property TextColor() As System.Drawing.Color
Get
Return _TextColor
End Get
Set(value As System.Drawing.Color)
_TextColor = value
Me.Invalidate()
End Set
End Property
Public Property TextFont() As System.Drawing.Font
Get
Return _TextFont
End Get
Set(value As System.Drawing.Font)
_TextFont = value
Me.Invalidate()
End Set
End Property
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
If m.Msg = &HF OrElse m.Msg = &H133 Then
'拦截系统消息,获得当前控件进程以便重绘。
'一些控件(如TextBox、Button等)是由系统进程绘制,重载OnPaint方法将不起作用.
'所有这里并没有使用重载OnPaint方法绘制TextBox边框。
'
'MSDN:重写 OnPaint 将禁止修改所有控件的外观。
'那些由 Windows 完成其所有绘图的控件(例如 Textbox)从不调用它们的 OnPaint 方法,
'因此将永远不会使用自定义代码。请参见您要修改的特定控件的文档,
'查看 OnPaint 方法是否可用。如果某个控件未将 OnPaint 作为成员方法列出,
'则您无法通过重写此方法改变其外观。
'
'MSDN:要了解可用的 Message.Msg、Message.LParam 和 Message.WParam 值,
'请参考位于 MSDN Library 中的 Platform SDK 文档参考。可在 Platform SDK(“Core SDK”一节)
'下载中包含的 windows.h 头文件中找到实际常数值,该文件也可在 MSDN 上找到。
Dim hDC As IntPtr = GetWindowDC(m.HWnd)
If hDC.ToInt32() = 0 Then
Return
End If
'base.OnPaint(e);
Dim g As System.Drawing.Graphics = Graphics.FromHdc(hDC)
Dim brush As New SolidBrush(_TextColor)
Dim s As String = String.Format("{0}%", Me.Value * 100 / Me.Maximum)
Dim size As SizeF = g.MeasureString(s, _TextFont)
Dim x As Single = (Me.Width - size.Width) / 2
Dim y As Single = (Me.Height - size.Height) / 2
g.DrawString(s, _TextFont, brush, x, y)
'返回结果
m.Result = IntPtr.Zero
'释放
ReleaseDC(m.HWnd, hDC)
End If
End Sub
End Class