在使用settimer()进行MFC应用程序时遇到问题

时间:2015-06-23 15:27:26

标签: c++ timer mfc

我设置了这个代码,所以我可以为扫雷游戏准备一个计时器,但我无法编译它。

    void CALLBACK CMineSweeperBoard::clock(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime)
{
    if (t_seconds < 59){ t_seconds++; }
    else{
     t_minutes++;
     t_seconds = 0;
    }   
}

void CMineSweeperBoard::timer(void)
{
    MSG msg;

    SetTimer(NULL, 0, 1000, (TIMERPROC) &clock);
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
}

问题似乎与set timer函数中的参数有关,但我无法找出它是什么,任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:4)

您需要SetTimerKillTimerON_WM_TIMER()。请参阅本页底部的示例: https://msdn.microsoft.com/en-us/library/49313fdf.aspx

不要在其中放置消息循环 While(GetMessage()...)

您可以通过调用SetTimer(1, 1000, NULL);启动1秒计时器然后将ON_WM_TIMER()添加到消息映射,将结果传递给void CMyWnd::OnTimer(UINT nIDEvent)这样您就不需要定义自己的TimerProc

或者您可以提供自己的TimerProc,但TimerProc函数必须声明为 static 。这可能不方便,因为静态成员函数无法访问成员数据。使用WM_TIMER会更容易。