Visual Basic 圆形进度条

     2023-02-23     163

关键词:

【中文标题】Visual Basic 圆形进度条【英文标题】:Visual basic circular progress bar 【发布时间】:2015-01-11 18:11:15 【问题描述】:

我正在尝试制作一个具有良好 UI 的软件,但我在 VB 方面并不专业...... 如何制作圆形进度条?

举例

【问题讨论】:

见codeproject.com/Articles/30625/Circular-Progress-Indicator和codeproject.com/Articles/14855/SQL-Server-Circular-Progress-Bar 这是用 c++ 构建的,我想用 Visual Basic 构建它。 它们在 C++ 中不是;它们可以很容易地转换为 VB。关键是它们(和其他)已经构建并可以使用;您不必重新创建***。 好的,我会尝试将其转换为 vb 并了解它是如何工作的。 【参考方案1】:

使用 GDI+ 自己绘制如何。

您可以稍后将其转换为您自己的用户控件,但这将帮助您入门。它应该是不言自明的:

Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    DrawProgress(e.Graphics, New Rectangle(5, 5, 60, 60), 40)
    DrawProgress(e.Graphics, New Rectangle(80, 5, 60, 60), 80)
    DrawProgress(e.Graphics, New Rectangle(155, 5, 60, 60), 57)
End Sub

Private Sub DrawProgress(g As Graphics, rect As Rectangle, percentage As Single)
    'work out the angles for each arc
    Dim progressAngle = CSng(360 / 100 * percentage)
    Dim remainderAngle = 360 - progressAngle

    'create pens to use for the arcs
    Using progressPen As New Pen(Color.LightSeaGreen, 2), remainderPen As New Pen(Color.LightGray, 2)
        'set the smoothing to high quality for better output
        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        'draw the blue and white arcs
        g.DrawArc(progressPen, rect, -90, progressAngle)
        g.DrawArc(remainderPen, rect, progressAngle - 90, remainderAngle)
    End Using

    'draw the text in the centre by working out how big it is and adjusting the co-ordinates accordingly
    Using fnt As New Font(Me.Font.FontFamily, 14)
        Dim text As String = percentage.ToString + "%"
        Dim textSize = g.MeasureString(text, fnt)
        Dim textPoint As New Point(CInt(rect.Left + (rect.Width / 2) - (textSize.Width / 2)), CInt(rect.Top + (rect.Height / 2) - (textSize.Height / 2)))
        'now we have all the values draw the text
        g.DrawString(text, fnt, Brushes.Black, textPoint)
    End Using
End Sub

输出

【讨论】:

谢谢...效果很好,但是如果您知道如何使它高于“高质量”? 哥们,你是高手!【参考方案2】:

@faresabb2 在代码最开始的 Form2.Paint 子中,放

e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality

【讨论】:

【参考方案3】:

这是一个如何在需要时更新进度圆形条的示例,不会因刷新而闪烁。

基于马特的代码

只需将代码复制到您的表单 Paint Event 中,适当地更改矩形大小和位置以在您的表单中承载圆。 percent 是一个全局变量,当它发生变化时,可以调用 me.refresh() 方法来触发重绘!

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint

    Dim g As Graphics = e.Graphics
    Dim rect As New Rectangle(70, 45, 90, 90)


    Dim curvatura_progress = CSng(360 / 100 * percent)
    Dim curvatura_rimanente = 360 - curvatura_progress 


    Using tratto_progresso As New Pen(Color.Lime, 4), tratto_rimanente As New Pen(Color.White, 4)

        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

        g.DrawArc(tratto_progresso, rect, -90, curvatura_progress)
        g.DrawArc(tratto_rimanente, rect, curvatura_progress - 90, curvatura_rimanente)
    End Using

           Using fnt As New Font(Me.Font.FontFamily, 14)

        Dim text As String = percent.ToString + "%"

                    Dim textSize = g.MeasureString(text, fnt)
        Dim textPoint As New Point(CInt(rect.Left + (rect.Width / 2) - (textSize.Width / 2)), CInt(rect.Top + (rect.Height / 2) - (textSize.Height / 2)))

        g.DrawString(text, fnt, Brushes.Black, textPoint)

    End Using

End Sub

【讨论】:

只需将 Dim percent 添加为 Single 可能会被 BR1COP 遗漏【参考方案4】:

这里是用户控件

