Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4635737
  • 博文数量: 671
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 7310
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-14 09:56
文章分类

全部博文(671)

文章存档

2011年(1)

2010年(2)

2009年(24)

2008年(271)

2007年(319)

2006年(54)

我的朋友

分类: C/C++

2008-09-14 21:03:09

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found

阅读(840) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~