检测5秒内鼠标是否移动的函数
我想检测鼠标是否在 5 秒内移动,如果是则显示经过的时间。这是我的代码,看起来没问题,但无法正常工作。
void timer()
{
if (ismouseclick(WM_MOUSEMOVE))
{
movetime=clock();
clearmouseclick(WM_MOUSEMOVE);
}
if ((clock()-movetime)<6)
{
sprintf(time_str,"%d",clock();
outtextxy(275,483,"Time: ");
outtextxy(340,483,time_str);
}
else
{
setfillstyle(1,0);
bar(275,483,370,500);
}
}
I want to detect if mouse moved in 5 seconds, if yes time elapsed is showed. Here is my code, it seems ok but doesn't work correctly.
void timer()
{
if (ismouseclick(WM_MOUSEMOVE))
{
movetime=clock();
clearmouseclick(WM_MOUSEMOVE);
}
if ((clock()-movetime)<6)
{
sprintf(time_str,"%d",clock();
outtextxy(275,483,"Time: ");
outtextxy(340,483,time_str);
}
else
{
setfillstyle(1,0);
bar(275,483,370,500);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是关于clock()函数的。如果需要秒数,则需要将其除以 CLK_TCK(常量)。
Its about clock() function. You need to divide it to CLK_TCK (a constant) if you want seconds.
clock()
通常返回以毫秒为单位的经过时间(这取决于操作系统,因此请检查操作系统的文档),因此当您检查(clock() - movetime)
(clock() - movetime)
(clock() - movetime)
(clock() - movetime)
(clock() - movetime)
6
,这不是实际经过的时间(以秒为单位),而是很可能以毫秒为单位。因此,您可能没有看到您期望的打印输出(即,每次调用timer()
时它可能都会打印,或者可能根本不打印)。clock()
typically returns the elapsed time in milliseconds (this is OS dependent, so check your OS's documentation), so when you are checking whether(clock() - movetime) < 6
, that's not the elapsed time in actual seconds, but most likely milliseconds. Therefore you're probably not seeing the print-out you're expecting (i.e., it probably prints ever time you calltimer()
, or maybe it doesn't print at all).