Skip to content

Instantly share code, notes, and snippets.

@taisyo7333
Last active August 29, 2015 14:28
Show Gist options
  • Save taisyo7333/5935925885122c5c4a5d to your computer and use it in GitHub Desktop.
Save taisyo7333/5935925885122c5c4a5d to your computer and use it in GitHub Desktop.
Win32API SetTimer関数のコールバック関数からメンバ関数をコールしたい。
#pragma once
#include "stdafx.h"
// Reference URL
// https://social.msdn.microsoft.com/Forums/ja-JP/1a0354fc-8e7c-4d75-b65e-6e38041b6e5d/c-how-send-the-class-pointer-with-settimer-function?forum=vclanguage
class Timer
{
public:
Timer()
:idKill(0)
{}
~Timer()
{
if( isLive() ) {
End();
}
}
void Start(UINT uElapse_ms)
{
idKill = ::SetTimer(0,
reinterpret_cast<UINT_PTR>(this),
uElapse_ms,
&Timer::_TimerProc);
}
BOOL End()
{
auto result = ::KillTimer(0,idKill);
idKill = 0;
return result;
}
private:
UINT_PTR idKill;
BOOL isLive() const
{
return idKill != 0 ;
}
static void CALLBACK _TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime )
{
reinterpret_cast<Timer*>(idEvent)->TimerProc(hwnd,uMsg,idEvent,dwTime);
}
void TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
{
TRACE(_T("Timer::MEM_FUNC: uMsg=%08X , id=%08X , time=%ld\n"),uMsg,idEvent,dwTime);
}
};
/* usage sample
class WORKER
{
public:
WORKER()
:spTimer( new Timer())
{
spTimer->Start(1000);
}
~WORKER(){}
private:
std::unique_ptr<Timer> spTimer;
};
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment