Click here to Skip to main content
13,086,120 members (83,468 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 6 Jan 2003

Analog Clock

, 6 Jan 2003
Rate this:
Please Sign up or sign in to vote.
Add an analog clock to your project (A tutorial for creating CStatic derived controls)
<!-- Article image -->

Analog Clock


<!-- Add the rest of your HTML here -->

This article describes how you can add a static control to show an analog clock. This control is derived from CStatic. First of all, add Clock.h and Clock.cpp files to your project. Select Resource tab from Workspace window, and select your dialog that you want to add an analog clock. Select Static Control from Control Toolbox and draw it on dialog (Figure 1). Change it's ID from IDC_STATIC to IDC_CLOCK.

Add Static Control on your dialog
Figure 1 - Add Static Control to your dialog.

Now it's time to add a member variable to your dialog class. Call Class Wizard to do it for you. Figure 2 and 3 show you it. In this case we add a member variable m_Clock with type CStatic.

Add member variable to your dialog class
Figure 2 - Add member variable to your dialog class.

Class Wizard
Figure 3 - Class Wizard

Ok, open your dialog class header file, add this line on top of your class definition:

#include "Clock.h"

Then, change CStatic m_Clock; to CClock m_Clock;. Now your dialog definition, looks like this:

// AnalogClockDlg.h : header file

#if !defined(AFX_ANALOGCLOCKDLG_H__D5D048D5_079A_
#define AFX_ANALOGCLOCKDLG_H__D5D048D5_079A_40BD_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "Clock.h"
// CAnalogClockDlg dialog

class CAnalogClockDlg : public CDialog
// Construction
  CAnalogClockDlg(CWnd* pParent = NULL);  // standard constructor

// Dialog Data
  CClock  m_Clock;    //We change it from CStatic m_Clock; 
                            //to CClock m_Clock;

  // ClassWizard generated virtual function overrides
  virtual void DoDataExchange(CDataExchange* pDX);  
           // DDX/DDV support

// Implementation
  HICON m_hIcon;

  // Generated message map functions
  virtual BOOL OnInitDialog();
  afx_msg void OnPaint();
  afx_msg HCURSOR OnQueryDragIcon();

// Microsoft Visual C++ will insert additional declarations 
// immediately before the previous line.

#endif // !defined(AFX_ANALOGCLOCKDLG_H__D5D048D5_079A_40BD_

Member Functions

Class CClock has only 2 member functions. One for setting clock hand color and other to get clock hand color as below:

void SetHandColor(COLORREF color);


color=RGB(0, 0, 255);    //Blue
COLORREF GetHandColor();

color=m_Clock.GetHandColor();    //Get color

Now, every thing is ready!


Part of code is obtained from Jeff Prosise Book (Programming with MFC - MSPress). Jeff's code is drawing an analog clock in CView derived class and I changed it to CStatic derived class. This code has another difference, if you change time (Control Panel, Date/Time) Jeff's code can not recognize new time. I change his code to recognize new time on the fly!


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 here


About the Author

A. Riazi
CEO Misbah3Com
Iran (Islamic Republic of) Iran (Islamic Republic of)
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acqusition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competetion, my articles are:

You can see list of my articles, by clicking here

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 19:48
membermanoj kumar choubey26-Feb-12 19:48 
Questionwho can tell me? Pin
zhaohaifeng14-Aug-05 21:45
memberzhaohaifeng14-Aug-05 21:45 
Generalvery usefull Pin
mohsen.n25-Oct-04 1:07
sussmohsen.n25-Oct-04 1:07 
GeneralPropise -> Prosise Pin
stefan b7-Jan-03 20:41
memberstefan b7-Jan-03 20:41 
GeneralRe: Propise -> Prosise Pin
Yinghsuan Huang8-Jan-03 4:57
memberYinghsuan Huang8-Jan-03 4:57 
GeneralCDateTimeCtrl focus problem Pin
cool_jay7-Jan-03 20:04
membercool_jay7-Jan-03 20:04 
GeneralSo ... Pin
Anonymous7-Jan-03 19:29
memberAnonymous7-Jan-03 19:29 
QuestionDid you know? Pin
A. Riazi7-Jan-03 17:20
memberA. Riazi7-Jan-03 17:20 
AnswerRe: Did you know? Pin
Roger Stewart7-Jan-03 18:47
memberRoger Stewart7-Jan-03 18:47 
GeneralRe: Did you know? Pin
A. Riazi7-Jan-03 19:25
memberA. Riazi7-Jan-03 19:25 
GeneralRe: Did you know? Pin
Yinghsuan Huang8-Jan-03 5:06
memberYinghsuan Huang8-Jan-03 5:06 
GeneralUgly as all heck Pin
Marc Clifton7-Jan-03 11:45
memberMarc Clifton7-Jan-03 11:45 
GeneralWhy your code is same as Jeff Propise... Pin
Yinghsuan Huang7-Jan-03 8:25
memberYinghsuan Huang7-Jan-03 8:25 
GeneralRe: Why your code is same as Jeff Propise... Pin
Yinghsuan Huang7-Jan-03 8:33
memberYinghsuan Huang7-Jan-03 8:33 
GeneralRe: Why your code is same as Jeff Propise... Pin
Maximilien7-Jan-03 8:43
memberMaximilien7-Jan-03 8:43 
GeneralRe: Why your code is same as Jeff Propise... Pin
Roger Stewart7-Jan-03 10:15
memberRoger Stewart7-Jan-03 10:15 
GeneralRe: Why your code is same as Jeff Propise... Pin
Ted Ferenc7-Jan-03 10:29
memberTed Ferenc7-Jan-03 10:29 
GeneralRe: Why your code is same as Jeff Propise... Pin
Ted Ferenc7-Jan-03 10:31
memberTed Ferenc7-Jan-03 10:31 
GeneralRe: Why your code is same as Jeff Propise... Pin
Yinghsuan Huang7-Jan-03 11:50
memberYinghsuan Huang7-Jan-03 11:50 
GeneralRe: Why your code is same as Jeff Propise... Pin
Ted Ferenc7-Jan-03 12:15
memberTed Ferenc7-Jan-03 12:15 
GeneralRe: Why your code is same as Jeff Propise... Pin
Yinghsuan Huang7-Jan-03 13:14
memberYinghsuan Huang7-Jan-03 13:14 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 7 Jan 2003
Article Copyright 2003 by A. Riazi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid