/**************************************
XTimer.h -- the class Timer
version 1.0.8, Apr 26th, 2016
Copyright (C) 2015-2016 Acheld CHEN
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
*************************************************/
//Xtime.h , header file
#pragma once
#include "afxwin.h"
class CXTimer :
public CWnd
{
public:
CXTimer(int milliseconds = 200);
~CXTimer(void);
public:
int Init();
void StartTimer( );
void KillTimer();
protected:
UINT m_nTimer;
int m_milliseconds;
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnTimer(UINT_PTR nIDEvent);
static void Tick();
static void TimerProc(HWND hwnd,UINT uMsg,UINT IdEvent,DWORD dwTime );
};
-------------------
// XTimer.cpp
#include "StdAfx.h"
#include "XTimer.h"
#include "MyWnd.h"
#include "Mgr.h"
CMgr* theMgr = NULL;
MYWND_IMPLEMENT_DYNCREATE
///定义消息映射
MYWND_BEGIN_MESSAGE_MAP
MYWND_END_MESSAGE_MAP
BEGIN_MESSAGE_MAP(CXTimer, CWnd)
ON_WM_TIMER()
END_MESSAGE_MAP()
void CXTimer::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (nIDEvent == m_nTimer) {
Tick(); //GetTickCount()
}
CWnd::OnTimer(nIDEvent);
}
CXTimer::CXTimer(int milliseconds)
{
m_nTimer = 0;
m_milliseconds = milliseconds;
Init();
}
CXTimer::~CXTimer(void)
{
}
int CXTimer::Init()
{
HWND hwnd;
MYWND_CREATE(hwnd);
if (hwnd){
this->m_hWnd = hwnd;
}
return 1;
}
void CXTimer::StartTimer( )
{
if(m_nTimer != 0) return;
int timer_id = 1010;
do {
//m_nTimer = CWnd::SetTimer( timer_id, m_milliseconds, 0 );
m_nTimer = CWnd::SetTimer( timer_id, m_milliseconds, (TIMERPROC)TimerProc );
timer_id++;
} while( m_nTimer == 0 && timer_id < 50 );
}
void CXTimer::KillTimer()
{
if(m_nTimer == 0) return;
CWnd::KillTimer(m_nTimer);
m_nTimer = 0;
}
void CXTimer::Tick()
{
DWORD dwTick = GetTickCount();
if (!theMgr){
theMgr = new CMgr();
}
theMgr->TickMgr(NULL);
}
void CXTimer::TimerProc(HWND hwnd,UINT uMsg,UINT IdEvent,DWORD dwTime )
{
static int nCount = 0;
nCount++;
Tick();
}
自定义定时器类
最新推荐文章于 2023-01-19 12:03:27 发布