Introduction
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
.
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
.
Figure 2 - Add member variable to your dialog class.
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:
#if !defined(AFX_ANALOGCLOCKDLG_H__D5D048D5_079A_
40BD_86A0_32A26253D2E5__INCLUDED_)
#define AFX_ANALOGCLOCKDLG_H__D5D048D5_079A_40BD_
86A0_32A26253D2E5__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Clock.h"
class CAnalogClockDlg : public CDialog
{
public:
CAnalogClockDlg(CWnd* pParent = NULL);
enum { IDD = IDD_ANALOGCLOCK_DIALOG };
CClock m_Clock;
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
};
#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);
e.g.:
COLORREF color;
color=RGB(0, 0, 255);
m_Clock.SetHandColor(color);
COLORREF GetHandColor();
e.g.:
COLORREF color;
color=m_Clock.GetHandColor();
Now, every thing is ready!
Note
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!
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 Acquisition 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 competition, my articles are:
You can see list of my articles, by clicking here