简体   繁体   English

WM_PAINT基于按钮单击

[英]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: Animate功能:

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. 该程序由于不存在while(!stopClicked)循环while(!stopClicked)崩溃。

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. 在事件上调用WindowProcedure ,直到您离开它,其他任何事件都不会被处理。

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. 您需要设置一个计时器,该计时器将向您发送一个必须处理的事件,您可以在其中绘制动画的下一帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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