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 transparent moveable analog clock in VB .NET
DescriptionThis example shows how to make a transparent moveable analog clock in VB .NET. The user can click and drag on the center of the clock to move it.
Keywordsanalog, clock, time, position, lower right, rotated text, transparent, move, move form
CategoriesMultimedia, Graphics, Utilities, VB.NET
 
See these examples for the basics:

When this version draws the clock's face, it makes the form's and face's background colors both purple. When it finishes drawing the clock face, it sets the form's TransparencyKey color to purple so the purple areas are not drawn.

This version of the program draws the clock's hands a bit thicker than the previous versions because lines that are one pixel wide tend to disappear in the desktop clutter behind the transparent form.

 
' Draw the clock's face without hands.
Private Sub DrawFace()
    ' Make a Bitmap to hold the clock face.
    m_Face = New Bitmap(Me.ClientRectangle.Width, _
        Me.ClientRectangle.Height)
    Dim gr As Graphics = Graphics.FromImage(m_Face)

    ' Use a purple background. This will later be
    ' transparent.
    gr.Clear(Color.Purple)

    ' Fill the clock face with CornflowerBlue.
    Dim inner_rect As New Rectangle(0, 0, _
        Me.ClientRectangle.Width - 1, _
        Me.ClientRectangle.Height - 1)
    gr.FillEllipse(Brushes.Purple, inner_rect)

    ' Draw the clock face.
    gr.DrawEllipse(Pens.Blue, inner_rect)

    ' Draw the tic marks and numerals.
    Dim cx As Integer = (Me.ClientRectangle.Width - 1) \ 2
    Dim cy As Integer = (Me.ClientRectangle.Height - 1) \ 2
    Dim dtheta As Double = PI / 30
    Dim theta As Double = -10 * dtheta
    Dim x1, y1, x2, y2 As Double
    Dim txt As String
    Dim tick_pen As New Pen(Color.Blue, 2)
    For i As Integer = 0 To 59
        ' Draw the tic marks.
        x1 = cx + cx * Cos(theta)
        y1 = cy + cy * Sin(theta)
        If i Mod 5 = 0 Then
            ' Label the digit.
            txt = (i \ 5 + 1).ToString()

            ' Find the point lined up along the tic mark.
            x2 = cx + (cx - 1) * Cos(theta) * 0.8
            y2 = cy + (cy - 1) * Sin(theta) * 0.8

            ' Create a rotated font.
            DrawRotatedText(gr, txt, _
                CSng(360 * (i + 5) / 60), _
                x2, y2)

            x2 = cx + cx * Cos(theta) * 0.9
            y2 = cy + cy * Sin(theta) * 0.9
        Else
            x2 = cx + cx * Cos(theta) * 0.95
            y2 = cy + cy * Sin(theta) * 0.95
        End If
        gr.DrawLine(tick_pen, CSng(x1), CSng(y1), CSng(x2), _
            CSng(y2))
        theta += dtheta
    Next i
    tick_pen.Dispose()

    ' Display the clock face on the form's background.
    Me.BackgroundImage = m_Face

    ' Set TransparencyKey so the purple background is
    ' transparent.
    Me.TransparencyKey = Color.Purple
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated