Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I make an analog clock dial?

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
AU
Anyone know of code to create moving hands for a large dial clock?
It would be nice to use a nice graphic of a hand rather than draw a line but how do you rotate it and reposition it to follow the time circle like the old windows 3.1 clock?

I couldnt find references in this forum's archives but then I dont know what other people would call it!

Ted
 
IMHO you might be better off searching for an OCX or some kind of exe that does that sort of thing for you.
 
API's

bitblt
plgblt
setworldtransform

the list goes on...

strongm wrote some cool rotatie code in one of my threads a few weeks back if i find it ill post it!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
here it is

thread222-535523

enjoy

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
No need for all that heavyweight stuff if you just want to draw an analogue hand, as it is just a bit of trignometry. For this example you'll need a picturebox and a command button (and most of the code is just a bit of setup):
[tt]
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
Dim cx As Double
Dim cy As Double
Dim r As Double
Dim pi As Double
Dim rlp As Double
Dim dSeconds As Long

pi = 4 * Atn(1)
Picture1.ScaleMode = vbPixels
' Make destination square
If Picture1.Width > Picture1.Height Then
Picture1.Height = Picture1.Width
Else
Picture1.Width = Picture1.Height
End If

Picture1.AutoRedraw = True

cx = Picture1.ScaleWidth / 2
cy = Picture1.ScaleHeight / 2
r = (Picture1.ScaleWidth / 2) - 4
Picture1.Circle (cx, cy), r
Picture1.AutoRedraw = False
Picture1.DrawWidth = 2

' This is the analogue handsweep bit
For dSeconds = 0 To 360 Step 6
Sleep 1000
Picture1.Cls
rlp = (180 - dSeconds) * pi / 180
Picture1.Line (cx, cy)-Step(r * Sin(rlp), r * Cos(rlp)), RGB(0, 0, 0)
Next

End Sub
 
And here is a similar code for whole clock face if you are interested.
All you need is a timer and a picture box of appropriate size on the form.
___
Dim r As Integer
Private Sub Form_Load()
Timer1.Interval = 900
With Picture1
r = .ScaleWidth
If r > .ScaleHeight Then r = .ScaleHeight
Picture1.Scale (-.ScaleWidth, .ScaleHeight)-(.ScaleWidth, -.ScaleHeight)
End With
End Sub

Private Sub Timer1_Timer()
Dim t As Date, h As Single, m As Single, s As Single
Const pi = 3.1415927

'Get h:m:s
t = Time
h = Hour(t) Mod 12
m = Minute(t)
s = Second(t)

'Convert to angles
h = pi / 2 - h / 12 * 2 * pi
m = pi / 2 - m / 60 * 2 * pi
s = pi / 2 - s / 60 * 2 * pi

'Draw hands
Picture1.Cls
Picture1.Line (0, 0)-(r * 0.9 * Cos(s), r * 0.9 * Sin(s)), vbBlue
Picture1.Line (0, 0)-(r * 0.7 * Cos(m), r * 0.7 * Sin(m)), vbGreen
Picture1.Line (0, 0)-(r * 0.5 * Cos(h), r * 0.5 * Sin(h)), vbRed
End Sub
___
However if you want to display rotating graphics instead of lines for drawing hands than you need to visit the thread pointed by ADoozer.
 
And then, eventually, you end up with something like:

mclock2.png


;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top