VC++ MFC中如何将应用程序的配置信息保存到注册表中(二)
在上一篇中介绍了几个写入注册表数据和读取注册表数据的接口,并介绍了使用方法。
这一片教你如何使得你的应用程序在下次打开时保持上一次关闭前的状态。
在上一篇添加的代码的基础上,要添加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 here
int s, t, b, r, l; if (AfxGetApp()->GetProfileInt("Settings", "SavePosition", 1))
{
// only restore if there is a previously saved position
if ( -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 status
wp.showCmd = s; // restore the window's width and height
wp.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 sight
int 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();
}
VC++ MFC中如何将应用程序的配置信息保存到注册表中(二)的更多相关文章
- java如何操作注册表(Preferences类)(在windows的注册表中保存、读取)
我们经常需要将我们的程序运行中的一些信息(比如在选项对话框中的设置)记录下来,以做便再次运行的时候不用再重写填写这些数据.这对改善软件的人机可用性方面是很有用的.比如:数据库监控.日志工具,JDBMo ...
- 在注册表中查看Windows10系统激活密钥的方法
1 2 3 4 5 6 7 分步阅读 百度经验:jingyan.baidu.com 激活Windows10系统(非自己使用激活密钥激活的系统)以后,我们不一定清楚激活密钥是什么.如果想查看自己电脑 ...
- VC++ MFC单文档应用程序SDI下调用glGenBuffersARB(1, &pbo)方法编译通过但执行时出错原因分析及解决办法:glewInit()初始化的错误
1.问题症状 在VC++环境下,利用MFC单文档应用程序SDI下开发OpenGL程序,当调用glGenBuffersARB(1, &pbo)方法编译通过但执行时出错,出错代码如下: OpenG ...
- oracle 11g在安装过程中出现监听程序未启动或数据库服务未注册到该监听程序
15511477451 原文 oracle 11g在安装过程中出现监听程序未启动或数据库服务未注册到该监听程序? 环境:win7 64位系统.oracle11g数据库 问题描述:在win7 64位系统 ...
- 【转】C#程序打包安装部署之添加注册表项
今天为大家整理了一些怎样去做程序安装包的具体文档,这些文档并不能确保每个人在做安装包的时候都能正确去生成和运行,但是这些文档的指导作用对于需要的朋友来说还是很有必要的,在实际产品的安装部署过程中可能有 ...
- NSIS:在注册表中记录安装路径以便重装或升级时读取
原文 NSIS:在注册表中记录安装路径以便重装或升级时读取 在NSIS中,这个功能是非常有用的,可以避免用户把程序安装到多个位置的尴尬. 第1步:在“安装目录选择页面”前面加入以下代码: 1 !def ...
- 如何完全卸载oracle和删除oracle在注册表中的注册信息
卸载步骤介绍 1.停止所有Oracle相关的服务 操作方法: 控制面板-->管理工具 -->服务 -->将所有oracle开头的服务均停止 2.卸载Oracle 10g数据库服务器组 ...
- 应用SharedPreference保存程序的配置信息
SharedPreference: 1.用来保存应用程序的配置信息的XML文件,内部的数据形式为键值对 2.一般存在于/data/data/<包名>shared_prefs目录下 3.该对 ...
- C#实现在注册表中保存信息
C#实现在注册表中保存信息 最近做的项目需要在注册表中记录一些用户设置,方便在程序下次启动时读取设置,应用上次用户保存的设置,挺简单的. 写出来,方便记忆,以后要用,可以直接改改就能用. 1 usin ...
随机推荐
- 第七课第六节,T语言流程语句( 版本5.0)
select语句 seelct语句,可以说是if语句的升级版,当我们的if语句嵌套太多,或者if语句判断太多的时候,都可以考虑使用选择语句 在多条件判断的时候,选择语句的执行速度比多个if语句要快而且 ...
- codeforces 381 D Alyona and a tree(倍增)(前缀数组)
Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- boost -- scoped_lock V.S. mutex lock/unlock —— why scoped_lock is recommanded?
why scoped_lock is recommanded? 其实,这和RAII idiom变流行是一样的原因:因为你可以确保任何情况下离开执行范围都会解锁mutex. 注意,这不仅仅是说你可能忘记 ...
- 基于MVC4+EasyUI的Web开发框架形成之旅--框架总体界面介绍
在前面介绍了一些关于最新基于MVC4+EasyUI的Web开发框架文章,虽然Web开发框架的相关技术文章会随着技术的探讨一直写下去,不过这个系列的文章,到这里做一个总结,展示一下整体基于MVC4+Ea ...
- Sharepoint CAML 增删改查 List
Lists.UpdateListItems 方法 (websvcLists) Windows SharePoint Services 3 Adds, deletes, or updates the ...
- 利用硬链接和truncate降低drop table对线上环境的影响
众所周知drop table会严重的消耗服务器IO性能,如果被drop的table容量较大,甚至会影响到线上的正常. 首先,我们看一下为什么drop容量大的table会影响线上服务 直接执行drop ...
- Datagridview 列绑定
Datagridview 列绑定 dataGridView1.Columns.Clear(); dataGridView1.Columns.Add("id", "id&q ...
- JDBC使用步骤
JDBC编程步骤 加载驱动程序:Class.forName(driverClass) 加载Mysql驱动:Class.forName("com.mysql.jdbc.Driver" ...
- ruby中proc和lambda的return区别
学习ruby有一段时间了,但是我看了好几遍proc和lambda的return区别的区别讲解,始终没明白到底什么区别,今天上午又看,终于感觉是茅塞顿开有点领悟了 一下内容部分来自<<rub ...
- [CF148E] Porcelain (分组背包)
题目链接:http://codeforces.com/problemset/problem/148/E 题目大意:有n组数据,每次可以从任意一组的两端取出1个数,问你取m个数最大能组成多少? 思路:先 ...