1 //在应用程序app类中,写入以下代码
BOOL CMyMDIApp::InitInstance(){
//...............
// 主窗口已初始化,因此显示它并对其进行更新
HKEY key;
DWORD keyType=REG_BINARY;
DWORD keySize=sizeof(WINDOWPLACEMENT);
WINDOWPLACEMENT wininfo;
long regInfo;
CString appKey=_T("software\\wolfeLee\\saveWinState\\settings");
CString keyName=_T("WinState");
if( RegOpenKeyEx( HKEY_CURRENT_USER, appKey,0, KEY_READ, &key)
== ERROR_SUCCESS){
regInfo=RegQueryValueEx(key,keyName,0, &keyType,(LPBYTE)&wininfo, &keySize);
RegCloseKey(key);
}
if(regInfo==ERROR_SUCCESS){
m_pMainWnd->SetWindowPlacement(&wininfo);
}else{
//按原来默认方式显示
pMainFrame->ShowWindow(m_nCmdShow);
//pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
}
pMainFrame->UpdateWindow();
return TRUE;
}
2 //在窗口框加 WM_Close 消息中,加入处理函数
void CMainFrame::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
HKEY key;
CString appKey=_T("software\\wolfeLee\\saveWinState\\settings");
CString keyName=_T("WinState");
if( RegOpenKeyEx( HKEY_CURRENT_USER, appKey,0, KEY_WRITE, &key)
!= ERROR_SUCCESS){
DWORD dispotion;
RegCreateKeyEx(HKEY_CURRENT_USER, appKey,0,_T(""),
REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&key,&dispotion);
}
WINDOWPLACEMENT wininfo;
GetWindowPlacement( &wininfo);
RegSetValueEx(key, keyName,0, REG_BINARY,(LPBYTE)&wininfo, sizeof(WINDOWPLACEMENT));
RegCloseKey(key);
CMDIFrameWnd::OnClose();
}
窗口状态写入注册表
最新推荐文章于 2021-07-22 11:23:18 发布