Windows下的定時器模板類

來源:互聯網
上載者:User

定義:

#pragma once

#include <windows.h>

template <typename T>
class TTimerHandle
{
 typedef void (T::*TimerHandle)();

 T *m_pObj;
 TimerHandle m_pfnHandle;

 HWND m_hWnd;

public:

 TTimerHandle(T *pObj, TimerHandle pfnHandle)
 {
  m_pObj = pObj;
  m_pfnHandle = pfnHandle;

  RegisterClass();
  CreateWnd();
 }
 ~TTimerHandle()
 {
  if (m_hWnd)
  {
   Stop();
   CloseWindow(m_hWnd);
  }
 }

 void Start(unsigned int uMinSeconds)
 {
  if (m_hWnd)
  {
   ::SetTimer(m_hWnd, 1, uMinSeconds, NULL);
  }
 }
 void Stop()
 {
  if (m_hWnd)
  {
   ::KillTimer(m_hWnd, 1);
  }
 }

private:

 BOOL RegisterClass()
 {
  WNDCLASS stWndClass;
  stWndClass.style = CS_HREDRAW | CS_VREDRAW;
  stWndClass.cbClsExtra = 0;
  stWndClass.cbWndExtra = 0;
  stWndClass.hInstance = ::GetModuleHandle(NULL);
  stWndClass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
  stWndClass.hCursor = ::LoadCursor(NULL, IDC_ARROW);
  stWndClass.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
  stWndClass.lpszClassName = _T("MyThreadClass");
  stWndClass.lpszMenuName = NULL;
  stWndClass.lpfnWndProc = (WNDPROC)WndProc;

  return ::RegisterClass(&stWndClass);
 }

 BOOL CreateWnd()
 {
  m_hWnd = ::CreateWindow(_T("MyThreadClass"), _T(""), WS_OVERLAPPEDWINDOW,
   CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, GetModuleHandle(NULL), this);
  if (m_hWnd)
  {
   ::ShowWindow(m_hWnd, SW_HIDE);
   ::UpdateWindow(m_hWnd);
  }
  return (m_hWnd != NULL);
 }

 static LRESULT WINAPI WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 {
  switch (uMsg)
  {
  case WM_NCCREATE:
   {
    CREATESTRUCT *pCreateStruct = (CREATESTRUCT*)lParam;
    if (pCreateStruct)
    {
     ::SetWindowLong(hWnd, GWL_USERDATA, (LONG)pCreateStruct->lpCreateParams);
    }
   }
   break;
  case WM_TIMER:
   {
    TTimerHandle<T> *pClass = (TTimerHandle<T>*)::GetWindowLong(hWnd, GWL_USERDATA);
    if (pClass)
    {
     ( (pClass->m_pObj)->*(pClass->m_pfnHandle) )();
    }
   }
   break;
  }

  return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
 }
};

 

使用:

class CTestTimer
{
public:
 void TimerHandle()
 {
  ::OutputDebugStringA("CTestTimer::Timer /n");
 }
 void TimerHandle2()
 {
  ::OutputDebugStringA("CTestTimer::Timer2 /n");
 }
 TTimerHandle<CTestTimer> m_Timer1;
 TTimerHandle<CTestTimer> m_Timer2;

 CTestTimer() : m_Timer1(this, &CTestTimer::TimerHandle), m_Timer2(this, &CTestTimer::TimerHandle2)
 {
  m_Timer1.Start(5000);
  m_Timer2.Start(10000);
 }
};

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.