#include <windows.h> #define TIMER_SEC 1 #define TIMER_MIN 2 LRESULT CALLBACK MyWNDPROC(HWND hWnd,UINT meseage,WPARAM wParam,LPARAM lParam); int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { WNDCLASSEX ex; HWND hWnd; MSG msg; //1. Design window ex.style = CS_HREDRAW | CS_VREDRAW; ex.cbSize = sizeof(ex); ex.cbClsExtra = 0; ex.cbWndExtra = 0; ex.hInstance = hInstance; ex.hIcon = NULL; ex.hCursor = NULL; ex.hbrBackground = CreateSolidBrush(RGB(0,255,0)); ex.hIconSm = NULL; ex.lpfnWndProc = &MyWNDPROC; ex.lpszMenuName = NULL; ex.lpszClassName = "aaa"; //2. Registration window RegisterClassEx(&ex); //3. Create windows hWnd = CreateWindow(ex.lpszClassName,"Of course, choose to forgive him.",WS_OVERLAPPEDWINDOW,50,50,500,500,NULL,NULL,hInstance,NULL); //4. Display window ShowWindow(hWnd,SW_SHOW); SetTimer(hWnd,TIMER_SEC,500,NULL); //The fourth parameter is the timer message processing function, and if it is NULL, the message processing function of the window specified by the first parameter is used. SetTimer(hWnd,TIMER_MIN,1000,NULL); //Message loop while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } LRESULT CALLBACK MyWNDPROC(HWND hwnd,UINT meseage,WPARAM wPAREAM,LPARAM lParam) { static int n = 10; static int m = 10; HDC hdc1; HDC hdc2; switch(meseage) { case WM_CLOSE: PostQuitMessage(0); break; case WM_KEYDOWN: if(wPAREAM == 65) //Stop timer 1 a key { KillTimer (hwnd, 1); } else if(wPAREAM == 66) //Stop timer 2B key { KillTimer (hwnd, 2); } else if(wPAREAM == 67) //Start timer 1 c key { SetTimer(hwnd,TIMER_SEC,500,NULL); } else if(wPAREAM == 68) //Start timer 2D key { SetTimer(hwnd,TIMER_MIN,1000,NULL); } break; case WM_TIMER: if(wPAREAM == 1) { hdc1 = GetDC(hwnd); Rectangle(hdc1,10 + n,10,30 + n,30); ReleaseDC(hwnd,hdc1); n = n + 10; } else { hdc2 = GetDC(hwnd); Rectangle(hdc2,10 + m,50,30 + m,70); ReleaseDC(hwnd,hdc2); m = m + 10; } break; } return DefWindowProc(hwnd,meseage,wPAREAM,lParam); }
Method 1
This is the most convenient way for Windows to send WM_TIMER messages to the application's normal window message handler. SetTimer calls are as follows:
SetTimer (hwnd, 1, uiMsecInterval, NULL) ;
The first parameter is the window handle whose window message handler will receive WM_TIMER messages. The second parameter is the timer ID, which is a non-zero value and is assumed to be 1 in the whole example. The third parameter is a 32-bit unsigned integer that specifies a time interval in milliseconds and a value of 60,000 will cause Windows to send WM_TIMER messages once a minute.
You can call
KillTimer (hwnd, 1) ;
Stop the WM_TIMER message at any time (even if the WM_TIMER message is being processed). The second parameter of this function is the same timer ID used in the SetTimer call. Before terminating the program, you should respond to the timer where the WM_DESTROY message stops any activity.
When your window message handler receives a WM_TIMER message, the wParam parameter is equal to the ID value of the timer (in this case 1), and the lParam parameter is 0. If multiple timers need to be set, different timer IDs are used for each timer. The value of wParam varies with the WM_TIMER message passed to the window message handler. To make the program more readable, you can define different timer IDs using the # define narrative:
#define TIMER_SEC 1
#define TIMER_MIN 2
Then you can use two SetTimer calls to set two timers:
SetTimer (hwnd, TIMER_SEC, 1000, NULL) ;
SetTimer (hwnd, TIMER_MIN, 60000, NULL) ;