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

MFC ActiveX Digital Clock

, 12 Dec 2002
Rate this:
Please Sign up or sign in to vote.
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

About the Author

Nikolai Serdiuk
Web Developer
Bulgaria Bulgaria
I am a senior developer in Melon Technologies Ltd.
 

Comments and Discussions

 
GeneralOleControl from dialog Pinmemberant_kampo8-Aug-07 1:24 
GeneralDoPropExchange PinmemberZainu13-Jun-07 4:08 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 13 Dec 2002
Article Copyright 2002 by Nikolai Serdiuk
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid