vb.net 自定义progressbar
生活随笔
收集整理的這篇文章主要介紹了
vb.net 自定义progressbar
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先上效果圖:
背景色,前景色,字體,字號,字色,字的對齊方式都可以設置。
開始寫代碼:
窗體上加一個timer控件,目的是讓進度條值不斷增加。timer 的 interval 設置為100,并且啟用.
全部窗體代碼如下:
Public Class Form1Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.TickIf MyProgressBar1.Value < 100 ThenMyProgressBar1.Value += 1End IfEnd Sub End ClassPublic Class MyProgressBarInherits ProgressBarPublic Property CharColor As ColorPublic Property CharTop As Single = 0Public Property CharFont As FontPublic Property CharPositonType As PositionTypePublic Property ShowPercent As BooleanPublic Enum PositionTypeleft = 0middle = 1right = 2auto = 3auto2 = 4End EnumPublic Sub New()CharFont = New Font("candara", 14, FontStyle.Regular)CharPositonType = PositionType.autoCharColor = Color.WhiteBackColor = Color.MediumPurpleForeColor = Color.SlateBlueMe.SetStyle(ControlStyles.UserPaint, True)End SubProtected Overrides Sub OnPaint(e As PaintEventArgs)Dim percentage As UInt16, x As Int16percentage = CType((Value * 100) / Maximum, UInt16)Dim percent_str As String = percentage & IIf(ShowPercent, "%", "")Dim Rect As Rectangle = e.ClipRectangleRect.Width = CType((Rect.Width * Value / Maximum), UInt16) - 4Rect.Height = Rect.Height - 4e.Graphics.FillRectangle(New SolidBrush(BackColor), 0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height)e.Graphics.FillRectangle(New SolidBrush(ForeColor), 2, 2, Rect.Width, Rect.Height)Dim myfont As New Font(CharFont.FontFamily, CharFont.Size, FontStyle.Regular)Dim size As Size = TextRenderer.MeasureText(percent_str, myfont)If CharPositonType = 4 ThenIf Rect.Width - size.Width / 2 > 0 ThenIf (Rect.Width < e.ClipRectangle.Width - size.Width / 2) Thenx = (Rect.Width - size.Width / 2)Elsex = e.ClipRectangle.Width - size.Width - 2End IfElsex = 2End IfElseIf (CharPositonType = 3) ThenIf (Rect.Width - size.Width > 0) Thenx = (Rect.Width - size.Width) / 2Elsex = 2End IfElseIf CharPositonType = 2 Thenx = e.ClipRectangle.Width - size.Width - 2ElseIf CharPositonType = 1 Thenx = (e.ClipRectangle.Width - size.Width) / 2ElseIf CharPositonType = 0 Thenx = 2End Ife.Graphics.DrawString(percent_str, New Font(CharFont.FontFamily, CharFont.Size, FontStyle.Regular), New SolidBrush(CharColor), New Point(x, CharTop))MyBase.OnPaint(e)End SubEnd Class代碼寫上以后(先把timer的代碼注釋掉。因為這時候還沒有自定義的控件),點運行,然后關閉窗體。這時候,在工具箱就能看見我們的 MyProgressBar了。拖到窗體上,timer的代碼取消注釋,再運行,就能看到效果了。
可以設置的屬性:
總結
以上是生活随笔為你收集整理的vb.net 自定义progressbar的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全程干货!人物设计是什么?如何设计出好看
- 下一篇: 安装IIS以及配置ASP.NET流程