自定义定时器类

/**************************************
  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();

	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值