title: Prompt user to save changes when closing MFC view
body: <p>I have an MFC MDI application, in which it is possible for documents to have multiple views, and for users to customise and then save layout data for the views. This data is associated with the views, not the documents.</p>
  
  <p>I would like to prompt the users to save if they choose to close a view with unsaved layout changes, and am running into problems, as it seems MFC is only geared towards changes in the document. Here are some approaches I've tried:</p>
  
  <ol>
  <li><p>Override CDocument::SaveModified function, which gets called by the framework when a document is closed. In this function, I send a message to all the document's views, which can then check for unsaved changes and prompt the user. </p></li>
  <li><p>Perform the check inside the View's destructor.</p></li>
  <li><p>Perform the check inside the View's OnClose handler</p></li>
  </ol>
  
  <p>Each of these approaches has problems. (1) is the best, but it fails to deal with cases where there are several views onto one document, and the user closes one of the views. As the document is still open, SaveModified is not called. </p>
  
  <p>The problem with (2) is that on application shutdown, the application has already disappeard by the time any CView destrutors are called. This can leave an orphan dialog box open on the desktop. This is also the case if I try performing the check inside OnDestroy.</p>
  
  <p>I can't get (3) to work - I can't get my views to respond to WM_CLOSE.</p>
  
  <p>Currently, my best solution is to do both (1) and (2), but this requires some smelly logic to prevent the app from prompting the user to save view changes twice on app shutdown.</p>
  
  <p>Anyone know of a better way of doing this? Where is the correct place to hook in?</p>