thongkorn โพสต์ 2019-2-12 12:30:11

[VB6] การทำ ProgressBar ด้วย PictureBox Control

http://www.g2gnet.com/webboard/images/vb6/progressbar.png


โค้ดชุดนี้ก็ไม่มีอะไรมากมาย แต่แฝงนัยยะของการฝึกคิดฝึกทำ และการฝึกเขียนโปรแกรม ไม่ใช่การฝึกใช้โปรแกรม โดยการนำเอา PictureBox Control มาทำเป็น ProgressBar โดยไม่ต้องไปโหลด Microsoft Common Control เข้ามา ...

มาดูโค้ดกันเถอะ ...
' / --------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / MORE: http://www.g2gnet.com/webboard
' /
' / Purpose: Make simple ProgressBar.
' / Microsoft Visual Basic 6.0 (SP6)
' /
' / This is open source code under @CopyLeft by Thongkorn/Common Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------
Option Explicit
' ไว้ทำเป็น Progress Bar นับค่า 1 - 100
Dim CurrentPercent As Byte

Public Function UpdateProgress(pb As Control, ByVal Percent)
    Dim Num As String
    If Not pb.AutoRedraw Then pb.AutoRedraw = -1
    pb.Cls
    pb.ScaleWidth = 100
    pb.DrawMode = 10
    Num = Format$(Percent, "###") + "%"
    pb.CurrentX = 50 - pb.TextWidth(Num) \ 2
    pb.CurrentY = (pb.ScaleHeight - pb.TextHeight(Num)) \ 2
    pb.Print Num
    pb.Line (0, 0)-(Percent, pb.ScaleHeight), , BF
    pb.Refresh
End Function

Private Sub cmdExit_Click()
    Unload Me
End Sub

Private Sub cmdProcess_Click()
    Timer1.Enabled = True
End Sub

Private Sub Form_Load()
    CurrentPercent = 0
    '// Timer1
    Timer1.Enabled = False
    Timer1.Interval = 100   '<-- 100 millisecond.
End Sub

Private Sub Timer1_Timer()
    CurrentPercent = CurrentPercent + 10 ' เพิ่มขึ้นทีละ 10 ไปเรื่อยๆ จนถึง 100 ก็จบการทำงาน (Timer.Enabled = False)
    If CurrentPercent <= 100 Then
      UpdateProgress picStatus, CurrentPercent
    Else
      MsgBox "สิ้นสุดกระบวนการทำงาน.", vbInformation + vbSystemModal, "ProgressBar"
      Timer1.Enabled = False
      CurrentPercent = 0
      picStatus.Cls
      'Unload Me
    End If
End Sub


ดาวน์โหลดโค้ดต้นฉบับ VB6 ได้ที่นี่ ...


หน้า: [1]
ดูในรูปแบบกติ: [VB6] การทำ ProgressBar ด้วย PictureBox Control