WM 关闭

单击应用程序的关闭按钮时发送。不要将此与 WM_DESTROY 混淆,WM_DESTROY 会在窗口被破坏时发送。主要的区别在于可以在 WM_CLOSE 中取消关闭(想想 Microsoft Word 要求保存你的更改),而不是在窗口已经关闭的情况下销毁(想想 Microsoft Word 释放内存)。

LRESULT CALLBACK winproc(HWND hwnd, UINT wm, WPARAM wp, LPARAM lp)
{
    static char *text;
    switch (wm) {
        case WM_CREATE:
            text = malloc(256);
            /* use the allocated memory */
            return 0;
        case WM_CLOSE:
             switch (MessageBox(hwnd, "Save changes?", "", MB_YESNOCANCEL)) {
                 case IDYES:
                      savedoc();
                                         /* fall through */
                 case IDNO:
                      DestroyWindow(hwnd);
                      break;
             }
             return 0;
        case WM_DESTROY:
            /* free the memory */
            free(text);
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, wm, wp, lp);
}