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

William Dotson 1 Reputation point
2021-10-24T05:24:13.823+00:00

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++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,516 questions
{count} votes

1 answer

Sort by: Most helpful
  1. William Dotson 1 Reputation point
    2021-10-24T19:42:59.457+00:00

    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?