MFC中如何将应用程序的配置信息保存到注册表中(二)
来源:互联网 发布:为知笔记 退出团队 编辑:程序博客网 时间:2024/05/21 10:34
在上一篇中介绍了几个写入注册表数据和读取注册表数据的接口,并介绍了使用方法。
这一片教你如何使得你的应用程序在下次打开时保持上一次关闭前的状态。
在上一篇添加的代码的基础上,要添加WM_CLOSE消息的响应函数,因为我们只有在窗口关闭前要保存窗口的位置信息,所以保存窗口位置到注册表的代码要写在这个消息处理函数。
代码如下:
void CTestClassDlg::OnClose() {if (AfxGetApp()->GetProfileInt("Settings", "SavePosition", 1)){//保存窗口的位置WINDOWPLACEMENT wp;GetWindowPlacement(&wp);AfxGetApp()->WriteProfileInt("Settings", "FrameStatus", wp.showCmd);AfxGetApp()->WriteProfileInt("Settings", "FrameTop", wp.rcNormalPosition.top);AfxGetApp()->WriteProfileInt("Settings", "FrameLeft", wp.rcNormalPosition.left);AfxGetApp()->WriteProfileInt("Settings", "FrameBottom", wp.rcNormalPosition.bottom);AfxGetApp()->WriteProfileInt("Settings", "FrameRight", wp.rcNormalPosition.right);}CDialog::OnClose();}
在注册表中保存有应用程序关闭前的位置信息,在下一次打开的时候我们就可以取得这些数据来使得应用程序的窗口显示出关闭前的样子。
在MFC中窗口的初始化的代码一般都添加在OnInitDialog()函数中。
代码如下:
BOOL CTestClassDlg::OnInitDialog(){CDialog::OnInitDialog();................................................................// TODO: Add extra initialization hereint s, t, b, r, l;if (AfxGetApp()->GetProfileInt("Settings", "SavePosition", 1)){// only restore if there is a previously saved positionif ( -1 != (s = AfxGetApp()->GetProfileInt("Settings", "FrameStatus", -1)) && -1 != (t = AfxGetApp()->GetProfileInt("Settings", "FrameTop", -1)) && -1 != (l = AfxGetApp()->GetProfileInt("Settings", "FrameLeft", -1)) && -1 != (b = AfxGetApp()->GetProfileInt("Settings", "FrameBottom", -1)) && -1 != (r = AfxGetApp()->GetProfileInt("Settings", "FrameRight", -1)) ) {WINDOWPLACEMENT wp;// restore the window's statuswp.showCmd = s;// restore the window's width and heightwp.rcNormalPosition.bottom = b;wp.rcNormalPosition.right = r;// the following correction is needed when the taskbar is// at the left or top and it is not "auto-hidden"RECT workArea;SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);l += workArea.left;t += workArea.top;// make sure the window is not completely out of sightint max_x = GetSystemMetrics(SM_CXSCREEN) -GetSystemMetrics(SM_CXICON);int max_y = GetSystemMetrics(SM_CYSCREEN) -GetSystemMetrics(SM_CYICON);wp.rcNormalPosition.top = min(t, max_y);wp.rcNormalPosition.left = min(l, max_x);SetWindowPlacement(&wp);}}return TRUE; // return TRUE unless you set the focus to a control}
运行应用程序
然后调整一下窗口的大小和位置:
关闭程序,再次打开你就会发现应用程序的窗口大小和位置和上次的是一样的。你可以测试一下!
下面是注册表的变化截图:
写的不是很详细,但是这些东西都是一层窗户纸的东西。没有必要深究,也没有什么可深究的。重要的是会用就可以了。
为了方便以后再次用到时快速的复习,也为了帮助初学的人,做了上面的简单整理!
下面是一些参考代码,可以借鉴一下!
void CMainFrame::ActivateFrame(int nCmdShow) {if (m_bFirst){m_bFirst = FALSE;WINDOWPLACEMENT* pWndpl = new WINDOWPLACEMENT;pWndpl->length = sizeof(WINDOWPLACEMENT);CWinApp* pApp = AfxGetApp();//恢复窗口位置pWndpl->flags = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("FLAGS"), 0);pWndpl->showCmd = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("SHOWCMD"), 0);nCmdShow = pWndpl->showCmd;pWndpl->ptMinPosition.x = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("MINX"), 0);pWndpl->ptMinPosition.y = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("MINY"), 0);pWndpl->ptMaxPosition.x = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("MAXX"), 0);pWndpl->ptMaxPosition.y = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),_T("MAXY"), 0);pWndpl->rcNormalPosition.top = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("TOP"), 0);pWndpl->rcNormalPosition.left = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("LEFT"), 0);pWndpl->rcNormalPosition.right = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("RIGHT"), 0);pWndpl->rcNormalPosition.bottom = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("BOTTOM"), 0);//设置窗口位置SetWindowPlacement(pWndpl);delete pWndpl;}CFrameWnd::ActivateFrame(nCmdShow);}void CMainFrame::OnClose() {WINDOWPLACEMENT* pWndpl = new WINDOWPLACEMENT;pWndpl->length = sizeof(WINDOWPLACEMENT);//获得窗口位置GetWindowPlacement(pWndpl);CWinApp* pApp = AfxGetApp();//保存窗口位置pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("FLAGS"), pWndpl->flags);pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("SHOWCMD"), pWndpl->showCmd);pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MINX"), pWndpl->ptMinPosition.x);pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MINY"), pWndpl->ptMinPosition.y);pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MAXX"), pWndpl->ptMaxPosition.x);pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MAXY"), pWndpl->ptMaxPosition.y);pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("TOP"), pWndpl->rcNormalPosition.left);pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("LEFT"), pWndpl->rcNormalPosition.top);pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("RIGHT"), pWndpl->rcNormalPosition.right);pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("BOTTOM"), pWndpl->rcNormalPosition.bottom);delete pWndpl;CFrameWnd::OnClose();}
0 0
- MFC中如何将应用程序的配置信息保存到注册表中(二)
- MFC中如何将应用程序的配置信息保存到注册表中(一)
- Enterprise Library 2.0 技巧(2):如何将配置信息保存到数据库中
- 关闭对话框重启还保留着上次的数据--MFC中如何把数据保存到注册表
- 将应用配置信息写在注册表中
- 将Ubuntu终端的输出信息保存到log中
- 将登陆成功后的信息保存到session中
- MFC 将信息写入文件中,保存信息
- C#中如何将信息保存到txt文件中去,以及如何读取
- Android将应用程序的崩溃信息如何保存到本地文件,并上传服务器
- C++ (MFC)将字符串保存到指定目录下的文本中
- VC6的include配置信息保存在系统的注册表中
- Java:将异常的完整堆栈追踪信息保存到字符串中(详解)
- android中如何将下载的图片保存到手机SD(外部存储)卡中
- 如何将自己的应用程序编译到uCLinux/Linux中
- 如何将exception的stacktrace信息提取到字符串中
- MFC将视图文件保存到PPT中
- MFC将视图文件保存到PPT中
- ADB server didn't ACK 默认的ADB端口被占用之解决办法
- HDU 1597 find the nth digit && HDU 2141 Can you find it?
- 输入10个整型整数,用选择法对这10个数排序,并该由大到小顺序在屏幕上输出。
- java把string.xml转化成excel文件
- [Redis] redis-cli 命令总结
- MFC中如何将应用程序的配置信息保存到注册表中(二)
- 黑马程序员_入学测试题详解
- 输入10个整型整数,用起泡法对这10个数排序,并该由小到大顺序在屏幕上输出
- <%%>与<scriptrunat=server>,<%=%>与<%#%>的区别
- myibatis--增删查改
- poj 3134 Power Calculus(IDA*)
- IIS7.5精细控制web访问权限
- vlc-nox创建的文件
- (《数论及应用1.3》NEFU 116 两仪剑法(最小公倍数&&最大公约数))