【VC学习】Dialog1_2秒内按键次数计时_自行整理

来源:互联网 发布:深圳软件外包公司排名 编辑:程序博客网 时间:2024/05/19 00:37
 老师要求:在我们上次课的例子基础上,在EDIT窗口中输入0,然后按下按钮开始计时,每按下一次,EDIT中数加1,直到2秒(或其它时长),再按就不能加1了 
目前只会button、static、edit三个函数,以及messagebox,老师推荐了一个GetTickCount();函数,返回DWORD(long int)型数值,得到计算机从起动开始,到目前共运行了多长时间,以毫秒为单位。
MSDN,这个VS2010还是在线的,这个...
https://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(GETTICKCOUNT);k(DevLang-"C%2B%2B")&rd=true

GetTickCount function

Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.

Syntax

C++
DWORD WINAPI GetTickCount(void);

Parameters

This function has no parameters.

Return value

The return value is the number of milliseconds that have elapsed since the system was started.

Remarks

The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds. The resolution of the GetTickCount function is not affected by adjustments made by the GetSystemTimeAdjustment function.

The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days. To avoid this problem, use theGetTickCount64 function. Otherwise, check for an overflow condition when comparing times.

If you need a higher resolution timer, use a multimedia timer or a high-resolution timer.

To obtain the time elapsed since the computer was started, retrieve the System Up Time counter in the performance data in the registry key HKEY_PERFORMANCE_DATA. The value returned is an 8-byte value. For more information, see Performance Counters.

To obtain the time the system has spent in the working state since it was started, use the QueryUnbiasedInterruptTime function.

Note  The QueryUnbiasedInterruptTime function produces different results on debug ("checked") builds of Windows, because the interrupt-time count and tick count are advanced by approximately 49 days. This helps to identify bugs that might not occur until the system has been running for a long time. The checked build is available to MSDN subscribers through the Microsoft Developer Network (MSDN) Web site.

Requirements

Minimum supported client

Windows 2000 Professional [desktop apps only]

Minimum supported server

Windows 2000 Server [desktop apps only]

Header

Winbase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll
看了下,大概明白是怎么个意思了,但是具体的用法还是会有点模糊,所以又上网看了看别人的,看这位博主写的不错,特搬至此
form博客园  网址http://www.cnblogs.com/jxsoft/archive/2011/10/17/2215366.html

DWORD GetTickCount(void);    

1) 定义

For Release configurations, this function returns the number of milliseconds since the device booted, excluding any time that the system was suspended. GetTickCount starts at 0 on boot and then counts up from there.

在Release版本中,该函数从0开始计时,返回自设备启动后的毫秒数(不含系统暂停时间)。

For Debug configurations, 180 seconds is subtracted from the the number of milliseconds since the device booted. This allows code that uses GetTickCount to be easily tested for correct overflow handling.

在Debug版本中,设备启动后便从计时器中减去180秒。这样方便测试使用该函数的代码的正确溢出处理。

Return Values

The number of milliseconds indicates success.

返回值:如正确,返回毫秒数。

Header: Winbase.h.
Link Library: Coredll.lib.

2) 应用

用来计算某个操作所使用的时间:   

  Start:=GetTickCount;   
   ...//执行耗时的操作   
   Stop:=GetTickCount;   
   TimeUsed:=(Stop-Start)/1000;     //使用了xxx秒

用来定时  


void main() 
{
  DWORD dwLast;
  DWORD dwCurrent;
  DWORD dwInterval = 1000;
  dwLast = GetTickCount();
  int i = 0;
  while(true)
     {
        dwCurrent = GetTickCount();
        if( dwCurrent - dwLast < dwInterval )
        continue;
         //your code to be executed when interval is elapsed
        printf("dwLast,dwCurrent,diff:%d,%d,%d ",dwLast,dwCurrent,dwCurrent-dwLast);
         //your code to determine when to break
        if( i > 10 ) break;
        i++;
        dwLast = dwCurrent;
        printf("Time is up!");
        break;
     }

   getchar();   
   return;
}
 

对于一般的实时控制,使用GetTickCount()函数就可以满足精度要求,但要进一步提高计时精度,就要采用QueryPerformanceFrequency()函数和QueryPerformanceCounter()函数。这两个函数是VC提供的仅供Windows   9X使用的高精度时间函数,并要求计算机从硬件上支持高精度计时器。

以上引用:

http://www.wesoho.com/article.asp?id=2072

http://bitboy.blog.edu.cn/user1/19986/archives/2005/1001846.shtml



0 0