简体   繁体   English

计时器超时重置

[英]Timer time-out reset

I use a timer to draw animations, but I want the speed of the animation to change upon user input. 我使用计时器绘制动画,但是我希望动画的速度根据用户输入而改变。

On the API's documentation , I read: API的文档中 ,我读到:

A 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. 如果将hWnd的NULL值与现有计时器的nIDEvent一起传递,则将以与现有非NULL hWnd计时器相同的方式替换该计时器。

I understood that I am supposed to call the SetTimer() function without the hWnd parameter to reset the timer, and so I did: 我知道应该在没有hWnd参数的情况下调用SetTimer()函数来重置计时器,所以我做到了:

//function declaration //函数声明

void InitiateTimer(HWND hWnd)
{
    SetTimer(hWnd,                          // handle to main window 
        IDT_TIMER,                          // timer identifier 
        1000 / Robot_Settings::getSpeed(),  // 1-second interval / speed 
        (TIMERPROC)NULL);                   // no timer callback 

    timerInitiated = true;
}

void ResetTimer()
{
    SetTimer(NULL,
        IDT_TIMER, 
        1000 / Robot_Settings::getSpeed(), 
        (TIMERPROC)NULL);
}

//function call in WindowProc // WindowProc中的函数调用

    case BUTTON_START:
        stopClicked = false;
        DestroyWindow(hStartButton);
        CreateStopButton(hWnd);
        if (!timerInitiated)
        {
            InitiateTimer(hWnd);
        }
        else if (timerInitiated)
        {
            ResetTimer();
        }
        return 0;

The idea was that on the reset, the timeout would be recalculated based on Robot_Settings::getSpeed() . 想法是在重置时,将根据Robot_Settings::getSpeed()重新计算超时时间。 Unfortunately, this doesn't happen. 不幸的是,这不会发生。

What did I miss? 我错过了什么?

I think you have misunderstood the docs. 我认为您误解了文档。

To change an existing timer, you have to pass the same combination of hWnd , nIDEvent and lpTimerFunc arguments as when you initially called SetTimer . 要改变现有的计时器,你必须通过相同的组合hWndnIDEventlpTimerFunc参数,当您最初称为SetTimer

From the reference : 参考

If the hWnd parameter is not NULL and the window specified by hWnd already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. 如果hWnd参数不为NULL,并且由hWnd指定的窗口已经具有值为nIDEvent的计时器,则现有计时器将被新计时器替换。

Also: 也:

The timer identifier, nIDEvent, is specific to the associated window. 计时器标识符nIDEvent特定于关联的窗口。 Another window can have its own timer which has the same identifier as a timer owned by another window. 另一个窗口可以拥有自己的计时器,该计时器具有与另一个窗口拥有的计时器相同的标识符。 The timers are distinct. 计时器是不同的。

The last quote alone is proof enough that you always have to specify the hWnd parameter to modify an existing timer that is associated with a window. 仅最后一个引号就足以证明您始终必须指定hWnd参数才能修改与窗口关联的现有计时器。 Otherwise, how should the system know, which timer do you want to change? 否则,系统应如何知道您要更改哪个计时器? You could have two windows, each with a timer ID of 1, which are two distinct timers! 您可能有两个窗口,每个窗口的计时器ID为1,这是两个不同的计时器!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM