Win32 API WM_CLOSE

示例

单击应用程序的关闭按钮时发送。请勿将其与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);
}