vb.net Visual Basic 圆形进度条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26928523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:28:34  来源:igfitidea点击:

Visual basic circular progress bar

.netvb.netuser-interfaceprogress-bar

提问by faresabb2

I'm trying to make a software with good UI , but i'm not professional in VB ... How can i make a circular progress bar ?

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

for Example

例如

enter image description here

在此处输入图片说明

回答by Matt Wilko

How about just drawing your own using GDI+.

使用 GDI+ 绘制自己的图像如何。

You can convert this to your own usercontrol later but this will get you started. It should be fairly self explanatory:

您可以稍后将其转换为您自己的用户控件,但这将使您开始。它应该是相当不言自明的:

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

Output

输出

enter image description here

在此处输入图片说明

回答by BR1COP

Here's an example of how to update the progress circular bar just when you need, with no flickering due to refresh.

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

Based on Matt's Code

基于马特的代码

Simply copy the code in your form Paint Event, properly changing the rectangle size and location to host the circle in your form. Percent is a global variable, when it changes, you can call me.refresh() method to trigger the repaint!

只需复制表单 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

回答by user2155059

@faresabb2 In the Form2.Paint sub at the very start of the code, put

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

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