📄 wintimerthread.cpp
字号:
#include "stdafx.h"
#include "token.h"
#include "WinTimerThread.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Static declarations
static CMapPtrToPtr g_MapTimerID_TO_CWinThreadPtr;
static void CALLBACK TimerProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_TIMER message
UINT_PTR idEvent, // timer identifier
DWORD dwTime // current system time
)
{
CWinThread* pThread;
if (g_MapTimerID_TO_CWinThreadPtr.Lookup((void *) idEvent, (void*&) pThread))
{
pThread->PostThreadMessage(WM_TIMER, (WPARAM)idEvent, (LPARAM)dwTime);
}
}
// Class
IMPLEMENT_DYNCREATE(CWinTimerThread, CWinThread)
BEGIN_MESSAGE_MAP(CWinTimerThread, CWinThread)
//{{AFX_MSG_MAP(CWinTimerThread)
ON_THREAD_MESSAGE(THREADOVER,OnThreadOver)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CWinTimerThread::CWinTimerThread()
{
m_autoremove = false;
}
CWinTimerThread::~CWinTimerThread()
{
// check if autoremove is activated
if (!m_autoremove)
return;
// declare local variables
POSITION pos;
UINT_PTR idEvent;
CWinThread* pThread;
//get the starting point in the list of timer events
pos = g_MapTimerID_TO_CWinThreadPtr.GetStartPosition();
//loop while there are list items
while (pos)
{
// get timer event entry
g_MapTimerID_TO_CWinThreadPtr.GetNextAssoc(pos, (void *&) idEvent, (void*&) pThread);
// check if the idevent is refering to this object
if (pThread == this)
{
RemoveTimer(idEvent);
}
}
}
UINT_PTR CWinTimerThread::AddTimer(UINT uElapse)
{
UINT_PTR idEvent;
idEvent = ::SetTimer(NULL, NULL, uElapse, (TIMERPROC) TimerProc);
g_MapTimerID_TO_CWinThreadPtr.SetAt((void *) idEvent, this);
return idEvent;
}
BOOL CWinTimerThread::RemoveTimer(UINT_PTR idEvent)
{
::KillTimer(NULL, idEvent);
return g_MapTimerID_TO_CWinThreadPtr.RemoveKey((void *) idEvent);
}
long CWinTimerThread::OnThreadOver(WPARAM wparam,LPARAM lparam)
{
int ThreadType=(int)wparam;
if(ThreadType==1)
{
AfxEndThread(0,TRUE);
}
else if(ThreadType==2)
{
PostQuitMessage(0);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -