📄 ngtimer.cpp
字号:
////////////////////////////////////////////////////////////////
// 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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -