gpt4 book ai didi

c++ - 定时器超时复位

转载 作者:搜寻专家 更新时间:2023-10-31 00:52:19 32 4
gpt4 key购买 nike

我使用计时器绘制动画,但我希望动画的速度根据用户输入而改变。

关于 API's documentation ,我读到:

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 参数的情况下调用 SetTimer() 函数来重置计时器,所以我这样做了:

//函数声明

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

//WindowProc中的函数调用

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

想法是在重置时,超时将根据 Robot_Settings::getSpeed() 重新计算。不幸的是,这不会发生。

我错过了什么?

最佳答案

我认为您误解了文档。

要更改现有计时器,您必须传递与最初调用时相同的 hWndnIDEventlpTimerFunc 参数组合 设置定时器

来自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.

还有:

The timer identifier, nIDEvent, is specific to the associated window. Another window can have its own timer which has the same identifier as a timer owned by another window. The timers are distinct.

仅最后一句话就足以证明您始终必须指定 hWnd 参数来修改与窗口关联的现有计时器。否则,系统怎么知道,你想改变哪个定时器?您可以有两个窗口,每个窗口的计时器 ID 为 1,这是两个不同的计时器!

关于c++ - 定时器超时复位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52154500/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com