Click here to Skip to main content
15,884,629 members
Articles / Desktop Programming / MFC
Article

MFC ActiveX Digital Clock

Rate me:
Please Sign up or sign in to vote.
4.75/5 (11 votes)
12 Dec 20021 min read 70.4K   2.4K   27   2
A digital clock ActiveX control.

Sample Image - clock.gif

Introduction

The main idea of this project is creation of a digital segment indicator. In the first version I had developed one segment indicator. After this I grouped some segment indicators and colon indicator and made the DigitalClock class. The next step was creation of an Active X control, based on DigitalClock class.

The base class is CDigit. This class draws segments of a digit with given brush, pen and width. The common function of this class is drawDigit(HDC dc, const int iDigit). This function gives the device context for painting and the number.

// draw digit, form 0 - 9

void CDigit::drawDigit( HDC dc, const int iDigit ) 
{ 
switch( iDigit )
{
case 0:

drawSegment(dc, UET, true ); 
drawSegment( dc, RUET, true ); 
drawSegment( dc, RDET, true ); 
drawSegment( dc, DET, true ); 
drawSegment( dc, LDET, true ); 
drawSegment( dc, LUET, true );
drawSegment( dc, MET, false );

break;
case 1:

drawSegment( dc, UET, false ); drawSegment( dc, RUET, true ); 
drawSegment( dc, RDET, true ); drawSegment( dc, DET, false ); 
drawSegment( dc, LDET, false ); drawSegment( dc, LUET, false );
drawSegment( dc, MET, false );

break;
.........In same manner draw segments for other digits...............

The digit enumeration:

enum DEType
{

UET = 0,
RUET = 1,
RDET = 2,
DET = 3,
LDET = 4,
LUET = 5,
MET = 6

};

The function drawSegment is used for painting of one segment:

// draw segment with selected color and pen
void CDigit::drawSegment( HDC dc, const DEType segType, 
                                const bool bState ) const 
{ 

HBRUSH oldBrush = (HBRUSH) SelectObject( dc, 
         (bState) ? m_activeBrush : m_inactiveBrush );
HPEN oldPen = (HPEN) SelectObject( dc, (bState) ? m_hActPen : m_hInactPen );
Polygon( dc, m_ptDG[ segType ], 6 );
SelectObject( dc, oldPen );
SelectObject( dc, oldBrush );

}

drawSegment selects the correspond brush and pen and calls Poligon API function, which draws a segment.

In this project, I had some difficulties with persisting of control's properties. After some experiments I did following:

void CDigitalClockCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);

// TODO: Call PX_ functions for each persistent custom property.
long lSOffset = GetLSOffset();
long lSWidth = GetLSWidth();
long lXOffset = GetLXOffset();
long lYOffset = GetLYOffset();
OLE_COLOR clrActive = GetClrActive();
OLE_COLOR clrBkgnd = GetClrBkgnd(); 
OLE_COLOR clrInactive = GetClrInactive();

PX_Long( pPX, _T("lSOffset"), lSOffset );
PX_Long( pPX, _T("lSWidth"), lSWidth );
PX_Long( pPX, _T("lXOffset"), lXOffset );
PX_Long( pPX, _T("lYOffset"), lYOffset );

PX_Color( pPX, _T("clrActive"), clrActive );
PX_Color( pPX, _T("clrBkgnd"), clrBkgnd );
PX_Color( pPX, _T("clrInactive"), clrInactive );

SetLSOffset( lSOffset );
SetLSWidth( lSWidth );
SetLXOffset( lXOffset );
SetLYOffset( lYOffset );
SetClrActive( clrActive );
SetClrBkgnd( clrBkgnd ); 
SetClrInactive( clrInactive );

}

First I get control's properties by invoking control's Getxxx functions, after that call macros and call Setxxx functions to set new properties' values.

Using the component

  1. Register component by using: regsvr32 <path\to installed\axtivex>
  2. Add component to projects
  3. The component icon will appear on controls toolbar
  4. Using drag-and-drop operation, put the component into dialogs.
  5. Set appropriate settings

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 here


Written By
Web Developer
Bulgaria Bulgaria
I am a senior developer in Melon Technologies Ltd.

Comments and Discussions

 
GeneralOleControl from dialog Pin
ant_kampo8-Aug-07 1:24
ant_kampo8-Aug-07 1:24 
GeneralDoPropExchange Pin
shiraztk13-Jun-07 4:08
shiraztk13-Jun-07 4:08 
Hi
Good work!

Cant you directly serialize your properties instead of calling member functions to set and get the values.

Is there anything particular?

Regards


The Best Religion is Science.
Once you understand it, you will know God.

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.