Windows程序设计:计时器

微笑、不失礼 提交于 2019-11-27 18:40:39

我们可以通过调用SetTimer函数建立一个计时器。SetTimer函数有一个时间间隔范围为1~4294967295毫秒(将近50天)的整型参数,这个值指示windows每隔一定时间给您的出现发送一个WM_TIMER消息。

在Windows98中,计时器具有55毫秒的分辨率。在Windows NT中,计时器具有10毫秒的分辨率。Windows不能以高于这一分辨率的速度接受WM_TIMER消息。

SetTimer函数使用方法如下:
SetTimer(hwnd, 1, uiMsecInterval, NULL);

第一个参数是其窗口过程将要接受WM_TIMER消息的窗口的句柄。第二个参数是计时器ID,他是一个非0数。第三个参数是一个32位无符号整数,以毫秒为单位指定一个时间间隔。第四个参数为回调函数的地址,若为NULL,则用默认的WndProc。也可以另外指定回叫函数:
SetTimer(hwnd, 1, uiMsecInterval, TimerProc);

回叫函数TimerProc可定义如下:
VOID CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime){}

我们可以调用KillTimer (hwnd, ID_TIMER)在任何时刻停止计时器。

下面的例子,按秒刷新,显示当前时间:

#include <windows.h>
#define ID_TIMER    1
 
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
 
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("DigClock") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
 
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
 
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
 
     hwnd = CreateWindow (szAppName, TEXT ("Clock"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
 
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
 
     while (GetMessage (&msg, NULL, 0, 0))
          {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
          }
     return msg.wParam ;
     }
 
 
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC           hdc ;
     PAINTSTRUCT   ps ;
     TCHAR         szBuffer [10] ;
 
     switch (message)
     {
     case WM_CREATE:
          SetTimer (hwnd, ID_TIMER, 1000, NULL) ;
 
     case WM_TIMER:
          InvalidateRect (hwnd, NULL, TRUE) ;
          return 0 ;
 
     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
 
          SYSTEMTIME st ;      
          GetLocalTime (&st) ; //获取当前时间
          //格式化时间
          GetTimeFormat(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT, &st, NULL, szBuffer, 10);
 
          //在屏幕上输出显示时间
          TextOut (hdc,0,0,szBuffer,8);
 
          EndPaint (hwnd, &ps) ;
          return 0 ;
 
     case WM_DESTROY:
          KillTimer (hwnd, ID_TIMER) ;
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

使用回叫函数TimerProc的版本:

#include <windows.h>
#define ID_TIMER    1
 
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
VOID CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime);
 
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("DigClock") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
 
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
 
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
 
     hwnd = CreateWindow (szAppName, TEXT ("Clock"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
 
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
 
     while (GetMessage (&msg, NULL, 0, 0))
          {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
          }
     return msg.wParam ;
     }
 
 
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     switch (message)
     {
     case WM_CREATE:
          SetTimer (hwnd, ID_TIMER, 1000, TimerProc) ;
          return 0;
 
     case WM_DESTROY:
          KillTimer (hwnd, ID_TIMER) ;
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 
VOID CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
    HDC hdc;
    TCHAR         szBuffer [10] ;
 
    hdc = GetDC(hwnd);
 
    SYSTEMTIME st ;      
    GetLocalTime (&st) ;
    GetTimeFormat(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT, &st, NULL, szBuffer, 10);
    TextOut (hdc,0,0,szBuffer,8);
 
    ReleaseDC(hwnd, hdc);
}

函数GetTimeFormat用于格式化输出时间。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!