Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleMake a windowless clock control with a transparent background
Keywordsclock, windowless, analog, transparent
CategoriesActiveX, Controls, Multimedia, Graphics
 
[Note: Double click the .vbg file in VB6 to open the project group.]

Set the control's Windowless property to True and its BackStyle property to Transparent. Add these constituent controls:

TypeNamePurpose
Shape (Oval)shpClockFaceElliptical clock face
LinelinHourHandHour hand
LinelinMinuteHandMinute hand
LinelinSecondHandSecond hand
LinelinTic(0)Minute tic marks
TimertmrTickUpdate the clock

When the control initializes, load 58 other linTic controls in a control array.

 
Private Sub UserControl_Initialize()
Dim i As Integer

    ' Load tic mark Line controls.
    For i = 1 To 59
        Load linTic(i)
        linTic(i).Visible = True
    Next i
End Sub
 
When the control resizes, position shpClockFace to fill the control. Position the linTic controls around the face of the clock. Position one end of the hour, minute, and second hands at the center of the clock.
 
' Position the clock face and tic marks. Put one end
' of the hands at the center of the clock.
Private Sub UserControl_Resize()
Const TIC_R1 = 0.95
Const TIC_R2 = 0.9
Dim i As Integer
Dim tic_r As Single
Dim theta As Single
Dim dtheta As Single
Dim cos_theta As Single
Dim sin_theta As Single

    ' Make the clock face fit the control.
    shpClockFace.Move 0, 0, ScaleWidth, ScaleHeight

    ' Find the center of the clock.
    m_Cx = ScaleWidth / 2
    m_Cy = ScaleHeight / 2

    ' Move one end of the hands to the center.
    linHourHand.X1 = m_Cx
    linHourHand.Y1 = m_Cy
    linMinuteHand.X1 = m_Cx
    linMinuteHand.Y1 = m_Cy
    linSecondHand.X1 = m_Cx
    linSecondHand.Y1 = m_Cy

    ' Position the tic marks.
    theta = PI / 2
    dtheta = 2 * PI / 60
    For i = 0 To 59
        ' Make every 5th tic mark longer.
        If i Mod 5 = 0 Then
            tic_r = TIC_R2
        Else
            tic_r = TIC_R1
        End If

        ' Position the tic mark.
        With linTic(i)
            .X1 = m_Cx * (1 + Cos(theta))
            .Y1 = m_Cx * (1 + Sin(theta))
            .X2 = m_Cx * (1 + tic_r * Cos(theta))
            .Y2 = m_Cx * (1 + tic_r * Sin(theta))
        End With
        theta = theta + dtheta
    Next i

    ' Show the time.
    PositionHands
End Sub
 
When the timer fires, position the other ends of the hour, minute, and second hands to show the current time.
 
Private Sub tmrTick_Timer()
Static last_time As Date

    If last_time = Now Then Exit Sub

    last_time = Now
    PositionHands
End Sub

' Position the constituent Line controls
' to show the current time.
Private Sub PositionHands()
Dim X As Single
Dim Y As Single
Dim theta As Single

    ' Position the hour hand.
    theta = PI / 2 - Hour(Now) * (PI * 2 / 12)
    linHourHand.X2 = m_Cx * (1 + m_HourHandLength * _
        Cos(theta))
    linHourHand.Y2 = m_Cy * (1 - m_HourHandLength * _
        Sin(theta))

    ' Position the minute hand.
    theta = PI / 2 - Minute(Now) * (PI * 2 / 60)
    linMinuteHand.X2 = m_Cx * (1 + m_MinuteHandLength * _
        Cos(theta))
    linMinuteHand.Y2 = m_Cy * (1 - m_MinuteHandLength * _
        Sin(theta))

    ' Position the second hand.
    theta = PI / 2 - Second(Now) * (PI * 2 / 60)
    linSecondHand.X2 = m_Cx * (1 + m_SecondHandLength * _
        Cos(theta))
    linSecondHand.Y2 = m_Cy * (1 - m_SecondHandLength * _
        Sin(theta))
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated