|
////////////////////////////////////////////////////////////////
// CNGTimer Copyright 1999 by Anna-Jayne Metcalfe (code@annasplace.me.uk)
//
// CNGTimer makes using Timers easy in non-window objects
// like CDocuments.
// NGTimer.cpp : implementation file
//
#include "StdAfx.h"
#include "NGTimer.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNGTimer construction/destruction
CNGTimer::CNGTimer(void)
{
m_nID = 0;
m_nInterval = 0;
m_pTarget = NULL;
m_nCmdID = 0;
}
CNGTimer::~CNGTimer(void)
{
Stop(); // Just in case
}
/////////////////////////////////////////////////////////////////////////////
// CNGTimer overrides
void CNGTimer::OnTimer(DWORD dwTime)
{
m_dwTime = dwTime;
if (NULL != m_pTarget)
{
m_pTarget->OnCmdMsg(m_nCmdID, CN_COMMAND, NULL, NULL);
}
}
/////////////////////////////////////////////////////////////////////////////
// CNGTimer operations
/************************************************************************
* Start the timer.
*
* The first form start a timer which sends a WM_COMMAND message with a specified
* ID to a command target.
*
* The second sends a WM_TIMER message directly to a window.
*
* The third restarts a paused timer
*
************************************************************************/
UINT CNGTimer::Start( CCmdTarget* pTarget, // Target to send WM_COMMAND message to
UINT nID, // ID of message
UINT nInterval) // Interval in ms
{
ASSERT_VALID(pTarget);
ASSERT(nID > 0);
ASSERT(nInterval > 0);
if (m_nID > 0) // timer running, so kill it first
{
Stop();
}
if (nInterval > 0)
{
m_nID = ::SetTimer( NULL,
0,
nInterval,
CNGTimer::TimerProc);
if (m_nID > 0)
{
m_nInterval = nInterval;
m_pTarget = pTarget;
m_nCmdID = nID;
m_mapTimers.SetAt(m_nID, this);
}
}
return m_nID;
}
UINT CNGTimer::Start( CWnd* pWnd, // Window to send WM_TIMER message to
UINT nInterval) // Interval in ms
{
ASSERT_VALID(pWnd);
ASSERT(nInterval > 0);
if (m_nID > 0) // Timer running, so kill it first
{
Stop();
}
if (nInterval > 0)
{
m_nID = ::SetTimer( pWnd->GetSafeHwnd(),
0,
nInterval,
NULL);
if (m_nID > 0)
{
m_nInterval = nInterval;
m_pTarget = pWnd;
}
}
return m_nID;
}
UINT CNGTimer::Start(void)
{
if (NULL != m_pTarget)
{
if (m_nCmdID > 0)
{
return Start(m_pTarget, m_nCmdID, m_nInterval);
}
}
return Start( (CWnd*)m_pTarget, m_nInterval);
}
/************************************************************************
* Stop the timer
*
************************************************************************/
BOOL CNGTimer::Stop(void)
{
if (m_nID > 0) // Timer running?
{
::KillTimer(NULL, m_nID);
m_mapTimers.RemoveKey(m_nID);
m_nID = 0;
return TRUE;
}
return FALSE;
}
/************************************************************************
* Set Timer Interval. Note: setting the interval while the timer is running will stop it
* and create a new timer. SetInterval returns the ID of the new timer if running.
*
************************************************************************/
UINT CNGTimer::SetInterval(UINT nInterval)
{
m_nInterval = nInterval; // 0 stops it
if (m_nID > 0) // timer running, so kill it first
{
Stop();
return Start();
}
return m_nID;
}
/////////////////////////////////////////////////////////////////////////////
// CNGTimer diagnostics
#ifdef _DEBUG
void CNGTimer::AssertValid(void) const
{
CNGTimer_BASE::AssertValid();
}
void CNGTimer::Dump(CDumpContext& dc) const
{
CNGTimer_BASE::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CNGTimer Implementation
/************************************************************************
* Static callback method to handle timers destined for non-window
* command targets
*
************************************************************************/
// Static map used to route timer callbacks back into the document
CMapIDtoTimer CNGTimer::m_mapTimers;
void CALLBACK EXPORT CNGTimer::TimerProc( HWND /*hWnd*/, // Handle of CWnd that called SetTimer
UINT /*nMsg*/, // WM_TIMER
UINT nID, // Timer identification
DWORD dwTime) // System time
{
CNGTimer* pTimer = NULL;
CNGTimer::m_mapTimers.Lookup(nID, (CNGTimer*&)pTimer);
ASSERT(NULL != pTimer);
if (NULL != pTimer)
{
pTimer->OnTimer(dwTime);
}
}
|
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.
I haven't always written software for a living. When I graduated from Surrey University in 1989, it was with an Electronic Engineering degree, but unfortunately that never really gave me the opportunity to do anything particularly interesting (with the possible exception of designing
Darth Vader's Codpiece * for the UK Army in 1990).
* Also known as the Standard Army Bootswitch. But that's another story...
Since the opportunity arose to lead a software team developing C++ software for
Avionic Test Systems in 1996, I've not looked back. More recently I've been involved in the development of subsea acoustic navigation systems, digital TV broadcast systems, port security/tracking systems, and most recently software development tools with my own company,
Riverblade Ltd.
One of my personal specialities is IDE plug-in development.
ResOrg was my first attempt at a plug-in, but my day to day work is with
Visual Lint, an interactive code analysis tool environment with works within the Visual Studio and Eclipse IDEs or on build servers.
I love lots of things, but particularly music, photography and anything connected with history or engineering. I
despise ignorant, intolerant and obstructive people - and it shows...I can be a bolshy cow if you wind me up the wrong way...
I'm currently based 15 minutes walk from the beach in Bournemouth on the south coast of England. Since I moved here I've grown to love the place - even if it is full of grockles in Summer!