我已经实现了保存和加载主窗口大小和位置的代码,它工作正常,但是,当我打开一个文档时,它会将窗口大小更改回某个内部默认值。
这些是我看到的来自 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();
}
如果我打开文档时窗口大小和位置保持不变,我希望它。我怎样才能做到这一点?