?
Solved

make analog watch within VB2008

Posted on 2009-05-17
3
Medium Priority
?
522 Views
Last Modified: 2012-05-07
Salam

How can I make a analog watch within VB2008

Thanks
0
Comment
Question by:Ahmadal_najjar2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Expert Comment

by:uucknaaa
ID: 24407004
Hey ..

Here's one:

http://www.dreamincode.net/code/snippet225.htm

Check It Out!
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24407454
Here is a pared down version of a more complex clock I wrote.  This version simply displays the current time:
Imports System.Drawing.Drawing2D
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ac As New AnalogClock
        ac.Location = New Point(10, 10)
        Me.Controls.Add(ac)
    End Sub
 
End Class
 
Public Class AnalogClock
    Inherits UserControl
 
    Private Const PI As Single = 3.1415926535897931
 
    ' these two determine the clock position
    ' counterclockwise motion increases angle
    ' Valid values are 0 --> 359 (six degrees = one minute on clock)
    ' up = 0
    ' left = 90
    ' down = 180
    ' right = 270
 
    ' Time = 3:55
    Private hourHand As Single = 270
    Private minuteHand As Single = 30
    Private secondHand As Single
 
    ' these are the actual time values
    Private _curHour As Integer
    Private _curMinute As Integer
    Private _curSecond As Integer
 
    Public ReadOnly Property Hour() As Integer
        Get
            Return _curHour
        End Get
    End Property
    Public ReadOnly Property Minute() As Integer
        Get
            Return _curMinute
        End Get
    End Property
    Public ReadOnly Property Second() As Integer
        Get
            Return _curSecond
        End Get
    End Property
    Public ReadOnly Property Time() As TimeSpan
        Get
            Return New TimeSpan(Me.Hour, Me.Minute, IIf(Me.ShowSecondHand, Me.Second, 0))
        End Get
    End Property
 
    Private _ShowSecondHand As Boolean = True
    Public Property ShowSecondHand() As Boolean
        Get
            Return _ShowSecondHand
        End Get
        Set(ByVal value As Boolean)
            _ShowSecondHand = value
        End Set
    End Property
 
    Private _ShowCurrentTime As Boolean = True
    Private clockFace As Bitmap
    Private WithEvents BackgroundWorker1 As New System.ComponentModel.BackgroundWorker
 
    Private Sub AnalogClock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BackgroundWorker1.WorkerReportsProgress = True
        BackgroundWorker1.RunWorkerAsync()
    End Sub
 
    Public Sub SetClock(ByVal dt As DateTime)
        _curHour = IIf(dt.Hour = 0, 12, IIf(dt.Hour <= 12, dt.Hour, dt.Hour - 12))
        _curMinute = dt.Minute
        _curSecond = dt.Second
 
        hourHand = (-_curHour + 12) * 30
        minuteHand = -6 * (_curMinute - 60)
        secondHand = -6 * (_curSecond - 60)
 
        Me.Refresh()
    End Sub
 
    Private Function DrawClockFace() As Bitmap
        Dim bmp As New Bitmap(Me.Width, Me.Height)
        Using g As Graphics = Graphics.FromImage(bmp)
            Dim p As Point
            Dim a, s As Integer
            Dim cx, cy As Single
 
            ' Compute the center of the UserControl
            cx = Me.Width / 2
            cy = Me.Height / 2
 
            ' Clear the box
            g.Clear(Me.BackColor)
 
            ' Main Circle of the Clock
            Dim radius As Double = Me.Width * 0.8 / 2
            g.DrawEllipse(Pens.Black, New Rectangle(cx - radius, cy - radius, radius * 2, radius * 2))
 
            For a = 0 To 354 Step 6
                ' Minute Tick Marks
                p = GetPoint(cx, cy, a, Me.Width * 0.6 / 2)
                g.DrawRectangle(Pens.Blue, New Rectangle(p.X, p.Y, 1, 1))
 
                If a Mod 30 = 0 Then
                    ' Hour Tick Marks
                    p = GetPoint(cx, cy, a, Me.Width * 0.7 / 2)
                    g.DrawRectangle(Pens.Red, New Rectangle(p.X, p.Y, 1, 1))
 
                    ' Put the Hours (1 to 12) on the Clock Face
                    p = GetPoint(cx - 6, cy - 8, a, Me.Width * 0.9 / 2)
                    s = 12 - a / 30
                    If s = 0 Then
                        s = 12
                    End If
                    g.DrawString(s, Me.Font, Brushes.Black, p.X, p.Y)
                End If
            Next a
        End Using
 
        Return bmp
    End Function
 
    Private Sub AnalogClock_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If IsNothing(Me.BackgroundImage) Then
            Me.BackgroundImage = DrawClockFace()
        End If
 
        Dim p As Point
        Dim cx As Single = Me.Width / 2
        Dim cy As Single = Me.Height / 2
 
        Using pn As New Pen(Color.Red, 2)
            p = GetPoint(cx, cy, hourHand, Me.Width * 0.48 / 2)
            e.Graphics.DrawLine(pn, cx, cy, p.X, p.Y)
        End Using
 
        Using pn As New Pen(Color.Blue, 2)
            p = GetPoint(cx, cy, minuteHand, Me.Width * 0.58 / 2)
            e.Graphics.DrawLine(pn, cx, cy, p.X, p.Y)
        End Using
 
        If Me.ShowSecondHand Then
            p = GetPoint(cx, cy, secondHand, Me.Width * 0.48 / 2)
            e.Graphics.DrawLine(Pens.Black, cx, cy, p.X, p.Y)
        End If
 
        Dim rc As New Rectangle(cx, cy, 1, 1)
        rc.Inflate(3, 3)
        e.Graphics.FillEllipse(Brushes.Black, rc)
    End Sub
 
    Private Function GetDistance(ByVal Ax As Single, ByVal Ay As Single, ByVal Bx As Single, ByVal By As Single) As Single
        Return Math.Sqrt((Ax - Bx) ^ 2 + (Ay - By) ^ 2) ' Pythagoreans Theorem
    End Function
 
    Private Function GetPoint(ByVal cx As Single, ByVal cy As Single, ByVal angleInDegrees As Single, ByVal distance As Single) As Point
        ' angleInDegrees
        ' Valid values are 0 --> 359 (six degrees = one minute on clock)
        ' draw a line from the center of the circle:
        ' 0 = up
        ' 90 = left
        ' 180 = down
        ' 270 = right
 
        ' Offset and Invert Angle then Convert Degrees to Radians
        angleInDegrees = -(angleInDegrees + 90) * PI / 180
 
        ' Compute the point that is at the specified angle from horizontal and at the specifed distance
        Return New Point(cx + distance * Math.Cos(angleInDegrees), cy + distance * Math.Sin(angleInDegrees))
    End Function
 
    Private Function GetAngleInDegrees(ByVal cx As Single, ByVal cy As Single, ByVal X As Single, ByVal Y As Single) As Single
        ' draw a line from the center of the circle
        ' to the where the cursor is...
        ' If the line points:
        ' up = 0 degrees
        ' right = 90 degrees
        ' down = 180 degrees
        ' left = 270 degrees
 
        Dim dx As Single = X - cx
        Dim dy As Single = Y - cy
        Dim angle As Single
 
        If dx = 0 Then
            ' Straight up and down causes div by zero error
            angle = IIf(dy <= 0, 0, 180)
        Else
            ' Compute Angle in Radians
            angle = Math.Atan(dy / dx)
            ' Convert to Degrees
            angle = angle * (180 / PI)
 
            ' Make it a number from 0 to 360 degrees
            If X > cx Then
                angle = 90 + angle
            Else
                angle = 270 + angle
            End If
        End If
 
        Return angle
    End Function
 
    Private Sub AnalogClock_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
        If Me.Height <> Me.Width Then
            Me.Height = Me.Width
        End If
        clockFace = DrawClockFace()
        Me.BackgroundImage = clockFace
    End Sub
 
    Private Function GetHourAngle(ByVal hour As Integer) As Integer
        hour = IIf(hour = 0, 12, IIf(hour <= 12, hour, hour - 12))
        Return (-hour + 12) * 30
    End Function
 
    Private Function GetMinuteAngle(ByVal minute As Integer) As Integer
        Return -6 * (minute - 60)
    End Function
 
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        While True
            BackgroundWorker1.ReportProgress(0)
            System.Threading.Thread.Sleep(1000)
        End While
    End Sub
 
    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Me.SetClock(DateTime.Now)
    End Sub
 
End Class

Open in new window

AnalogClock-BareBonesCurrentTime.jpg
0
 

Author Closing Comment

by:Ahmadal_najjar2003
ID: 31582400
very good coding
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question