--- q_a/samples/timers/timers.c 2018/08/09 18:29:19 1.1 +++ q_a/samples/timers/timers.c 2018/08/09 18:29:37 1.1.1.2 @@ -1,14 +1,14 @@ -/************************************************************************\ +/******************************************************************************\ * * PROGRAM: TIMERS.C * * PURPOSE: To demonstrate the use of APIs SetTimer() and KillTimer(). * -* FUNCTIONS: WinMain() - standard WinMain() -* MainWndProc() - main window procedure -* TimerFunc() - handles timer messages -* SetButtonPositions() - sizes buttons to client area -* SetFlashRectPositions() - sizes flashing rectangles to +* FUNCTIONS: WinMain - standard WinMain() +* MainWndProc - main window procedure +* TimerFunc - handles timer messages +* SetButtonPositions - sizes buttons to client area +* SetFlashRectPositions - sizes flashing rectangles to * client area * DrawStuff() - draws separator, headers, * rectangles @@ -17,7 +17,11 @@ * the client area) is inverted each time a WM_TIMER is * received or each timer the TimerFunc() is called. * -\************************************************************************/ +* +* Microsoft Developer Support +* Copyright (c) 1992 Microsoft Corporation +* +\******************************************************************************/ #include #include @@ -25,19 +29,19 @@ -/************************************************************************\ +/******************************************************************************\ * * FUNCTION: WinMain (standard WinMain INPUTS/RETURNS) * -* GLOBAL VARS: hInst - handle of program instance +* GLOBAL VARS: ghInst - handle of program instance * * LOCAL VARS: hwnd - handle of the main standard window * msg - msg to get/dispatch * -\************************************************************************/ +\******************************************************************************/ -int APIENTRY WinMain (HANDLE hInstance,HANDLE hPrevInstance, - LPSTR lpCmdLine, int nCmdShow) { +int WINAPI WinMain (HANDLE hInstance,HANDLE hPrevInstance, + LPSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG msg; @@ -53,29 +57,31 @@ int APIENTRY WinMain (HANDLE hInstance,H wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor (NULL, IDC_ARROW); wc.hbrBackground = GetStockObject (WHITE_BRUSH); - wc.lpszMenuName = (LPSTR) NULL; - wc.lpszClassName = (LPSTR) "TIMERS"; + wc.lpszMenuName = (LPCTSTR) NULL; + wc.lpszClassName = (LPCTSTR) "TIMERS"; if (!RegisterClass (&wc)) - { MessageBox (NULL, "WinMain(): RegisterClass() failed", - "Err! - TIMERS", MB_OK | MB_ICONHAND); + { + MessageBox (NULL, (LPCTSTR) "WinMain(): RegisterClass() failed", + (LPCTSTR) "Err! - TIMERS", MB_OK | MB_ICONEXCLAMATION); return(FALSE); } } - hInst = hInstance; + ghInst = hInstance; if (!(hwnd = CreateWindow ("TIMERS", "TIMERS Sample Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - NULL, NULL, hInstance, NULL))) + NULL, NULL, ghInst, NULL))) return (NULL); - ShowWindow(hwnd, nCmdShow); + ShowWindow (hwnd, nCmdShow); - while (GetMessage(&msg, NULL, NULL, NULL)) - { TranslateMessage(&msg); - DispatchMessage(&msg); + while (GetMessage (&msg, NULL, NULL, NULL)) + { + TranslateMessage (&msg); + DispatchMessage (&msg); } return (msg.wParam); UNREFERENCED_PARAMETER(lpCmdLine); @@ -83,66 +89,79 @@ int APIENTRY WinMain (HANDLE hInstance,H -/************************************************************************\ +/******************************************************************************\ * * FUNCTION: MainWndProc (standard window procedure INPUTS/RETURNS) * -* GLOBAL VARS: hInst - handle of program instance -* hwndButtons - array of button window handles -* flashRects - array of flashing rectangles coordinates -* -\************************************************************************/ - -LONG APIENTRY MainWndProc (HWND hwnd, UINT message, UINT wParam, - LONG lParam) -{ switch (message) - { case WM_CREATE: - { LONG i; - - /******************************************************************\ - * Create the buttons to turn timer on/off - \******************************************************************/ +* GLOBAL VARS: ghInst - handle of program instance +* gahwndButtons - array of button window handles +* gaFlashRects - array of flashing rectangles coordinates +* +\******************************************************************************/ + +LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, + LPARAM lParam) +{ + switch (message) + { + case WM_CREATE: + { + LONG i; + + // + // Create the buttons to turn timer on/off + // + for (i= ID_TIMER1; i <= ID_TIMER4; i++) - { hwndButtons[i] = CreateWindow ("button", "Start timer", + { + gahwndButtons[i] = CreateWindow ("button", "Start timer", WS_CHILD | BS_PUSHBUTTON, 0, 0, 0, 0, hwnd, NULL, - hInst, NULL); - SetWindowLong (hwndButtons[i], GWL_ID, i); + ghInst, NULL); + SetWindowLong (gahwndButtons[i], GWL_ID, i); } - /******************************************************************\ - * Have TIMER1 started initially (giving the user a clue what to do) - \******************************************************************/ + // + // Have TIMER1 started initially (giving the user a clue what to do) + // + PostMessage (hwnd, WM_COMMAND, (DWORD) ID_TIMER1, - (LONG) hwndButtons[ID_TIMER1]); + (LONG) gahwndButtons[ID_TIMER1]); break; } + case WM_COMMAND: - { char buf[BUFSIZE]; + { + char buf[BUFSIZE]; WORD id; id = LOWORD (wParam); GetWindowText ((HWND) lParam, buf, BUFSIZE); if (!strcmp (buf, "Start timer")) { - /****************************************************************\ - * Set button text to "Stop timer" and start the timer. For - * timers 1&2 we'll specify a WNDPROC ("TimerFunc") which will - * get called for each timer tick. For timers 3&4 we'll just - * have WM_TIMER messages sent to this window procedure. - \****************************************************************/ + // + // Set button text to "Stop timer" and start the timer. For + // timers 1&2 we'll specify a WNDPROC ("TimerFunc") which will + // get called for each timer tick. For timers 3&4 we'll just + // have WM_TIMER messages sent to this window procedure. + // + SetWindowText ((HWND) lParam, (LPTSTR) "Stop timer"); if (id < ID_TIMER3) - { if (SetTimer (hwnd, (UINT) id, (UINT) (id * TIMERINTERVAL), - (WNDPROC) TimerFunc) == 0) - MessageBox (NULL, "MainWndProc(): SetTimer() failed", - "Err! - TIMERS", MB_OK | MB_ICONHAND); + { + if (SetTimer (hwnd, (UINT) id, (UINT) (id * TIMERINTERVAL), + (TIMERPROC) TimerFunc) == 0) + + MessageBox (NULL, (LPCTSTR) "MainWndProc(): SetTimer() failed", + (LPCTSTR) "Err! - TIMERS", MB_OK | MB_ICONEXCLAMATION); } else - { if (SetTimer (hwnd, (UINT) id, (UINT) (id * TIMERINTERVAL), + { + if (SetTimer (hwnd, (UINT) id, (UINT) (id * TIMERINTERVAL), NULL) == 0) - MessageBox (NULL, "MainWndProc(): SetTimer() failed", - "Err! - TIMERS", MB_OK | MB_ICONHAND); + + MessageBox (NULL, (LPCTSTR) "MainWndProc(): SetTimer() failed", + (LPCTSTR) "Err! - TIMERS", MB_OK | MB_ICONEXCLAMATION); } } else @@ -152,40 +171,51 @@ LONG APIENTRY MainWndProc (HWND hwnd, UI } break; } + case WM_TIMER: { - /******************************************************************\ - * Invert the flashRect associated with this timer event - \******************************************************************/ + // + // Invert the gaFlashRect associated with this timer event + // + HDC hdc = GetDC (hwnd); - InvertRect (hdc, &flashRects[wParam]); /* wParam = timerId */ - ReleaseDC (hwnd, hdc); + InvertRect (hdc, &gaFlashRects[wParam]); // wParam = timerId + ReleaseDC (hwnd, hdc); break; } + case WM_SIZE: - { int width = (int) LOWORD(lParam); + { + int width = (int) LOWORD(lParam); int height = (int) HIWORD(lParam); - /******************************************************************\ - * User resized the window, so reposition button & flashing rects - \******************************************************************/ + // + // User resized the window, so reposition button & flashing rects + // + SetButtonPositions (width, height); SetFlashRectPositions (width, height); break; } + case WM_PAINT: - { PAINTSTRUCT ps; + { + PAINTSTRUCT ps; BeginPaint (hwnd, &ps); DrawStuff (hwnd, ps.hdc); EndPaint (hwnd, &ps); break; } + case WM_DESTROY: + PostQuitMessage(NULL); break; + default: + return (DefWindowProc(hwnd, message, wParam, lParam)); } return (NULL); @@ -193,74 +223,72 @@ LONG APIENTRY MainWndProc (HWND hwnd, UI -/************************************************************************\ +/******************************************************************************\ * * FUNCTION: TimerFunc * * INPUTS: hwnd - window associated with timer -* wMsg - WM_TIMER message -* nIDEvent - timer's ID +* uMsg - WM_TIMER message +* idEvent - timer's ID * dwTime - current system time * * RETURNS: 0 * -* GLOBAL VARS: flashRects - array of flashing rectangles coordinates +* GLOBAL VARS: gaFlashRects - array of flashing rectangles coordinates * * LOCAL VARS: hdc - device context of main window * -* COMMENTS: Inverts the flashRect corresponding to nIDEvent +* COMMENTS: Inverts the gaFlashRect corresponding to idEvent * -\************************************************************************/ - -WORD APIENTRY TimerFunc (HWND hwnd, WORD wMsg, int nIDEvent, DWORD dwTime) -{ HDC hdc = GetDC (hwnd); +\******************************************************************************/ - InvertRect (hdc, &flashRects[nIDEvent]); - ReleaseDC (hwnd, hdc); - return 0; +void CALLBACK TimerFunc (HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime) +{ + HDC hdc = GetDC (hwnd); - UNREFERENCED_PARAMETER(wMsg); - UNREFERENCED_PARAMETER(dwTime); + InvertRect (hdc, &gaFlashRects[idEvent]); + ReleaseDC (hwnd, hdc); } -/************************************************************************\ +/******************************************************************************\ * * FUNCTION: SetButtonPositions * * INPUTS: cx - new width of main window * cy - new height of main window * -* GLOBAL VARS: hwndButtons - array of button HWNDs (unmodified) +* GLOBAL VARS: gahwndButtons - array of button HWNDs (unmodified) * * COMMENTS: Repositions buttons according to new window size (cx,cy) * -\************************************************************************/ +\******************************************************************************/ void SetButtonPositions (int cx, int cy) -{ SetWindowPos (hwndButtons[ID_TIMER1], NULL, /* upper-left button */ +{ + SetWindowPos (gahwndButtons[ID_TIMER1], NULL, // upper-left button BUTTONBORDER, cy/2 - BUTTONBORDER - BUTTONHEIGHT, cx/2 - 2*BUTTONBORDER, BUTTONHEIGHT, SWP_SHOWWINDOW); - SetWindowPos (hwndButtons[ID_TIMER2], NULL, /* upper-right button */ + SetWindowPos (gahwndButtons[ID_TIMER2], NULL, // upper-right button cx/2 + BUTTONBORDER, cy/2 - BUTTONBORDER - BUTTONHEIGHT, cx/2 - 2*BUTTONBORDER, BUTTONHEIGHT, SWP_SHOWWINDOW); - SetWindowPos (hwndButtons[ID_TIMER3], NULL, /* lower-left button */ + SetWindowPos (gahwndButtons[ID_TIMER3], NULL, // lower-left button BUTTONBORDER, cy - BUTTONBORDER - BUTTONHEIGHT, cx/2 - 2*BUTTONBORDER, BUTTONHEIGHT, SWP_SHOWWINDOW); - SetWindowPos (hwndButtons[ID_TIMER4], NULL, /* lower-right button */ + SetWindowPos (gahwndButtons[ID_TIMER4], NULL, // lower-right button cx/2 + BUTTONBORDER, cy - BUTTONBORDER - BUTTONHEIGHT, cx/2 - 2*BUTTONBORDER, @@ -270,57 +298,57 @@ void SetButtonPositions (int cx, int cy) -/************************************************************************\ +/******************************************************************************\ * -* FUNCTION: SetFlashRectPositions +* FUNCTION: SetgaFlashRectPositions * * INPUTS: cx - new width of main window * cy - new height of main window * -* GLOBAL VARS: flashRects - array of flashing rectangles coordinates +* GLOBAL VARS: gaFlashRects - array of flashing rectangles coordinates * (modified) * * COMMENTS: Repositions flashing rectangles according to new window * size (cx,cy) * -\************************************************************************/ +\******************************************************************************/ void SetFlashRectPositions (LONG cx, LONG cy) { /* upper-left rect */ - flashRects[ID_TIMER1].left = BUTTONBORDER; - flashRects[ID_TIMER1].top = 3*TEXTHEIGHT + BUTTONBORDER; - flashRects[ID_TIMER1].right = cx/2 - BUTTONBORDER; - flashRects[ID_TIMER1].bottom = cy/2 - BUTTONHEIGHT - 2*BUTTONBORDER; + gaFlashRects[ID_TIMER1].left = BUTTONBORDER; + gaFlashRects[ID_TIMER1].top = 3*TEXTHEIGHT + BUTTONBORDER; + gaFlashRects[ID_TIMER1].right = cx/2 - BUTTONBORDER; + gaFlashRects[ID_TIMER1].bottom = cy/2 - BUTTONHEIGHT - 2*BUTTONBORDER; /* upper-right rect */ - flashRects[ID_TIMER2].left = cx/2 + BUTTONBORDER; - flashRects[ID_TIMER2].top = 3*TEXTHEIGHT + BUTTONBORDER; - flashRects[ID_TIMER2].right = cx - BUTTONBORDER; - flashRects[ID_TIMER2].bottom = cy/2 - BUTTONHEIGHT - 2*BUTTONBORDER; + gaFlashRects[ID_TIMER2].left = cx/2 + BUTTONBORDER; + gaFlashRects[ID_TIMER2].top = 3*TEXTHEIGHT + BUTTONBORDER; + gaFlashRects[ID_TIMER2].right = cx - BUTTONBORDER; + gaFlashRects[ID_TIMER2].bottom = cy/2 - BUTTONHEIGHT - 2*BUTTONBORDER; /* lower-left rect */ - flashRects[ID_TIMER3].left = BUTTONBORDER; - flashRects[ID_TIMER3].top = cy/2 + 3*TEXTHEIGHT + BUTTONBORDER; - flashRects[ID_TIMER3].right = cx/2 - BUTTONBORDER; - flashRects[ID_TIMER3].bottom = cy - BUTTONHEIGHT - 2*BUTTONBORDER; + gaFlashRects[ID_TIMER3].left = BUTTONBORDER; + gaFlashRects[ID_TIMER3].top = cy/2 + 3*TEXTHEIGHT + BUTTONBORDER; + gaFlashRects[ID_TIMER3].right = cx/2 - BUTTONBORDER; + gaFlashRects[ID_TIMER3].bottom = cy - BUTTONHEIGHT - 2*BUTTONBORDER; /* lower-right rect */ - flashRects[ID_TIMER4].left = cx/2 + BUTTONBORDER; - flashRects[ID_TIMER4].top = cy/2 + 3*TEXTHEIGHT + BUTTONBORDER; - flashRects[ID_TIMER4].right = cx - BUTTONBORDER; - flashRects[ID_TIMER4].bottom = cy - BUTTONHEIGHT - 2*BUTTONBORDER; + gaFlashRects[ID_TIMER4].left = cx/2 + BUTTONBORDER; + gaFlashRects[ID_TIMER4].top = cy/2 + 3*TEXTHEIGHT + BUTTONBORDER; + gaFlashRects[ID_TIMER4].right = cx - BUTTONBORDER; + gaFlashRects[ID_TIMER4].bottom = cy - BUTTONHEIGHT - 2*BUTTONBORDER; } -/************************************************************************\ +/******************************************************************************\ * * FUNCTION: DrawStuff * * INPUTS: hwnd - main window handle * hdc - DC for main window * -* GLOBAL VARS: flashRects - array of flashing rectangles coordinates +* GLOBAL VARS: gaFlashRects - array of flashing rectangles coordinates * (not modified) * * LOCAL VARS: rect - client rectangle @@ -328,15 +356,17 @@ void SetFlashRectPositions (LONG cx, LON * * COMMENTS: Draws separators, "Timer #" headers, & rectangles * -\************************************************************************/ +\******************************************************************************/ void DrawStuff (HWND hwnd, HDC hdc) -{ RECT rect; +{ + RECT rect; HBRUSH hbr; - /**********************************************************************\ - * Draw the separator lines between the timer info "windows" - \**********************************************************************/ + // + // Draw the separator lines between the timer info "windows" + // + GetClientRect (hwnd, &rect); MoveToEx (hdc, rect.right/2, 0, NULL); LineTo (hdc, rect.right/2, rect.bottom); @@ -344,28 +374,30 @@ void DrawStuff (HWND hwnd, HDC hdc) MoveToEx (hdc, 0, rect.bottom/2, NULL); LineTo (hdc, rect.right, rect.bottom/2); - /**********************************************************************\ - * Draw the "Timer #" headers - \**********************************************************************/ + // + // Draw the "Timer #" headers + // + TextOut (hdc, BUTTONBORDER, TEXTHEIGHT, "Timer 1:", 8); TextOut (hdc, rect.right/2 + BUTTONBORDER, TEXTHEIGHT, "Timer 2:", 8); TextOut (hdc, BUTTONBORDER, rect.bottom/2 + TEXTHEIGHT, "Timer 3:", 8); TextOut (hdc, rect.right/2 + BUTTONBORDER, rect.bottom/2 + TEXTHEIGHT, "Timer 4:", 8); - /**********************************************************************\ - * Draw the rectangles to flash on timer messages - \**********************************************************************/ + // + // Draw the rectangles to flash on timer messages + // + hbr = CreateSolidBrush (0x0000ff); - FillRect (hdc, &flashRects[ID_TIMER1], hbr); + FillRect (hdc, &gaFlashRects[ID_TIMER1], hbr); DeleteObject (hbr); hbr = CreateSolidBrush (0x00ff00); - FillRect (hdc, &flashRects[ID_TIMER2], hbr); + FillRect (hdc, &gaFlashRects[ID_TIMER2], hbr); DeleteObject (hbr); hbr = CreateSolidBrush (0xff0000); - FillRect (hdc, &flashRects[ID_TIMER3], hbr); + FillRect (hdc, &gaFlashRects[ID_TIMER3], hbr); DeleteObject (hbr); hbr = CreateSolidBrush (0x2266aa); - FillRect (hdc, &flashRects[ID_TIMER4], hbr); + FillRect (hdc, &gaFlashRects[ID_TIMER4], hbr); DeleteObject (hbr); }