65.9K
CodeProject is changing. Read more.
Home

BinClock

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (9 votes)

Jun 21, 2005

viewsIcon

37021

downloadIcon

495

A binary clock with options.

Sample Image

Sample Image

Introduction

This project shows how to convert a decimal number to binary using the subtraction method. It has options to change rectangle colors, hide or display a decimal clock, and to play a clock ticking sound.

Using the code

To sample the system clock in a timer, I have set the timer to 250ms (4 times per second) and compare the current second to a saved value.

void CBinClockDlg::OnTimer(UINT nIDEvent) 
{
    // TODO: Add your message handler code here and/or call default

    CTime time = CTime::GetCurrentTime();
    int nSecond = time.GetSecond();
    if(tTime != nSecond)
        Display();
    tTime = nSecond;

    CDialog::OnTimer(nIDEvent);
}

To convert the decimal value of hours, minutes, and seconds to binary, test if the decimal value is greater than or equal to the binary bit values 32, 16, 8, 4, 2, 1. If so, set the corresponding bit and subtract the bit value from the decimal value.

...
    // Convert decimal to binary

    if (nHour >= 16)
    {
        DisplayHour(5);
        nHour -= 16;
    }
...
    case 5:
        pDC = m_hour5.GetDC();
        m_hour5.GetClientRect(&rect);
        pDC->FillRect(&rect, &m_brush);
        ReleaseDC(pDC);
        break;
...

The ticking sound uses: PlaySound("SOUNDTICK", hInst, SND_RESOURCE | SND_ASYNC); which is defined in Mmsystem.h and included in Winmm.lib. SOUNDTICK is defined in the rc file: "res\\tick.wav".

History

  • 20 June 2005 - Version 1.0.