What's the logical difference between PostQuitMessage() and DestroyWindow()?

前端 未结 3 529
暗喜
暗喜 2021-01-01 17:56

In my demo kinda app

case WM_CLOSE:
    DestroyWindow(hndl);
    return 0;

and

case WM_CLOSE:
    PostQuitMessage(0);
    r         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 18:49

    Neither snippet is correct. The first one will do what the default window procedure already does when it processes the WM_CLOSE message so is superfluous. But doesn't otherwise make the application quit, it should keep running and you'd normally have to force the debugger to stop with Debug + Stop Debugging. If you run it without a debugger then you'll leave the process running but without a window so you can't tell it is still running. Use Taskmgr.exe, Processes tab to see those zombie processes.

    The second snippet will terminate the app but will not clean up properly since you don't pass the WM_CLOSE message to the default window procedure. The window doesn't get destroyed. Albeit that the operating system will clean up for you so it does all come to a good end, just without any bonus points for elegance.

    The proper way to do it is to quit when your main window is destroyed. You'll know about it from the WM_DESTROY notification that's sent when that happens:

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    

提交回复
热议问题