如何将 Label 用作 ProgressBar

How to use a Label as a ProgressBar

我正在做的项目有问题。

我有一个包含 8 个标签(标签 1、标签 2 等...)的面板

我想做的是把一个标签改成一个有文字的进度条。 这是动态发生的。

注意:要改回来

我可以这样做吗?

此外,当达到配额时,我必须将进度条颜色设置为红色和橙色。

我试过了:

Label1 = Progressbar1
Progressbar1.value = 75

但这不起作用 呃

What I want to do is change a label into a progress bar with text. 这样一来,它看起来就不像带有动画的标准进度条了。为了补偿这一点,将使用渐变:

Imports System.Drawing.Drawing2D

Public Class LabeledMeter
    Inherits Label

    Public Property MinValue As Int32 = 0
    Public Property MaxValue As Int32 = 100
    Private mVal As Int32 = 1
    Private mValue As Double
    Public Property Value As Int32
        Get
            Return mVal
        End Get
        Set(value As Int32)

            If mVal <= MaxValue AndAlso mVal >= MinValue Then
                mVal = value
                mValue = mVal / MaxValue
                Me.Invalidate()
            End If
        End Set
    End Property

    Public Property StartColor As Color = Color.LimeGreen
    Public Property EndColor As Color = Color.Firebrick

    Private meter As Boolean = False
    Public Property MeterDisplay As Boolean
        Get
            Return meter
        End Get
        Set(value As Boolean)
            If meter <> value Then
                meter = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Public Sub New()
        MyBase.TextAlign = ContentAlignment.MiddleCenter
        MyBase.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        If MeterDisplay = False Then
            MyBase.OnPaint(e)
            Return
        End If

        ' fun and games
        Dim c2 As Color = EndColor
        'If mValue < 0.51 Then
        '    c2 = Color.Yellow
        'End If

        Dim rect = New Rectangle(0, 0,
                        Convert.ToInt32(Width * mValue), Height)
        Using br As New LinearGradientBrush(rect, StartColor, c2, LinearGradientMode.Horizontal)
            e.Graphics.FillRectangle(br, rect)
        End Using

        ' could draw the Text - specs are not clear
        TextRenderer.DrawText(e.Graphics, mValue.ToString("P1"), MyBase.Font, ClientRectangle, MyBase.ForeColor)
    End Sub

    Protected Overrides Sub OnHandleCreated(e As EventArgs)
        MyBase.OnHandleCreated(e)
        MyBase.AutoSize = False
    End Sub

End Class

注释

  • MeterDisplay 属性 切换测光或仅标准标签模式。
  • Value 更改填充百分比。它仅在 MeterDisplay 为 True 时重绘。
  • 文本与 XX% 测试显示可以 属性 驱动。
  • 降低起始颜色的 alpha 有时可以提供良好的视觉效果