Add the WM_QUERYENDSESSION and WM_CLOSE Cases

You need to 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 function:

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 function 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 desired, and returns TRUE or FALSE depending on whether the user canceled or confirmed the operation.

Windows sends the WM_CLOSE message to the window function when the user has chosen the Close command in the main window's system menu. The QuerySaveFile function carries out the same task as in the WM_QUERYENDSESSION message, but in order to complete the WM_CLOSE case, you must also destroy the main window by using the DestroyWindow function.