Imports System.ComponentModel
Imports System.Drawing.Drawing2D
Public Class CircularProgressBar
    Inherits UserControl
    Public Enum _ProgressShape
        Round
        Flat
    End Enum
    Public Enum _TextMode
        None
        Value
        Percentage
        [Custom]
    End Enum
    Private _Value As Integer = 0
    Private _Maximum As Integer = 100
    Private _LineWitdh As Integer = 5
    Private _BarWidth As Single = 11
    Private _ProgressColor1 As Color = Color.Orange
    Private _ProgressColor2 As Color = Color.Orange
    Private _LineColor As Color = Color.LightGray
    Private _GradientMode As LinearGradientMode = LinearGradientMode.ForwardDiagonal
    Private ProgressShapeVal As _ProgressShape
    Private ProgressTextMode As _TextMode
    Private _ShadowOffset As Single = 0
    Public Sub New()
        MyBase.SuspendLayout()
        'SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.SupportsTransparentBackColor Or ControlStyles.Opaque, True)
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        SetStyle(ControlStyles.Opaque, True)
        BackColor = SystemColors.Control
        ForeColor = Color.DimGray
        Size = New Size(75, 75)
        Font = New Font("Segoe UI", 15)
        MinimumSize = New Size(58, 58)
        DoubleBuffered = True
        LineColor = Color.LightGray
        Value = 50
        ProgressShape = _ProgressShape.Flat
        TextMode = _TextMode.Percentage
        MyBase.ResumeLayout(False)
        MyBase.PerformLayout()
    End Sub
    <Description("Integer Value that determines the position of the Progress Bar."), Category("Behavior")>
    Public Property Value() As Long
        Get
            Return _Value
        End Get
        Set
            If Value > _Maximum Then
                Value = _Maximum
            End If
            _Value = Value
            Invalidate()
        End Set
    End Property
    <Description("Gets or Sets the Maximum Value of the Progress bar."), Category("Behavior")>
    Public Property Maximum() As Long
        Get
            Return _Maximum
        End Get
        Set
            If Value < 1 Then
                Value = 1
            End If
            _Maximum = Value
            Invalidate()
        End Set
    End Property
    <Description("Initial Color of Progress Bar"), Category("Appearance")>
    Public Property BarColor1() As Color
        Get
            Return _ProgressColor1
        End Get
        Set
            _ProgressColor1 = Value
            Invalidate()
        End Set
    End Property
    <Description("Final Color of Progress Bar"), Category("Appearance")> Public Property BarColor2() As Color
        Get
            Return _ProgressColor2
        End Get
        Set
            _ProgressColor2 = Value
            Invalidate()
        End Set
    End Property
    <Description("Progress Bar Width"), Category("Appearance")>
    Public Property BarWidth() As Single
        Get
            Return _BarWidth
        End Get
        Set
            _BarWidth = Value
            Invalidate()
        End Set
    End Property
    <Description("Modo del Gradiente de Color"), Category("Appearance")>
    Public Property GradientMode() As LinearGradientMode
        Get
            Return _GradientMode
        End Get
        Set
            _GradientMode = Value
            Invalidate()
        End Set
    End Property
    <Description("Color de la Linea Intermedia"), Category("Appearance")>
    Public Property LineColor() As Color
        Get
            Return _LineColor
        End Get
        Set
            _LineColor = Value
            Invalidate()
        End Set
    End Property
    <Description("Width of Intermediate Line"), Category("Appearance")>
    Public Property LineWidth() As Integer
        Get
            Return _LineWitdh
        End Get
        Set
            _LineWitdh = Value
            Invalidate()
        End Set
    End Property
    <Description("Get or Set the Shape of the progress bar terminals."), Category("Appearance")>
    Public Property ProgressShape() As _ProgressShape
        Get
            Return ProgressShapeVal
        End Get
        Set
            ProgressShapeVal = Value
            Invalidate()
        End Set
    End Property
    <Description("Modo del Gradiente de Color"), Category("Appearance")>
    Public Property ShadowOffset() As Integer
        Get
            Return _ShadowOffset
        End Get
        Set
            If Value > 2 Then
                Value = 2
            End If
            _ShadowOffset = Value
            Invalidate()
        End Set
    End Property
    <Description("Get or Set the Mode as the Text is displayed inside the Progress bar."), Category("Behavior")>
    Public Property TextMode() As _TextMode
        Get
            Return ProgressTextMode
        End Get
        Set
            ProgressTextMode = Value
            Invalidate()
        End Set
    End Property
    Protected Overloads Overrides Sub OnResize(e As EventArgs)
        MyBase.OnResize(e)
        SetStandardSize()
    End Sub
    Protected Overloads Overrides Sub OnSizeChanged(e As EventArgs)
        MyBase.OnSizeChanged(e)
        SetStandardSize()
    End Sub
    Protected Overloads Overrides Sub OnPaintBackground(p As PaintEventArgs)
        MyBase.OnPaintBackground(p)
    End Sub
    Private Sub SetStandardSize()
        Dim _Size As Integer = Math.Max(Width, Height)
        Size = New Size(_Size, _Size)
    End Sub
    Public Sub Increment(Val As Integer)
        _Value += Val
        Invalidate()
    End Sub
    Public Sub Decrement(Val As Integer)
        _Value -= Val
        Invalidate()
    End Sub
    'Protected Overloads Overrides Sub OnPaint(e As PaintEventArgs)
    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Using bitmap As New Bitmap(Width, Height)
            Using graphics As Graphics = Graphics.FromImage(bitmap)
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
                graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
                graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
                PaintTransparentBackground(Me, e)
                Dim rect As Rectangle = New Rectangle(10, 10, MyBase.Width - 20, MyBase.Width - 20)
                Using mBackColor As Brush = New SolidBrush(BackColor)
                    graphics.FillEllipse(mBackColor, rect)
                End Using
                Using pen2 As New Pen(LineColor, LineWidth)
                    graphics.DrawEllipse(pen2, rect)
                End Using
                Using brush As New LinearGradientBrush(ClientRectangle, _ProgressColor1, _ProgressColor2, GradientMode)
                    Using pen As New Pen(brush, BarWidth)
                        Select Case ProgressShapeVal
                            Case _ProgressShape.Round
                                pen.StartCap = LineCap.Round
                                pen.EndCap = LineCap.Round
                                Exit Select
                            Case _ProgressShape.Flat
                                pen.StartCap = LineCap.Flat
                                pen.EndCap = LineCap.Flat
                                Exit Select
                        End Select
                        graphics.DrawArc(pen, rect, -90, CType((360 / _Maximum) * _Value, Integer))

                    End Using
                End Using

                Select Case TextMode
                    Case _TextMode.None
                        Text = String.Empty
                        Exit Select
                    Case _TextMode.Value
                        Text = _Value.ToString()
                        Exit Select
                    Case _TextMode.Percentage
                        Text = Convert.ToString((100 / _Maximum) * _Value) & "%"
                        Exit Select
                    Case _TextMode.Custom
                        Text = Text
                        Exit Select
                    Case Else
                        Exit Select
                End Select

                If Text IsNot String.Empty Then
                    Dim MS As SizeF = graphics.MeasureString(Text, Font)
                    Dim shadowBrush As New SolidBrush(Color.FromArgb(100, ForeColor))
                    If ShadowOffset > 0 Then graphics.DrawString(Text, Font, shadowBrush, (Width / 2 - MS.Width / 2) + ShadowOffset, (Height / 2 - MS.Height / 2) + ShadowOffset)
                    graphics.DrawString(Text, Font, New SolidBrush(ForeColor), (Width / 2 - MS.Width / 2), (Height / 2 - MS.Height / 2))
                End If
                MyBase.OnPaint(e)

                e.Graphics.DrawImage(bitmap, 0, 0)
                graphics.Dispose()
            End Using
        End Using
    End Sub
    Private Shared Sub PaintTransparentBackground(c As Control, e As PaintEventArgs)
        If c.Parent Is Nothing OrElse Not Application.RenderWithVisualStyles Then
            Return
        End If
        ButtonRenderer.DrawParentBackground(e.Graphics, c.ClientRectangle, c)
    End Sub

    Private Sub FillCircle(g As Graphics, brush As Brush, centerX As Single, centerY As Single, radius As Single)
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
        Using gp As New System.Drawing.Drawing2D.GraphicsPath()
            g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius)
        End Using
    End Sub
