Definition:
# 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, uint umsg, wparam, 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 );
}
};
Usage:
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 );
}
};