Click here to Skip to main content
15,891,253 members
Articles / Desktop Programming / MFC
Article

BinClock

Rate me:
Please Sign up or sign in to vote.
3.00/5 (9 votes)
20 Jun 2005 36.7K   493   18   4
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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralA binary clock shouldn't "tick-tock". Pin
WREY21-Jun-05 10:22
WREY21-Jun-05 10:22 
GeneralRe: A binary clock shouldn't "tick-tock". Pin
Roger6521-Jun-05 10:30
Roger6521-Jun-05 10:30 
GeneralRe: A binary clock shouldn't "tick-tock". Pin
Anonymous22-Jun-05 3:04
Anonymous22-Jun-05 3:04 
GeneralRe: A binary clock shouldn't "tick-tock". Pin
Anonymous29-Jun-05 12:47
Anonymous29-Jun-05 12:47 

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.