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 ...
随机推荐
- 问:Linux下Chrome标题栏中文乱码
From:http://blog.csdn.net/loveaborn/article/details/29579787 在使用Linux的时候你会遇到一些奇奇怪怪的问题,不过,你会在解决这些问题的过 ...
- css模糊效果
CSS代码: .blur { filter: url(blur.svg#blur); /* FireFox, Chrome, Opera */ -webkit-filter: blur(10px); ...
- ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- win7 下 arp 绑定mac和Ip
我们都知道直接执行arp -s 命令即可绑定IP和MAC地址,但是在Win7下会遇到不能运行arp -s 进行静态mac绑定的情况,提示“ARP 项添加失败: 拒绝访问.”(英文版提示:The ARP ...
- JSBinding / Home
Description JSBinding is a tool enabling you to run actual javascript in Unity3D. It contains Mozill ...
- mysql的ONLY_FULL_GROUP_BY语义 --转自http://www.wtoutiao.com/p/19dh3ec.html
执行SET GLOBAL sql_mode = ''; 把sql_mode 改成非only_full_group_by模式.验证是否生效 SELECT @@GLOBAL.sql_mode 或 SELE ...
- WPF ListBox响应鼠标滚轮
public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject { if (obj ...
- Joint Deep Learning for Pedestrian Detection笔记
1.结构图 Introduction Feature extraction, deformation handling, occlusion handling, and classification ...
- java短信接口
一.背景 从是Java一直想做一个跟生活联系特别紧密的东西,比如短信.邮箱.电话什么的一直是我感兴趣的,可是楞是当初没有头绪弄,恰巧今天公司在做一个 webrtc的视频会议的软件,刚好有短信这个需求, ...
- mysql 的 infobright 数据库的 mediumblob 显示不了数据
需要修改mysql的配置文件: /var/www/html/phpmyadmin/config.inc.php 增加: $cfg['ProtectBinary'] = FALSE; 即可