Donovan Haacke
Donovan Haacke

Reputation: 33

Having trouble using settimer() for MFC application

I set up this this code so I can have a timer for a minesweeper game but I can't get it to compile.

    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);
}

The issue seems to be with the arguments in the set timer function but I can't find out what it is, any help would be appreciated.

Upvotes: 3

Views: 1420

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31619

You need SetTimer, KillTimer, and ON_WM_TIMER(). See the example at the bottom of this page: https://msdn.microsoft.com/en-us/library/49313fdf.aspx

Don't put a message loop in there While(GetMessage()...)

You can start a 1 second timer by calling SetTimer(1, 1000, NULL); Then add ON_WM_TIMER() to message map, which passes the result to void CMyWnd::OnTimer(UINT nIDEvent) This way you don't need to define your own TimerProc

Or you can supply your own TimerProc, but TimerProc function will have to be declared as static. This can be inconvenient because the static member functions cannot access member data. It would be easier to use WM_TIMER instead.

Upvotes: 4

Related Questions