vc的SetTimer的API相信都是用了烂了不能再烂了,但是呢,一般来说是用在窗口类中的,只要SetTimer一下,然后就等着接受Ontimer消息就好了。话说c++最重要的是面向对象的类的思想了,怎么在类中使用呢,那几只好用回调函数了。
先看看SetTimer api吧
UINT_PTR SetTimer(
HWND
hWnd,
UINT_PTR
nIDEvent,
UINT
uElapse,
TIMERPROC
lpTimerFunc
);
最后一个是回调函数的指针了,可是关键的还是要有hWnd,也就是要和窗口相关联了,那么也就是说如果你在自定义的类中使用的话,还得传入一个窗口的句柄了,否则这个api就用不了。虽说如此其实应该也可以用NULL的,看微软的说法:
[in] Handle to the window to be associated with the timer. This window must be owned by the calling thread. If a NULL value for hWnd is passed in along with an nIDEvent of an existing timer, that timer will be replaced in the same way that an existing non-NULL hWnd timer will be.
如果没有句柄那么nIDEvent就是唯一的了,如果是重复的id的话就会替代原来的计时器了。
另外tickcount好像也可以实现定时的,不做深究了。
那么看看回调函数:
VOID CALLBACK TimerProc(
HWND
hwnd,
UINT
uMsg,
UINT_PTR
idEvent,
DWORD
dwTime
);
这个就是回调函数了,使用的时候要在头文件中写在类外面,而且要加static,因为操作系统回调的函数都必须是静态的,不能加this指针。
static void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime);//头文件中定义的
::SetTimer(this-> m_hWnd,1,10,(TIMERPROC)TimerProc);
void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent, DWORD dwTime)
{
//你的代码
}
吵吵微信朋友圈,请付款实名加入:
真是方法对了。
这个解决方法很值得推广。
[…] vc使用SetTimer回调函数实现自定义类定时器解决方案 […]
It is known that cash can make us free. But how to act when one doesn’t have cash? The one way only is to get the home loans and just secured loan.