Berikut adalah langkah-langkah membuat jam analog:
1. Buka Visual Basic 6.0 dengan project standar.exe
2. Buat 3 buah line dengan nama "LineSecond","LineMinute","LineHour"
3. Cari salah satu gambar jam,lalu buat component PictureBox 1 buah.
4. lalu masukkan gambar jam tadi.
5. Masukkan Code nya seperti berikut.
Option Explicit
Private Const pi As Double = 3.14159265358979
Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Sub MakeRoundObject(objObject As Object, Value As Long)
Static lngHeight, lngLong, lngReturn, lngWidth As Long
lngWidth = objObject.Width / Screen.TwipsPerPixelX
lngHeight = objObject.Height / Screen.TwipsPerPixelY
SetWindowRgn objObject.hWnd, CreateRoundRectRgn(0, 0, lngWidth, lngHeight, Value, Value), True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then
App.TaskVisible = False
Unload Me
End
End If
End Sub
Private Sub Form_Load()
Dim intX As Integer
Call MakeRoundObject(Picture2, 300)
Call tmrClock_Timer
For intX = 0 To 360 Step 6
If intX Mod 30 = 0 Then
Me.DrawWidth = 6
Me.PSet (1100 * Cos(intX * pi / 180) + Linesecond.X1, 1100 * Sin(intX * pi / 180) + Linesecond.Y1)
Else
Me.DrawWidth = 3
Me.PSet (1100 * Cos(intX * pi / 180) + Linesecond.X1, 1100 * Sin(intX * pi / 180) + Linesecond.Y1)
End If
Next intX
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage Me.hWnd, &HA1, 2, 0&
End Sub
Private Sub lblTime_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call Form_MouseDown(Button, Shift, X, Y)
End Sub
Private Sub tmrClock_Timer()
Dim dblSecond As Double, dblMinute As Double, dblHour As Double
dblSecond = Second(Now) * 6 - 90
dblMinute = (Minute(Now) + Second(Now) / 60) * 6 - 90
dblHour = (Hour(Now) + Minute(Now) / 60) * 30 - 90
Linesecond.X2 = 1000 * Cos(dblSecond * pi / 180) + Linesecond.X1
Linesecond.Y2 = 1000 * Sin(dblSecond * pi / 180) + Linesecond.Y1
LineMinute.X2 = 900 * Cos(dblMinute * pi / 180) + LineMinute.X1
LineMinute.Y2 = 900 * Sin(dblMinute * pi / 180) + LineMinute.Y1
Linehour.X2 = 700 * Cos(dblHour * pi / 180) + Linehour.X1
Linehour.Y2 = 700 * Sin(dblHour * pi / 180) + Linehour.Y1
End Sub
Private Const pi As Double = 3.14159265358979
Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Sub MakeRoundObject(objObject As Object, Value As Long)
Static lngHeight, lngLong, lngReturn, lngWidth As Long
lngWidth = objObject.Width / Screen.TwipsPerPixelX
lngHeight = objObject.Height / Screen.TwipsPerPixelY
SetWindowRgn objObject.hWnd, CreateRoundRectRgn(0, 0, lngWidth, lngHeight, Value, Value), True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then
App.TaskVisible = False
Unload Me
End
End If
End Sub
Private Sub Form_Load()
Dim intX As Integer
Call MakeRoundObject(Picture2, 300)
Call tmrClock_Timer
For intX = 0 To 360 Step 6
If intX Mod 30 = 0 Then
Me.DrawWidth = 6
Me.PSet (1100 * Cos(intX * pi / 180) + Linesecond.X1, 1100 * Sin(intX * pi / 180) + Linesecond.Y1)
Else
Me.DrawWidth = 3
Me.PSet (1100 * Cos(intX * pi / 180) + Linesecond.X1, 1100 * Sin(intX * pi / 180) + Linesecond.Y1)
End If
Next intX
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage Me.hWnd, &HA1, 2, 0&
End Sub
Private Sub lblTime_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call Form_MouseDown(Button, Shift, X, Y)
End Sub
Private Sub tmrClock_Timer()
Dim dblSecond As Double, dblMinute As Double, dblHour As Double
dblSecond = Second(Now) * 6 - 90
dblMinute = (Minute(Now) + Second(Now) / 60) * 6 - 90
dblHour = (Hour(Now) + Minute(Now) / 60) * 30 - 90
Linesecond.X2 = 1000 * Cos(dblSecond * pi / 180) + Linesecond.X1
Linesecond.Y2 = 1000 * Sin(dblSecond * pi / 180) + Linesecond.Y1
LineMinute.X2 = 900 * Cos(dblMinute * pi / 180) + LineMinute.X1
LineMinute.Y2 = 900 * Sin(dblMinute * pi / 180) + LineMinute.Y1
Linehour.X2 = 700 * Cos(dblHour * pi / 180) + Linehour.X1
Linehour.Y2 = 700 * Sin(dblHour * pi / 180) + Linehour.Y1
End Sub
6. Jalankan dengan menekan tombol F5.
Selesai.......
Selamat mencoba......
Download Source Codenya Disini
No comments:
Post a Comment