빙수달 게임 개발 노트

[WindowAPI] 키보드 입력 사각형 처리 본문

Programming/WindowAPI

[WindowAPI] 키보드 입력 사각형 처리

빙수달 2025. 1. 9. 00:17

윈도우 프로시저 내부만 작성하였다.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    HBRUSH hBrush, oldBrush;

    static POINT pos;
    static RECT rectView;
    static bool flag_East;
    static bool flag_South;
    static bool flag_West;
    static bool flag_North;
    
    switch (message)
    {
    case WM_CREATE:
    {
        pos.x = 50; pos.y = 50;
        
    }
    break;
    case WM_PAINT:
        {
        hdc = BeginPaint(hWnd, &ps);
       

        Rectangle(hdc,100,0,150,100);
        Rectangle(hdc, 150, 100, 200, 200);
        Rectangle(hdc, 100, 200, 150, 300);
        Rectangle(hdc, 50, 100, 100, 200);

        TextOut(hdc, 110, 50, _T("위쪽"), 2);
        TextOut(hdc, 151, 140, _T("오른쪽"), 3);
        TextOut(hdc, 60, 140, _T("왼쪽"), 2);
        TextOut(hdc, 101, 250, _T("아래쪽"), 3);

        hBrush = CreateSolidBrush(RGB(255, 0, 0));
        oldBrush = (HBRUSH)SelectObject(hdc, hBrush);
        if (flag_East)
        {
            Rectangle(hdc, 150, 100, 200, 200);
        }
        if (flag_South)
        {
            Rectangle(hdc, 100, 200, 150, 300);
        }
        if (flag_West)
        {
            Rectangle(hdc, 50, 100, 100, 200);
        }
        if (flag_North)
        {
            Rectangle(hdc, 100, 0, 150, 100);
        }

        

        EndPaint(hWnd, &ps);
        break;
        }
        
  
    case WM_CHAR:
        {
        hdc = BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        break;
        }
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        break;
    }

    case WM_KEYUP:
    {
        flag_East = false;
        flag_South = false;
        flag_West = false;
        flag_North = false;

        InvalidateRgn(hWnd, NULL, TRUE);
        break;
    }

    case WM_KEYDOWN:
    {
        if (wParam == VK_RIGHT)
        {
            flag_East = true;
        }
        if (wParam == VK_DOWN)
        {
            flag_South = true;
        }
        if (wParam == VK_LEFT)
        {
            flag_West = true;
        }
        if (wParam == VK_UP)
        {
            flag_North = true;
        }
        InvalidateRect(hWnd, NULL, TRUE);
        break;
    }
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

아무 입력이 없었을 때
해당 화살표를 눌렀을 때, 입력 처리가 된다.

'Programming > WindowAPI' 카테고리의 다른 글

[WindowAPI] 알카노이드(Alkanoid) / 벽돌깨기 만들기  (0) 2025.01.09
[WindowAPI] 격자 그리기  (0) 2025.01.08
[WindowAPI] 원 그리기  (1) 2025.01.08