在Win32窗口中处理SDL事件

问题描述 投票:2回答:1

我在Win32窗口中无法处理SDL事件。我制作了一个小程序来显示洋红色背景。基本上,当用户按下“ q”键时,背景色应从品红色变为红色,反之亦然。

sdlwin.c

#include <windows.h>

#include <SDL2/SDL.h>

const int win_width = 500;
const int win_height = 450;

int b = 0;

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static SDL_Window *sdlWnd;
    SDL_Surface *surface;

    switch (msg)
    {
    case WM_CREATE:
        if(SDL_Init(SDL_INIT_VIDEO) < 0)
        {
            MessageBox(hWnd, TEXT("SDL cannot be initialized!"), TEXT("Error!"),
                MB_ICONEXCLAMATION | MB_OK);
            PostQuitMessage(0);
        }

        sdlWnd = SDL_CreateWindowFrom(hWnd);

        if (sdlWnd == NULL)
        {
            MessageBox(hWnd, TEXT("SDL window cannot be created!"), TEXT("Error!"),
                MB_ICONEXCLAMATION | MB_OK);
            SDL_Quit();
            PostQuitMessage(0);
        }

        SDL_SetWindowTitle(sdlWnd, "SDL Window");

        break;
    case WM_PAINT:
        surface = SDL_GetWindowSurface(sdlWnd);
        SDL_FillRect(surface, &surface->clip_rect, b ? 0xFFFF00FF : 0xFFFF0000);
        SDL_UpdateWindowSurface(sdlWnd);
        break;
    case WM_CLOSE:
        SDL_DestroyWindow(sdlWnd);
        SDL_Quit();
        DestroyWindow(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    const TCHAR szClassName[] = TEXT("MyClass");

    WNDCLASS wc;
    HWND hWnd;
    MSG msg;

    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = szClassName;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);

    if (!RegisterClass(&wc))
    {
        MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"),
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hWnd = CreateWindow(szClassName,
        TEXT("SDLWin"),
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, win_width, win_height,
        NULL, NULL, hInstance, NULL);

    if (hWnd == NULL)
    {
        MessageBox(NULL, TEXT("Window Creation Failed!"), TEXT("Error!"),
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    SDL_Event e;

    while (GetMessage(&msg, NULL, 0, 0))
    {
        while (SDL_PollEvent(&e) != 0)
        {
            if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_q)
            {
                b = !b;
                //MessageBox(NULL, TEXT("bar"), TEXT("foo"), MB_ICONEXCLAMATION | MB_OK);
            }
        }

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

当我取消注释此行MessageBox(NULL, TEXT("bar"), TEXT("foo"), MB_ICONEXCLAMATION | MB_OK);时,按“ q”键时窗口没有响应,但是如果添加此行,则关闭消息框后窗口背景颜色会更改。我不确定为什么会发生这种奇怪的行为。我的问题是如何在Win32窗口中正确处理SDL事件。任何形式的帮助都将受到赞赏。

c winapi event-handling sdl sdl-2
1个回答
0
投票

您需要调用InvalidateRect(hWnd,NULL,NULL);更改后b。这将发布WM_Paint消息供您的窗口重绘。

© www.soinside.com 2019 - 2024. All rights reserved.