Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

BinClock

0.00/5 (No votes)
21 Jun 2005 1  
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.

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