question

WilliamDotson-2026 avatar image
0 Votes"
WilliamDotson-2026 asked RLWA32-6355 commented

MFC SDI OpenDocument() is resetting my main window size. How can I stop it?

I have implemented code to save and load the main window size and location, which works fine, however, when I open a document it changes the window size back to some internal default.

These are the calls from Windows I see leading up to when the change happens:

 CSingleDocTemplate::OpenDocumentFile()
 CFrameWnd::InitialUpdateFrame()
 CWnd::SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE)

This is my code for saving and loading the window info:

 BOOL CDisplayApp::InitInstance()
 {
 // existing code .....
     LONG Ret;
     HKEY RegistryKey;
     DWORD type = REG_BINARY;
     WINDOWPLACEMENT sWP;
     DWORD sizewp = sizeof(WINDOWPLACEMENT);
    
     Ret = RegOpenKeyEx(
         HKEY_CURRENT_USER,
         _T("SOFTWARE\\Local AppWizard-Generated Applications\\display\\PreservedWindowPos"),
         0,
         KEY_READ,
         &RegistryKey);
    
     if (Ret == ERROR_SUCCESS) {
         Ret = ::RegQueryValueEx(RegistryKey,
             _T("PosAndSize"),
             0,
             &type,
             (LPBYTE)&sWP,
             &sizewp);
    
         if (Ret != ERROR_SUCCESS)
             m_pMainWnd->ShowWindow(SW_SHOW);
         else
             m_pMainWnd->SetWindowPlacement(&sWP);
     }
 }
    
    
 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
 {
 // existing code .....
     this->LoadBarState(_T("MainToolBar"));
 }
    
    
 void CMainFrame::OnClose()
 {
     // TODO: Add your message handler code here and/or call default
    
        LONG Ret;
     HKEY Registry_Key;
     DWORD disposition;
     WINDOWPLACEMENT sWindow_Position;
    
     SaveBarState(_T("MainToolBar"));
    
     Ret = RegOpenKeyEx(
         HKEY_CURRENT_USER,
         _T("SOFTWARE\\Local AppWizard-Generated Applications\\display\\PreservedWindowPos"),
         NULL,
         KEY_WRITE,
         &Registry_Key);
    
     if (Ret != ERROR_SUCCESS)
     {
         RegCreateKeyEx(
             HKEY_CURRENT_USER,
             _T("SOFTWARE\\Local AppWizard-Generated Applications\\display\\PreservedWindowPos"),
             NULL,
             NULL,
             REG_OPTION_NON_VOLATILE,
             KEY_ALL_ACCESS,
             NULL,
             &Registry_Key,
             &disposition);
     }
    
     GetWindowPlacement(&sWindow_Position);
    
     RegSetValueEx(
         Registry_Key,
         _T("PosAndSize"),
         NULL,
         REG_BINARY,
         (BYTE*)&sWindow_Position,
         sizeof(WINDOWPLACEMENT));
     RegCloseKey(Registry_Key);
    
     CFrameWnd::OnClose();
 }

I would like it if the window size and location were to stay the same when I open documents. How can I do that?


c++
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The SDI application created by the MFC application template does not exhibit the described behaviors. If the application's frame window is resized/repositioned there is no change when opening a new document. And MFC automatically saves/restores window placement when exiting/restarting the application.

Did you use the new application template to create the SDI application? Are you working with old code?

0 Votes 0 ·

It seems to work. Maybe it depends on some details of your SDI program. Do you have a simple project (on OneDrive, GitHub, etc.) that demonstrates the problem?


0 Votes 0 ·

1 Answer

WilliamDotson-2026 avatar image
0 Votes"
WilliamDotson-2026 answered RLWA32-6355 commented

It is old code, originally created under MS C++ V6. I compiled it under VS2005, 2010, and now 2019. Each time the IDE "converted" the project to the new IDE. But does that update the application template? If not, how would I do that?

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

When Visual Studio upgrades a project that was created by an older version it does not change the source code. It updates the solution/project files. In many cases old MFC code can be compiled and linked successfully by current versions of Visual Studio. However, an MFC SDI application created by the MFC Application template in VS2019 will use different classes and contain implementations that differ from old code that has been carried forward. For example, application classes will be derived from CWinAppEx not CWinApp, the frame window will be derived from CFrameWndEx not CFrameWnd, and so forth. Consequently the generated implementations of functions like InitInstance will contain different code.

Depending on the particulars of your own application you may be able to use VS2019 to create a new MFC SDI application and then graft your existing code into it. Of course you would want to exclude anything you added to the old code that duplicates functionality provided by default (like state save/restore) in the app created by VS2019.

0 Votes 0 ·

Thank you, that all makes perfect sense!
But can you point me to any resource that might help me identify any calls in my code (not the MFC generated code) that might have changed?
My application is quite large, so I cannot rewrite it, but I have not had to modify any of the MFC generated files.

0 Votes 0 ·

I used VS2005 to generate an MFC SDI application. Then I opened the project created by VS2005 in VS2019. After the project was upgraded by VS2019 it compiled, linked and the MFC SDI application executed successfully. Then I edited the code to add the save/restore modifications noted in your initial question. After rebuilding, the application executed as expected with window state saved when exiting and restored when restarting. Opening a new document after repositioning/resizing the window did not exhibit the described issue.

If you can share a sample that reproduces the resizing/repositioning issue the community may be able to help resolve that specific issue.

0 Votes 0 ·