mfcsdi

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?


Solution

  • I think the correct answer here is that my code is old, and I need to create a new SDI project and graft my program onto it before going any further with this. Then I will see if I still have these problems.

    NEVER MIND! I am ashamed to report that the problem was coming from my own window sizing code that I wrote in CView::OnInitialUpdate() and forgot about long ago. SORRY!