10.9.6 Adding WM_QUERYENDSESSION and WM_CLOSE Cases

You must process the WM_QUERYENDSESSION and WM_CLOSE messages to prevent the contents of your files from being lost when the user closes a file or ends a session. Add the following statements to the window procedure:

case WM_QUERYENDSESSION:       /* message: to end the session? */
    return (QuerySaveFile(hWnd));

case WM_CLOSE:                 /* message: close the window    */
    if (QuerySaveFile(hWnd))
        DestroyWindow(hWnd);
    break;

Windows sends a WM_QUERYENDSESSION message to the window procedure when the user has chosen to exit Windows. The session ends only if TRUE is returned. The QuerySaveFile function checks for changes to the file, saves them if the user requests they be saved, and returns TRUE or FALSE depending on whether the user canceled or confirmed the operation.

Windows sends the WM_CLOSE message to the window procedure when the user chooses the Close command in the main window's System menu. QuerySaveFile carries out the same task as in WM_QUERYENDSESSION, but to complete the WM_CLOSE case, the application must also destroy the main window by using the DestroyWindow function.