The Clock program displays the time using big numbers, which resemble those
from a digital clock in real life. You can download the source code from
here:
The compiled program can be downloaded from the Downloads
page. Here is how to make it:
This program uses the following ActiveX controls:
Microsoft Windows Common Controls 6.0 (SP6) (MSCOMCTL.OCX)
First, design a Form, which looks similar to this:
The Clock Form
The Form includes six Image controls (Hour1, Hour2, Min1, Min2, Sec1 and
Sec2), four Labels (the colons, one at the
bottom for the date (lblDate) and one on the right of the Form to display 'AM' or 'PM'
(lblAMPM)), a
Timer (timTime - not displayed above) and an ImageList (ImgNum). The ImageList contains
numbers from 1 - 9 and then 0 as images. You can use the ones, included in
the source code ZIP file or you can use your own e.g.:
ImageList Property Pages
To get the Form to be the topmost on the screen, the following API function
and constants need to be declared:
Private Declare Sub SetWindowPos Lib "User32"
(ByVal hWnd As Long,
ByVal hWndInsertAfter As
Long, ByVal X As
Long, ByVal Y As
Long, ByVal cx As
Long, ByVal cy As
Long, ByVal wFlags As
Long)
Const HWND_TOPMOST = -1
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
The following code needs to be put into the Form Load event. The centre
line sets the window as the topmost window:
Private Sub Form_Load()
lblDate.Caption = Time & " - " & Format(Date, "Long
Date")
'Set window as topmost
SetWindowPos hWnd, HWND_TOPMOST, Left / 15, Top / 15, Width / 15, Height /
15, SWP_NOACTIVATE Or SWP_SHOWWINDOW
ClockT.Interval = (1 - (Int(Timer) Mod 1))
* 1000
End Sub
Here is the code for the Timer:
Private Sub timTime_Timer()
On Error GoTo Eror
Dim t As String, H1
As Integer, H2 As
Integer, M1 As Integer, M2 As
Integer, S1 As Integer, S2 As Integer
t = Time
If Len(Time) = 10 Then
t = 0 & t
t = Replace(t, ":", "")
t = Replace(t, " ", "")
lblAMPM.Caption = Right(t, 2)
t = Replace(t, "PM", "")
t = Replace(t, "AM", "")
Exit Sub
Eror:
If Err = 13 Then MsgBox "This program will only work if you set 'Start, Settings, Control Panel, Regional Settings, Time, Time separator:' to ':' (colon).", 16, "Error"
Unload frmClock
End Sub
This code works by firstly creating a string (t) that the program can work
with easily e.g. "9:09:28 PM" would be converted into
"090928". Next, the H1, H2, M1, M2, S1 and S2 variables are
given values from this string by extracting numbers using the Mid
function. Afterwards, the ClockSet function is given each variable and puts
the number for it on the Form.
The code for the ClockSet function is as follows:
Sub ClockSet(Img As Image, Number
As Integer)
If Number = 0 Then Number = 10
Img.Picture = ImgNum.ListImages.Item(Number).Picture
End Sub
As the image for '0' is in the number 10 position in the ImageList, the
program has to be told to get the image for '0' from this position hence the
first line.
The end result should look like this:
The Clock Program
Additional Information by Martin Allen:
To display the time in a different time zone, just change the following code
in the timTime_Timer event:
t = Time
to:
t = Time - 12 / 24
The above example will subtract half a day, or 12 hours. The first
number can be set to the number of hours you want to add or subtract. E.g.
to subtract one hour, change the first number to 1. To add an hour, use
'+' instead of '-' and change the first number to 1.
Hi..! Very congratulations for your nice clock program and one request
for u is that can u help me for going in deep with vb 6.0 & vb.net as i
have to be a very good programmer in my life.. Thanks
hi, I'm a newbie in vb programming i like your programs.. And right
now i'm studying the way you code i need to learn vb6.0 for my projects
thanks for sharing your vb projects..
Hey your work is nice, but it is very simple, i can make that one in
just few minute; by the way i'm a programmer also; keep up the good work;
your great!!!!!!!!!!!!!!!
hello i write from bosnia and herzegovina i have school asigment i have to make some simple program in Visual Basic, i dont know have,i dont have any litherature,and i cant download any of this programs,i really like clock program it looks great, byyy sanela
i'm from england, gr8 work man, it can be even greater if u cud add alarm to it, in order to alarm the user about n e special event.
Reply:
All you would need to do is to test whether the current time matches
the time you want an event to occur e.g. you could try the following
code in the timTime_Timer() event. Make sure you declare a Boolean
variable called Alarm.
...
lblAMPM.Caption = Right(t, 2)
If Alarm = False Then
If t = "080000AM" Then
Alarm = True
Shell "C:\Program Files\Windows Media Player\wmplayer.exe
c:\music.mp3", vbNormalFocus
End If
End If
Hi,
this is anil...I am from Bangalore[India].I am doing my final
year computer science.
About ur clock Program,it is wonderfull. I am in need help.can u
help me. How to get quick knowlege in vb?
Regards
[Anil Kumar)
Hi I am indian !!. very congatulations for ur nice clock programm and one request for u is that can u help me for going in deep with vb 6.0 & vb.net as i have to be a very good programmer in my life.. Thanks
I m from Karachi.It is really nice work .I want to be a VB Programmer in future. I used these programs and i found them excellent. If u have any new project so email i need to learn.
i am indian, now in russia (for education). i have simpler codes for the same clock program. but still i need some help in creating other projects. so pls e mail me your e mail address(if you wish to help). thanks.
Hi, ¿do you speak spanish?, I'm from México. My english is not good, but I will try to use it.
My question is: Why you need APIs and, image control and OCX, if you can do it with a text box with a font like LCD o some similar. and calling the function TIME, is more easy and the file is more light. Later I will send you my version of clock to you put it on your web. If you can, send me the answer to my mail.
Pepe.
Reply:
The API function is used to keep
the window above others - it does not have anything to do with the clock
code.
I used image controls because I thought they would look better than
just using a font - one of the reasons was so that the parts of the
number that are off would still be displayed; like in an LED display.