I have an MFC app, it got the Document/View architecture, but set to use only a single view. So far so good.
I was loading a file in the document and thought that the best way to notify the view of the new data was to call UpdateAllViews() when the file finished loading. It works good; I can transfer the data from the document to the view via the OnUpdate() function in the view.
However, right after OnUpdate() is called OnSize() is also called. This wouldn’t be a problem if it wasn’t for the fact that it’s called with the size the window had when it was created. So if I start the program, resize the window and then open a file, the window will pop back to the size it had initially! If I maximize the window and then call UpdateAllViews() it will resize while still thinking it’s maximized so I have to restore it before I can even change the size of it again. This is highly annoying and I wonder if there is any way to go around it? Maybe I can’t even use UpdateAllViews() in this way?
Ok so it turned out that this wasn’t due to the
UpdateAllViews(). This actually happens whenever you open a document in MFC. A lot of stuff happens when you do this and among that apparently a resize-command is sent.This resize command will resize the view to the size of its “document” (not to be confused with the document in the document/view architecture). In my case (as I was basing my view on a
CFormView), the document size was the size of the form.The form is not by default resized when you resize the window, which I guess is kind of logical, so whenever a “refresh” of the view happens it will size itself to fit its form.
I solved this by calling
SetScaleToFitSize()in theOnSize()function of my view, which results in a resize of not only the view but the form itself whenever the view is resized. As the form now always has the same size as the view, the size update that is sent whenever a file is opened has no effect.