Click here to Skip to main content
Click here to Skip to main content

Transparent digital clock

By , 7 Apr 2003
 

Introduction

Clock is a simple program in C++ which shows the current local time in a transparent window. When time changes the window region changes as well using only the area necessary for displaying time. Instead of setting a timer to read time, the program uses a thread which posts a WM_CLOCKTIMETICK message each second.

Brief description

Program displays time in 4 LEDs as shown in the following image:

Each LED consists of 7 lines so we need an array of 4 * 7 = 28 elements to describe each line. Each element shows the starting position of the corresponding line and how this is going to be drawn, horizontally or vertically.

// structure that describes each element
typedef struct _MZDrawObj
{
    int xPos; // x-coordinate
    int yPos; // y-coordinate
    char cType; // 1: horizontaly, 2: verticaly
} MZDrawObj;

// array for describing each line
MZDrawObj m_obj[4][7];

We also need to represent each hour (0 to 23) and minute (0 to 59) with the respective lines that will be drawn. In order to achieve this I use an array of 24 elements (WORD m_objHour[24]) for the first 2 LEDs (hour LEDs) and another one of 60 elements (WORD m_objMin[60]) for the last 2 LEDs (minute LEDs). Each element describes exactly which lines will be drawn (7 bits for each LED).

           0                   0
         *****               *****
        *     *             *     *
      5 *     * 1         5 *     * 1
        *  6  *             *  6  *
         *****               *****
        *     *             *     *
      4 *     * 2         4 *     * 2
        *     *             *     *
         *****               *****
           3                   3  

       1st BYTE            2nd BYTE
  • Function InitDrawObj initializes array elements.
  • Function DrawObj draws the specified m_obj element.
  • Function DrawHour draws the specified m_objHour element (hour LEDs).
  • Function DrawMin draws the specified m_objMin element (minute LEDs).
  • Function DrawDot draws the separator dot between hour leds and minute leds. It is called twice.
  • Function DrawTime does the whole drawing.

Defining window region

Variable HRGN m_hWndRgn defines window region. Function DefineWndRegion reads local time and creates window region. When time changes window region changes as well.

Thread function TimeTickThread posts a message each second which calls OnTimeTick responsible for changing window region and drawing the window.

Using the code

Source code is free for anyone who might find it interested.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

manos_crete
Software Developer (Senior)
Greece Greece
Computer Engineer

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalupgrade to Visual C++ 2005memberEderAndres16-Oct-07 6:05 
GeneralRe: upgrade to Visual C++ 2005membermerano4-Jun-10 4:00 
GeneralFeatures Request/Suggestions..!memberana_v12327-Sep-06 17:17 
Hi Manolis Z.
 
Transparent clock is excellent piece of code. Thanks for sharing on codeproject.
 
I request/suggest few useful features.
That may make this clock more exciting.
 
Sorry...I do not have win32 expertise like you have otherwise I would have surely contributed many of these features.
 
1. It shows time only. Showing time+date will be useful. Why not to give option show/hide date in addition to time-only. (e.g. Date in user-specified format -> mm/dd/yyyy, dd/mm/yyyy or yyyy/mm/dd).
 
2. Tray icon right-click menu for options/setings, show/hide clock, show/hide time, date-format, select color, select big/small size, exit etc. (For adding such Tray Icon menu, I found non-MFC version SystemTray class from CodeProject article "Adding Icons to the System Tray" By Chris Maunder is very easy to use.)
 
3. Instead of two arrays of WORD (m_objHour[24] and m_objMin[60]), probably you might use only one array of WORD for single digits (0 to 9, i.e. m_objDigits[10] for representing digit 0 to 9) ? That can be used to display digits of time. And if you wish to display date then it can be used to display digits of date also.
 
Thanks again for sharing nice clock.
Ana
AnswerRe: Features Request/Suggestions..!membermanos_crete27-Sep-06 21:03 
GeneralRe: Features Request/Suggestions..!membermerano4-Jun-10 4:18 
QuestionHow you make it move by draggingmemberSohailB29-Jan-06 21:16 
AnswerRe: How you make it move by draggingmemberIce_2k16-Nov-06 4:19 
GeneralWithout TaskBar IconmemberWalther Pari19-Nov-05 6:04 
Question&=?memberDavidRobertson28-Sep-05 5:27 
AnswerRe: &=?memberIce_2k15-Nov-06 22:26 
GeneralBrilliant, ... simply Brilliant !!memberWREY6-Jun-03 12:10 
GeneralAnother .NET transparent clocksussAnonymous15-Apr-03 17:02 
GeneralA few little problemsmembermwilliamson8-Apr-03 13:11 
GeneralRe: A few little problemsmemberRobert W.9-Apr-03 1:03 
GeneralRe: A few little problemssussAnt59-Apr-03 1:39 
GeneralRe: A few little problemsmembere-DJ9-Jul-03 2:13 
GeneralRe: A few little problemsmembermanos_crete9-Apr-03 7:58 
GeneralKalo!memberKastellanos Nikos7-Apr-03 23:28 
QuestionWhy don't you use WM_TIMER?memberRené Greiner7-Apr-03 20:31 
AnswerRe: Why don't you use WM_TIMER?memberslawek8-Apr-03 10:02 
GeneralRe: Why don't you use WM_TIMER?memberTommek28-Jun-03 2:58 
Generalvery nice, but works one hour too erarlymemberDieter Hammer7-Apr-03 20:14 
GeneralRe: very nice, but works one hour too erarlymemberBrad Bruce8-Apr-03 2:35 
GeneralRe: very nice, but works one hour too erarlymemberEnderJSC17-Aug-05 17:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 8 Apr 2003
Article Copyright 2003 by manos_crete
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid