Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / MFC

XTimer - Timer and Stopwatch Utility with Source Code

Rate me:
Please Sign up or sign in to vote.
4.81/5 (33 votes)
20 Aug 2007CPOL3 min read 166.1K   6.7K   82  
XTimer provides countdown timer and stopwatch features in a compact MFC dialog app.
// TimerOptions.cpp : implementation file
//

#include "stdafx.h"
#include "XTimer.h"
#include "TimerOptions.h"
#include "mmsystem.h"
#include <io.h>

#pragma warning(disable : 4996)	// disable bogus deprecation warning

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

///////////////////////////////////////////////////////////////////////////////
// CTimerOptions dialog

BEGIN_MESSAGE_MAP(CTimerOptions, CDialog)
	//{{AFX_MSG_MAP(CTimerOptions)
	ON_BN_CLICKED(IDC_PLAY, OnPlay)
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_BN_CLICKED(IDC_PLAYSOUND, OnPlaysound)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////////
// ctor
CTimerOptions::CTimerOptions(CWnd* pParent /*=NULL*/)
	: CDialog(CTimerOptions::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTimerOptions)
	m_nHours = 0;
	m_nMinutes = 0;
	m_nSeconds = 0;
	m_strSoundFile = _T("");
	m_bPlaySound = TRUE;
	//}}AFX_DATA_INIT
	m_nPlayOnce = 0;
}

///////////////////////////////////////////////////////////////////////////////
// DoDataExchange
void CTimerOptions::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTimerOptions)
	DDX_Control(pDX, IDC_PLAYSOUND, m_chkPlaySound);
	DDX_Control(pDX, IDC_SECONDS, m_edtSeconds);
	DDX_Control(pDX, IDC_MINUTES, m_edtMinutes);
	DDX_Control(pDX, IDC_HOURS, m_edtHours);
	DDX_Text(pDX, IDC_HOURS, m_nHours);
	DDX_Text(pDX, IDC_MINUTES, m_nMinutes);
	DDX_Text(pDX, IDC_SECONDS, m_nSeconds);
	DDX_Text(pDX, IDC_SOUND, m_strSoundFile);
	DDX_Control(pDX, IDC_PLAY, m_btnPlay);
	DDX_Check(pDX, IDC_PLAYSOUND, m_bPlaySound);
	DDX_Radio(pDX, IDC_RADIO_ONCE, m_nPlayOnce);
	//}}AFX_DATA_MAP
}

///////////////////////////////////////////////////////////////////////////////
// OnInitDialog
BOOL CTimerOptions::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_edtHours.SetLimitText(2);
	m_edtMinutes.SetLimitText(2);
	m_edtSeconds.SetLimitText(2);

	m_btnPlay.SetIcon(IDI_PLAY, CXButtonXP::LEFT);

	//m_btnPlay.SetFlat(FALSE);
	//m_btnPlay.SetBitmaps(IDB_PLAYD, RGB(255,0,255), IDB_PLAYU, RGB(255,0,255));

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

///////////////////////////////////////////////////////////////////////////////
// OnOK
void CTimerOptions::OnOK() 
{
	UpdateData(TRUE);

	if (m_nHours > 24)
	{
		AfxMessageBox(_T("Hours must be an integer between 0 and 24."),
			MB_OK | MB_ICONEXCLAMATION);
		m_edtHours.SetFocus();
		return;
	}

	CDialog::OnOK();
}

///////////////////////////////////////////////////////////////////////////////
// OnPlay
void CTimerOptions::OnPlay() 
{
	UpdateData(TRUE);

	if (!m_strSoundFile.IsEmpty())
	{
		if (_taccess(m_strSoundFile, 04) == 0)
		{
			::PlaySound(m_strSoundFile, AfxGetApp()->m_hInstance,
				SND_FILENAME|SND_ASYNC|SND_NOWAIT|SND_NODEFAULT);
		}
		else
		{
			AfxMessageBox(_T("The sound file cannot be played.\r\n")
						  _T("Please check the file and path name."));
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// OnBrowse
void CTimerOptions::OnBrowse() 
{
	::PlaySound(NULL, NULL, 0);		// stop anything playing

	TCHAR * pszFilters = _T("Wav Files (.wav)\0*.wav\0")
						 _T("All Files (*.*)\0*.*\0\0");

	static TCHAR szPathName[MAX_PATH*2];
	szPathName[0] = _T('\0');

	if (m_strSoundFile.IsEmpty())
	{
		if (szPathName[0] == _T('\0'))
			::GetModuleFileName(AfxGetInstanceHandle(), szPathName, 
					sizeof(szPathName)/sizeof(TCHAR)-1);
	}
	else
	{
		_tcsncpy(szPathName, m_strSoundFile, sizeof(szPathName)/sizeof(TCHAR)-1);
		szPathName[sizeof(szPathName)/sizeof(TCHAR)-1] = _T('\0');
	}

	TCHAR *cp = _tcsrchr(szPathName, _T('\\'));
	if (cp != NULL)
		*cp = _T('\0');

	TCHAR szFileDlgBuf[2000];
	szFileDlgBuf[0] = _T('\0');

	{
		CFileDialog dlg(TRUE, NULL, NULL, 0, NULL, this);

		dlg.m_ofn.lpstrFilter     = pszFilters;
		dlg.m_ofn.nFilterIndex    = 0;
		dlg.m_ofn.lpstrFile       = szFileDlgBuf;
		dlg.m_ofn.nMaxFile        = sizeof(szFileDlgBuf)/sizeof(TCHAR) - 10;
		dlg.m_ofn.lpstrInitialDir = szPathName;
		dlg.m_ofn.Flags          |= OFN_EXPLORER |
									OFN_FILEMUSTEXIST |
									OFN_PATHMUSTEXIST |
									OFN_HIDEREADONLY;

		if (dlg.DoModal() != IDOK)
			return;
	}

	UpdateWindow();

	if (szFileDlgBuf[0] == _T('\0'))
		return;

	TCHAR drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
	_tsplitpath(szFileDlgBuf, drive, dir, fname, ext);

	_tcscpy(szPathName, drive);
	_tcscat(szPathName, dir);

	m_strSoundFile = szFileDlgBuf;

	GetDlgItem(IDC_SOUND)->SetWindowText(m_strSoundFile);
}

///////////////////////////////////////////////////////////////////////////////
// OnPlaysound
void CTimerOptions::OnPlaysound() 
{
	UpdateData(TRUE);

	if (m_bPlaySound)
	{
		GetDlgItem(IDC_SOUND)->EnableWindow(TRUE);
		GetDlgItem(IDC_BROWSE)->EnableWindow(TRUE);
		GetDlgItem(IDC_PLAY)->EnableWindow(TRUE);
		GetDlgItem(IDC_RADIO_ONCE)->EnableWindow(TRUE);
		GetDlgItem(IDC_RADIO_CONTINUOUS)->EnableWindow(TRUE);
	}
	else
	{
		GetDlgItem(IDC_SOUND)->EnableWindow(FALSE);
		GetDlgItem(IDC_BROWSE)->EnableWindow(FALSE);
		GetDlgItem(IDC_PLAY)->EnableWindow(FALSE);
		GetDlgItem(IDC_RADIO_ONCE)->EnableWindow(FALSE);
		GetDlgItem(IDC_RADIO_CONTINUOUS)->EnableWindow(FALSE);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions