The Clock - VB-Script


' Visual Basic Scripting Example 04
'
' "The clock"
'


DrawClock

Sub DrawClock()

 Dim imageobj
 Dim imageobj2
 Dim imageobj3
 Dim imageobj4

 Dim date
 Dim hours
 Dim minutes
 Dim seconds

 Dim xCenter
 Dim yCenter
 Dim path

 ' Fix path
 path = Server.MapPath("..\..\images") & "\"

 ' Since the clock consists of four images, we will create
 ' four image objects.

 Set imageobj = Server.CreateObject("W3Image.Image")
 Set imageobj2 = Server.CreateObject("W3Image.Image")
 Set imageobj3 = Server.CreateObject("W3Image.Image")
 Set imageobj4 = Server.CreateObject("W3Image.Image")

 ' Set the backgroundcolours to black.
 ' We will use the black colour as transparent when blitting.

 imageobj.BkColor = &H00000000&
 imageobj2.BkColor = &H00000000&
 imageobj3.BkColor = &H00000000&
 imageobj4.BkColor = &H00000000&

 ' Load the four bitmaps.
 If (imageobj.LoadImage(path & "background.bmp") = False ) then
  ' Do your error handling here...
  DisplayError "Error when loading image 'background.bmp'."
  ' Jump out - otherwise the code will continue
  Exit Sub
 End If

 If (imageobj2.LoadImage(path & "hour.bmp") = False ) then
  ' Do your error handling here...
  DisplayError "Error when loading image 'hour.bmp'."
  ' Jump out - otherwise the code will continue
  Exit Sub
 End If


 If (imageobj3.LoadImage(path & "minute.bmp") = False ) then
  ' Do your error handling here...
  DisplayError "Error when loading image 'minute.bmp'."
  ' Jump out - otherwise the code will continue
  Exit Sub
 End If


 If (imageobj4.LoadImage(path & "second.bmp") = False ) then
  ' Do your error handling here...
  DisplayError "Error when loading image 'second.bmp'."
  ' Jump out - otherwise the code will continue
  Exit Sub
 End If

 ' Get the current time.
 date = Now
 hours = Hour(date)
 minutes = Minute(date)
 seconds = Second(date)


 ' Add the seconds
 xCenter = imageobj4.Width / 2
 yCenter = imageobj4.Height / 2

 seconds = seconds * 6

 imageobj4.Rotate CDbl(seconds), xCenter, yCenter
 imageobj4.StretchBltExt imageobj, 0, 0, imageobj.Width, imageobj.Height, 0, 0, imageobj.Width, imageobj.Height, "transparent"


 ' Add the minutes
 xCenter = imageobj3.Width / 2
 yCenter = imageobj3.Height / 2

 minutes = minutes * 6

 imageobj3.Rotate CDbl(minutes), xCenter, yCenter
 imageobj3.StretchBltExt imageobj, 0, 0, imageobj.Width, imageobj.Height, 0, 0, imageobj.Width, imageobj.Height, "transparent"


 ' Add the hours
 xCenter = imageobj2.Width / 2
 yCenter = imageobj2.Height / 2

 If(hours >= 12) then
  hours = hours - 12
 End If

 hours = hours * 30
 hours = hours + (minutes / 12)

 imageobj2.Rotate CDbl(hours), xCenter, yCenter
 imageobj2.StretchBltExt imageobj, 0, 0, imageobj.Width, imageobj.Height, 0, 0, imageobj.Width, imageobj.Height, "transparent"

 ' Stream imageobj as an JPG image.
 If (imageobj.StreamImage(Response, "JPG", 24) = False ) then
  ' Do your error handling here...
  DisplayError "Error when streaming the image."
 End If

End Sub

' Example of a error handler - Displaying the error as a image
Sub DisplayError(msgcode)

 ' Create an error image
 Dim errorimage
 Dim fontobj
 Dim width
 Dim height

 Set errorimage = Server.CreateObject("W3Image.Image")
 errorimage.CreateEmptySurface 1,1

 ' Create and select the font
 Set fontobj = errorimage.CreateFont("Tahoma",24,0,"normal",0,&H00000000&,False,False,True)
 errorimage.SetFont fontobj

 ' Get size of the text
 width = errorimage.GetTextWidth(msgcode)
 height = errorimage.GetTextHeight(msgcode)

 ' Create a surface as large as the error message
 errorimage.CreateEmptySurface width,height

 ' Select the font again (font is deselected when creating a new surface)
 errorimage.SetFont fontobj

 ' Write out error message
 errorimage.DrawText msgcode,0,0

 ' Stream the image
 errorimage.StreamImage Response, "JPG", 24

End Sub