End Class

【讨论】:

ios制作个圆形进度条

参考技术A1.需要做个这样的圆形进度条 自定义弹窗view就不说了,主要是这个圆形进度view,底色是灰色然后有进度灰色被黑色覆盖或者说变成黑色首先我们自定一个view,加个uilabel做进度lbael@property(nonatomic,strong)UILabel*percentLabel;/... 查看详情

圆形进度条 Android

】圆形进度条Android【英文标题】:CircularProgressBarAndroid【发布时间】:2015-06-0320:06:35【问题描述】:我想创建一个分段的圆形进度条,其行为类似于倒数计时器,这样每个分段都有自己的时间限制。有一个iOS应用pTimer+,我想在An... 查看详情

带有图像和圆形进度条的圆形按钮

】带有图像和圆形进度条的圆形按钮【英文标题】:Circularbuttonwithimageandcircularprogressbar【发布时间】:2019-12-2819:29:53【问题描述】:我正在尝试制作一个按钮,里面有一个图像,外面有一个圆形进度条,按下按钮后是彩色的。我... 查看详情

带圆角的 Android 圆形进度条

】带圆角的Android圆形进度条【英文标题】:Androidcircularprogressbarwithroundedcorners【发布时间】:2016-08-0623:37:54【问题描述】:我正在尝试获得一个带有圆角的圆形进度条,如下所示。但到目前为止,我无法获得圆角,我能够获得圆... 查看详情

googlechrome圆形进度条

Conmajia?2012UpdatedonFeb.21,2018GoogleChrome的圆形进度条。Demo功能显示百分比(0-100)。如果进度值达到100%,则将闪烁指定次数。属性BlinkCount,结束后闪烁几次。0为不闪烁,默认2.BlinkSpeed,闪烁速度,0-255,默认10.Image,中央图标SpokeCol... 查看详情

qt第三方圆形进度条-及其改进

Qt第三方圆形进度条的改进要实现一个圆形的进度条功能,在网上找到一个比较出名的第三方封装类:QRoundProgressBar,地址:sourceforge的QRoundProgressBar 功能封装的还是不错,提供了3种模式,线形、圆环、饼状。使用过程中发现... 查看详情

css3做圆形进度条

...天就重点讲解这个效果,首先,当有人说你能不能做一个圆形进度条效果出来时,如果是静态完整圆形进度条,那么就很简单了:.circleprogress{  width:160px;  height:160px;  border:20pxsolidred;  border-radius:50%;}... 查看详情

快速创建围绕图像的圆形进度条

】快速创建围绕图像的圆形进度条【英文标题】:Creatingacircularprogressbararoundanimageinswift【发布时间】:2016-07-2712:05:09【问题描述】:我正在尝试在图像周围创建一个圆形进度条,如下面的屏幕截图所示。到目前为止,我已经设法... 查看详情

纯css3实现圆形进度条动画

悄悄地,GIF格式的进度条已经越来越少,CSS进度条如雨后春笋般涌现。今天要介绍的这个CSS3进度条,效果和FlymeOS4上的加载动画一样。首先,来看下最终的效果:  查看详情

如何更改圆形进度条的颜色?

】如何更改圆形进度条的颜色?【英文标题】:Howtochangecolorincircularprogressbar?【发布时间】:2011-07-1707:03:54【问题描述】:我在Android上使用循环进度条。我想改变它的颜色。我正在使用"?android:attr/progressBarStyleLargeInverse"风格。那... 查看详情

javaswing圆形加载进度条(代码片段)

查看详情

android中的圆形进度条

】android中的圆形进度条【英文标题】:Circularprogressbarinandroid【发布时间】:2015-09-1318:26:05【问题描述】:我想实现一些圆形进度条,中间有一个按钮,就像在whatsapp中一样。有谁知道一个好的图书馆来实现这一点?我想要完成... 查看详情

如何重新加载圆形进度条

】如何重新加载圆形进度条【英文标题】:howtoreloadacircularprogressbar【发布时间】:2015-12-0921:59:58【问题描述】:我对画布以及如何创建圆形进度条进行了一些网络搜索,我得到了这个。我对在网上找到的代码片段进行了一些更... 查看详情

如何创建圆形样式进度条

】如何创建圆形样式进度条【英文标题】:HowtocreateaCircularStyleProgressBar【发布时间】:2011-06-1918:24:09【问题描述】:我需要帮助来实现这样的循环进度条:我应该如何通过增加Value属性来实现Circle来填充?【问题讨论】:相关链... 查看详情

如何在kivy中制作圆形进度条?

】如何在kivy中制作圆形进度条?【英文标题】:Howtomakecircularprogressbarinkivy?【发布时间】:2018-11-0516:04:11【问题描述】:我想用kivy和python制作一个简单的圆形进度条。我搜索了在线文档和GitHub曲目,但没有找到解释循环进度条... 查看详情

尝试在 Swift 中创建圆形进度条

】尝试在Swift中创建圆形进度条【英文标题】:TryingtocreatecircularprogressbarinSwift【发布时间】:2014-09-2722:45:58【问题描述】:我正在尝试在Swift中为我的应用程序添加一个圆形进度条,但是我无法找到任何Swift特定示例。因此,我... 查看详情

如何使用逆时针动画显示自定义圆形进度条?

】如何使用逆时针动画显示自定义圆形进度条?【英文标题】:HowtodisplaycustomcircularprogressbarwithAnti-Clockwiseanimation?【发布时间】:2014-07-0313:54:48【问题描述】:我正在使用与测验相关的应用程序。此处将显示自定义圆形进度条,... 查看详情

带有实心圆圈的自定义圆形进度条

】带有实心圆圈的自定义圆形进度条【英文标题】:CustomCircularProgressBarwithsolidcircles【发布时间】:2015-05-1505:52:21【问题描述】:我想要一个这样的圆形进度条我试过了,但它看起来不像圆形以及如何将动画与淡入淡出一起放置... 查看详情