简体   繁体   中英

WM_PAINT based on button click

I am trying to write a windowprocedure that would call the animation of a rectangle in the window only when the start button is clicked and stop when the stop button is clicked.

I tried doing this like this:

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_COMMAND:
        switch (wParam)
        {
        case BUTTON_START:
            stopClicked = false;
            DestroyWindow(hStartButton);
            CreateStopButton(hWnd);
            Animate(hWnd);
            return 0;
        case BUTTON_STOP:
            stopClicked = true;
            DestroyWindow(hStopButton);
            CreateStartButton(hWnd);
            return 0;
        }
    case WM_CREATE:
        AddMenus(hWnd);
        CreateStartButton(hWnd);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    default:
        return DefWindowProcW(hWnd, msg, wParam, lParam);
    }
    return 0;
}

the Animate function:

void Animate(HWND hWnd)
{
    HDC hdcWnd = GetDC(hWnd);

    while(!stopClicked)
    {
        //drawing code
    }
    ReleaseDC(hWnd, hdcWnd);
    DeleteDC(hdcWnd);
}

The program crashes as it never exist the while(!stopClicked) loop.

My question is how to make that possible that the animation would stop on a button click?

Your application hanged, bucause you are waiting for a flag to change and there is no way it will change.

WindowProcedure is called on an event, and until you leave it, any other event won't be processed.

What you need to do is to perform steps of animation on timer. You need to setup a timer which will send you an event which you have to handle and there you can draw next frame of your animation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM