Starting acquaintance with WinAPI need to do something like a traffic light (three gray squares and one of them should change color in order) I don’t know what condition will help someone to understand?

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static RECT rect1 = { 20,20,200,200 }; static RECT rect2 = { 230,20,420,200 }; static RECT rect3 = { 450,20,640,200 }; static INT id = 0; switch (message) { case WM_COMMAND: { int wmId = LOWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_TIMER: { }break; case WM_PAINT: { id = SetTimer(hWnd, TIMERid, 1000, NULL); PAINTSTRUCT ps; HBRUSH hbr = CreateSolidBrush(RGB(169, 169, 169));//grey FillRect(GetDC(hWnd), &rect1, hbr); FillRect(GetDC(hWnd), &rect2, hbr); FillRect(GetDC(hWnd), &rect3, hbr); EndPaint(hWnd, &ps); } break; case WM_DESTROY: KillTimer(hWnd, TIMERid); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } 

    1 answer 1

    so decided If there are other options - write

     LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static RECT rect1 = { 20,20,200,200 }; static RECT rect2 = { 230,20,420,200 }; static RECT rect3 = { 450,20,640,200 }; static INT id = 0; static INT timer = 0; switch (message) { case WM_LBUTTONDOWN: { } case WM_COMMAND: { int wmId = LOWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_TIMER: { HBRUSH hbrGrey = CreateSolidBrush(RGB(169, 169, 169));//grey HBRUSH hbrRed = CreateSolidBrush(RGB(255, 0, 0)); HBRUSH hbrGreen = CreateSolidBrush(RGB(0, 255, 0)); HBRUSH hbrYellow = CreateSolidBrush(RGB(255, 255, 0)); switch (timer) { case 1: { FillRect(GetDC(hWnd), &rect1, hbrRed); FillRect(GetDC(hWnd), &rect2, hbrGrey); FillRect(GetDC(hWnd), &rect3, hbrGrey); }break; case 2: { FillRect(GetDC(hWnd), &rect1, hbrGrey); FillRect(GetDC(hWnd), &rect2, hbrYellow); FillRect(GetDC(hWnd), &rect3, hbrGrey); }break; case 3: { FillRect(GetDC(hWnd), &rect1, hbrGrey); FillRect(GetDC(hWnd), &rect2, hbrGrey); FillRect(GetDC(hWnd), &rect3, hbrGreen); }break; default: break; } if (timer >= 3) timer = 0; timer++; //TCHAR buff[10] = TEXT(""); //wsprintf(buff, TEXT("%d"), timer++); //SetWindowText(hWnd, buff); }break; case WM_PAINT: { id = SetTimer(hWnd, TIMERid, 750, NULL); PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); ReleaseDC(hWnd, hdc); EndPaint(hWnd, &ps); } break; case WM_DESTROY: KillTimer(hWnd, TIMERid); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }