Click here to Skip to main content
15,892,199 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 165.9K   6.7K   82  
XTimer provides countdown timer and stopwatch features in a compact MFC dialog app.
// XTimerDlg.h  Version 1.3 - article available at http://www.codeproject.com/tools/xtimer.asp
//
// Author:  Hans Dietrich
//          hdietrich@gmail.com
//
// License:
//     This software is released into the public domain.  You are free to use
//     it in any way you like, except that you may not sell this source code.
//
//     This software is provided "as is" with no expressed or implied warranty.
//     I accept no liability for any damage or loss of business that this 
//     software may cause.
//
///////////////////////////////////////////////////////////////////////////////

#ifndef XTIMERDLG_H
#define XTIMERDLG_H

#include "StaticCounter.h"
#include "hightime.h"
#include "BtnST.h"

//=============================================================================
class CXTimerDlg : public CDialog
//=============================================================================
{
// Construction
public:
	CXTimerDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	//{{AFX_DATA(CXTimerDlg)
	enum { IDD = IDD_XTIMER_DIALOG };
	CStaticCounter	m_Time;
	CButtonST		m_btnStart;
	CButtonST		m_btnReset;
	CButtonST		m_btnMore;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CXTimerDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	HICON			m_hIcon;
	CBrush			m_brush;
	CHighTime		m_StartTime;
	int				m_nTimerSeconds;		// used in timer mode - no. of 
											// seconds for timer to run
	CString			m_strSoundFile;
	BOOL			m_bTimerStarted;		// timer is running - used for 
											// context menu display
	BOOL			m_bTimer;				// TRUE = countdown timer mode
											// FALSE = stopwatch mode
	BOOL			m_bPlaySound;			// TRUE = play sound file or play 
											// default sound
	int				m_nPlayOnce;			// 0 = play sound once;
											// 1 = play sound until stopped
	BOOL			m_bContinuousPlay;		// TRUE = in continuous play mode
	CHighTimeSpan	m_TimeLeft;				// for timer
	CHighTimeSpan	m_TimeElapsed;			// for stopwatch

	void	LoadMenu(CPoint point);
	void	LoadProfile();
	void	PlaySoundFile();
	void	Reset();
	void	RestoreWindowPos(HWND hWnd, LPCTSTR lpszEntry);
	void	SaveProfile();
	void	SaveWindowPos(HWND hWnd, LPCTSTR lpszEntry, UINT nShowCmd);
	void	SetWindowText();

	// Generated message map functions
	//{{AFX_MSG(CXTimerDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
	afx_msg void OnStart();
	afx_msg void OnStop();
	afx_msg void OnReset();
	afx_msg void OnAbout();
	afx_msg void OnMore();
	afx_msg void OnXTimer();
	afx_msg void OnStopwatch();
	afx_msg void OnTimerOptions();
	afx_msg void OnNcLButtonDblClk(UINT nHitTest, CPoint point);
	afx_msg void OnDestroy();
	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	//}}AFX_MSG
	afx_msg void OnQuickTimer(UINT nID);

	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif //XTIMERDLG_H

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