martin2k

Google
 
Web www.martin2k.co.uk

Clock


 

Home

Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here

Contact


 

Program

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:


vb_41_clock.zip
(9.3 KB)

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 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
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", "")

H1 = Mid(t, 1, 1)
H2 = Mid(t, 2, 1)
M1 = Mid(t, 3, 1)
M2 = Mid(t, 4, 1)
S1 = Mid(t, 5, 1)
S2 = Mid(t, 6, 1)

ClockSet Hour1, H1
ClockSet Hour2, H2
ClockSet Min1, M1
ClockSet Min2, M2
ClockSet Sec1, S1
ClockSet Sec2, S2
lblDate.Caption = Format(Date, "Long Date")

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
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.


Comments

From: Brentan
Date: Friday, October 19, 2007 at 10:09:22
Comments:

for some reason when i tried to create my own clock using your code the line:

Img.Picture = ImgNum.ListImages.Item(Number).Picture

always comes up as method or data member not found.
need help

 
Reply:

Have you named your ImageList control ImgNum?  Is the argument 'Img' present in your 'ClockSet' procedure?

Have a look at the source code to compare it to yours.

 
From: ahmad dukku
Date: Tuesday, September 11, 2007 at 13:28:18
Comments: its interesting the codes
 
From: Nurudeen
Date: Saturday, August 25, 2007 at 01:00:15
Comments: I love this stuff the code are important to me. What makes a programmer if he/she don't know how to write programmes or develop softward
 
From: dhondhoncraze
Date: Wednesday, August 1, 2007 at 04:53:23
Comments: 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!!!!!!!!!!!!!!!
 
From: nadeem arain
Date: Monday, July 30, 2007 at 15:03:36
Comments: this is a nice clock
+923003238138
 
From: AMEER
Date: Friday, June 8, 2007 at 13:03:10
Comments: You coding intuitiveness is so much helpful for all level of programmers. Thanks a lot. May you think of more such codes.
 
From: sanela
Date: Friday, June 1, 2007 at 12:21:22
Comments: 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
 
From: sachin nimkar
Date: Wednesday, May 2, 2007 at 08:10:46
Comments: your clock program is cooool iwant another also
 
From: phani
Date: Friday, April 6, 2007 at 15:26:31
Comments: clock program is very nice.
 
From: ardie
Date: Tuesday, April 3, 2007 at 03:32:26
Comments: hello... i like ur program its very nice....  ur the best programer ive ever scne... good luck ///
 
From: Umair
Date: Friday, March 23, 2007 at 11:50:39
Comments: 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

If t = "080001AM" Then Alarm = False

t = Replace(t, "PM", "")
...

 
From: Anil kumar
Date: Tuesday, January 9, 2007 at 12:21:11
Comments: 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)
 
From: syefriadi
Date: Friday, January 5, 2007 at 03:22:08
Comments: I am very glad to follow your website and your source code.
 
From: joginder kumar
Date: Monday, December 11, 2006 at 08:56:04
Comments: 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
 
From: Rehan Shah
Date: Thursday, November 30, 2006 at 17:22:32
Comments: 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.
 
From: Irudaya Raja
Date: Monday, November 13, 2006 at 14:59:48
Comments: Hai! I am an Indian. I am very glad to follow your website and your source code. This Clock theme is very useful for me.
 
From: Clydlanne
Date: Friday, September 29, 2006 at 07:40:51
Comments: Hi! I am a 3rd year BSCS student. Well, you have a nice program. Keep the good work. Your clock program is really perfect! Bye! Have a nice day!
 
From: arun <bindasaks2005@!gmail.com>
Date: Thursday, September 14, 2006 at 13:36:37
Comments: wonderful
 
From: Santosh Yadav
Date: Tuesday, August 29, 2006 at 07:49:56
Comments: Hi,
I m from India.It is really nice work .I want to be a VB Programmer too. Hope you can help me (i u wish).
Bye
 
From: omkar kanitkar
Date: Thursday, February 2, 2006 at 06:54:45
Comments: 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.
 
From: rafay ikram
Date: Saturday, January 28, 2006 at 18:47:01
Comments: i used these programs and i found them excellent
thanx for these programs
 
From: Pepe Rojas
Date: Tuesday, November 29, 2005 at 20:05:46
Comments: 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.

My program does use the Time function.

 
From: mostafa ahmadi
Date: Wednesday, July 6, 2005 at 04:43:16
Comments: thank you for every thing.i am from iran,and you can count on me for ever.you are a great programmer and i say congratulation to you.
bye

Please fill in the below form if you have any comments or additional information you want to add about the above information.  If you have any Visual Basic 6.0 questions, please visit the ForumAny questions sent using this form will be ignored.

* - Mandatory

*Name: E-mail:

*Comments:

*Please type the following code (your comment will not be accepted without it):


Martin Allen 1999 - 2008.  Last updated Sunday 04 May 2008 06:11:17 PM +0100.