Windows 游戏编程大师技巧第四章第6个例子---定时器的使用

  1. // DEMO4_6.CPP  - WM_TIMER demo
  2. // INCLUDES ///
  3. #define WIN32_LEAN_AND_MEAN  // just say no to MFC
  4. #include <windows.h>   // include all the windows headers
  5. #include <windowsx.h>  // include useful macros
  6. #include <mmsystem.h>  // very important and include WINMM.LIB too!
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. // DEFINES 
  11. // defines for windows 
  12. #define WINDOW_CLASS_NAME "WINCLASS1"
  13. #define WINDOW_WIDTH  400
  14. #define WINDOW_HEIGHT 300
  15. // timer defines
  16. #define TIMER_ID1_SEC   1 // id of 1 sec timer
  17. #define TIMER_ID3_SEC   2 // id of 3 sec timer
  18. #define TIMER_ID30_SEC  3 // id of 30 sec timer
  19. // MACROS /
  20. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  21. #define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  22. // GLOBALS 
  23. HWND      main_window_handle = NULL; // globally track main window
  24. HINSTANCE hinstance_app      = NULL; // globally track hinstance
  25. char buffer[80];                     // general printing buffer
  26. // FUNCTIONS //
  27. LRESULT CALLBACK WindowProc(HWND hwnd, 
  28.                             UINT msg, 
  29.                             WPARAM wparam, 
  30.                             LPARAM lparam)
  31. {
  32. // this is the main message handler of the system
  33. PAINTSTRUCT     ps;     // used in WM_PAINT
  34. HDC             hdc;    // handle to a device context
  35. char            buffer[80]; // used to print strings
  36. static int      counter1=0, // counters
  37.                 counter2=0,
  38.                 counter3=0;
  39. // what is the message 
  40. switch(msg)
  41.     {   
  42.     case WM_CREATE: 
  43.         {
  44.         // do initialization stuff here
  45.         // create a 1 second timer
  46.         SetTimer(hwnd, TIMER_ID1_SEC, 1000,NULL);
  47.         // now the 3 second timer
  48.         SetTimer(hwnd, TIMER_ID3_SEC, 3000,NULL);   
  49.         // and finally the 30 second timer
  50.         SetTimer(hwnd, TIMER_ID30_SEC, 30000,NULL); 
  51.         // return success
  52.         return(0);
  53.         } break;
  54.     case WM_TIMER:
  55.          {
  56.          // there is a timer event, test what timer fired
  57.         switch(wparam)
  58.               {
  59.               case TIMER_ID1_SEC:
  60.                    {
  61.                    // print out a message
  62.                    // get the dc
  63.                    hdc = GetDC(hwnd);
  64.                    // set the color
  65.                    SetTextColor(hdc,RGB(0,255,0));
  66.                    SetBkColor(hdc,RGB(0,0,0));
  67.                    SetBkMode(hdc,OPAQUE);
  68.                    // build up the messages
  69.                    sprintf(buffer,"The 1 second timer has fired %d times",++counter1);
  70.                    // print the message
  71.                    TextOut(hdc,0,0,buffer,strlen(buffer));
  72.                    // release the dc
  73.                    ReleaseDC(hwnd,hdc);
  74.                    } break;
  75.               case TIMER_ID3_SEC:
  76.                    {
  77.                    // make a beep
  78.                    MessageBeep(MB_ICONEXCLAMATION);
  79.                    // get the dc
  80.                    hdc = GetDC(hwnd);
  81.                    // set the color
  82.                    SetTextColor(hdc,RGB(0,255,0));
  83.                    SetBkColor(hdc,RGB(0,0,0));
  84.                    SetBkMode(hdc,OPAQUE);
  85.                    // build up the messages
  86.                    sprintf(buffer,"The 3 second timer has fired %d times",++counter2);
  87.                    // print the message
  88.                    TextOut(hdc,0,20,buffer,strlen(buffer));
  89.                    // release the dc
  90.                    ReleaseDC(hwnd,hdc);
  91.                    } break;
  92.             case TIMER_ID30_SEC:
  93.                    {
  94.                    // make a beep
  95.                    MessageBeep(MB_ICONEXCLAMATION);
  96.                    // get the dc
  97.                    hdc = GetDC(hwnd);
  98.                    // set the color
  99.                    SetTextColor(hdc,RGB(0,255,0));
  100.                    SetBkColor(hdc,RGB(0,0,0));
  101.                    SetBkMode(hdc,OPAQUE);
  102.                    // build up the messages
  103.                    sprintf(buffer,"The 30 second timer has fired %d times",++counter3);
  104.                    // print the message
  105.                    TextOut(hdc,0,40,buffer,strlen(buffer));
  106.                    // release the dc
  107.                    ReleaseDC(hwnd,hdc);
  108.                    } break;
  109.                // .. test for other id's
  110.                default:break;
  111.                } // end switch
  112.           } break;
  113.    
  114.     case WM_PAINT: 
  115.         {
  116.         // simply validate the window 
  117.         hdc = BeginPaint(hwnd,&ps);  
  118.         
  119.         // end painting
  120.         EndPaint(hwnd,&ps);
  121.         // return success
  122.         return(0);
  123.         } break;
  124.     case WM_DESTROY: 
  125.         {
  126.         // kill the application, this sends a WM_QUIT message 
  127.         KillTimer(hwnd,TIMER_ID1_SEC);
  128.         KillTimer(hwnd,TIMER_ID3_SEC);
  129.         KillTimer(hwnd,TIMER_ID30_SEC);
  130.         PostQuitMessage(0);
  131.         // return success
  132.         return(0);
  133.         } break;
  134.     default:break;
  135.     } // end switch
  136. // process any messages that we didn't take care of 
  137. return (DefWindowProc(hwnd, msg, wparam, lparam));
  138. // end WinProc
  139. // WINMAIN 
  140. int WINAPI WinMain( HINSTANCE hinstance,
  141.                     HINSTANCE hprevinstance,
  142.                     LPSTR lpcmdline,
  143.                     int ncmdshow)
  144. {
  145. WNDCLASSEX winclass; // this will hold the class we create
  146. HWND       hwnd;     // generic window handle
  147. MSG        msg;      // generic message
  148. HDC        hdc;      // graphics device context
  149. // first fill in the window class stucture
  150. winclass.cbSize         = sizeof(WNDCLASSEX);
  151. winclass.style          = CS_DBLCLKS | CS_OWNDC | 
  152.                           CS_HREDRAW | CS_VREDRAW;
  153. winclass.lpfnWndProc    = WindowProc;
  154. winclass.cbClsExtra     = 0;
  155. winclass.cbWndExtra     = 0;
  156. winclass.hInstance      = hinstance;
  157. winclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  158. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW); 
  159. winclass.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
  160. winclass.lpszMenuName   = NULL;
  161. winclass.lpszClassName  = WINDOW_CLASS_NAME;
  162. winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  163. // save hinstance in global
  164. hinstance_app = hinstance;
  165. // register the window class
  166. if (!RegisterClassEx(&winclass))
  167.     return(0);
  168. // create the window
  169. if (!(hwnd = CreateWindowEx(NULL,                // extended style
  170.                             WINDOW_CLASS_NAME,   // class
  171.                             "Multiple Timer Demo"// title
  172.                             WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  173.                             0,0,      // initial x,y
  174.                             WINDOW_WIDTH, // initial width
  175.                             WINDOW_HEIGHT,// initial height
  176.                             NULL,     // handle to parent 
  177.                             NULL,     // handle to menu
  178.                             hinstance,// instance of this application
  179.                             NULL))) // extra creation parms
  180. return(0);
  181. // save main window handle
  182. main_window_handle = hwnd;
  183. // get the graphics device context 
  184. hdc = GetDC(hwnd);
  185. // enter main event loop, but this time we use PeekMessage()
  186. // instead of GetMessage() to retrieve messages
  187. while(TRUE)
  188.     {
  189.     // test if there is a message in queue, if so get it
  190.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  191.        { 
  192.        // test if this is a quit
  193.        if (msg.message == WM_QUIT)
  194.            break;
  195.     
  196.        // translate any accelerator keys
  197.        TranslateMessage(&msg);
  198.        // send the message to the window proc
  199.        DispatchMessage(&msg);
  200.        } // end if
  201.     // main game processing goes here
  202.     if (KEYDOWN(VK_ESCAPE))
  203.        SendMessage(hwnd, WM_CLOSE, 0,0);
  204.        
  205.     } // end while
  206. // release the device context
  207. ReleaseDC(hwnd,hdc);
  208. // return to Windows like this
  209. return(msg.wParam);
  210. // end WinMain
  211. ///

 

 

 

定时器的使用:

                          1.创建定时器

UINT_PTR SetTimer(      

    HWND hWnd,     UINT_PTR nIDEvent,     UINT uElapse,     TIMERPROC lpTimerFunc );
        2.处理WM_TIMER消息 wparam标识定时器ID 或者 写lpTimerFunc
        3.销毁定时器  
BOOL KillTimer(      

    HWND hWnd,     UINT_PTR uIDEvent );
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值