A
A
anonymous2017-02-17 21:15:36
C++ / C#
anonymous, 2017-02-17 21:15:36

WinAPI WM_TIMER WM_PAINT how to make a traffic light?

I’m starting to get acquainted with WinAPI, I need to do something like a traffic light (three gray squares and one of them should change color in turn) I don’t know what condition to make, who will help me figure it out?
I understand that in WM_TIMER you need to insert what is in WM_PAINT, but how to prescribe - xs

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;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anonymouss, 2017-02-17
@anonymouss

I solved the problem so
if anyone has 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;
}

J
jcmvbkbc, 2017-02-17
@jcmvbkbc

I don’t know what condition to make, who will help to figure it out?

The state machine will help you. See _
And, by the way, in WM_PAINT BeginPaing lost.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question