1

我已经实现了保存和加载主窗口大小和位置的代码,它工作正常,但是,当我打开一个文档时,它会将窗口大小更改回某个内部默认值。

这些是我看到的来自 Windows 的调用,导致更改发生:

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

这是我保存和加载窗口信息的代码:

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();
}

如果我打开文档时窗口大小和位置保持不变,我希望它。我怎样才能做到这一点?

4

1 回答 1

0

我认为这里的正确答案是我的代码很旧,我需要创建一个新的 SDI 项目并将我的程序移植到它上面,然后再进一步处理。然后我会看看我是否还有这些问题。

没关系!我很惭愧地报告问题来自我自己在 CView::OnInitialUpdate() 中编写的窗口大小调整代码,并且很久以前就忘记了。对不起!

于 2021-10-25T02:17:59.547 回答