摘要:本文主要向大家介绍了在MFC编程实例中用WriteProfileInt等方法保存应用程序配置,通过具体的内容向大家展示,希望对大家学习MFC编程实例有所帮助。
本文主要向大家介绍了在MFC编程实例中用WriteProfileInt等方法保存应用程序配置,通过具体的内容向大家展示,希望对大家学习MFC编程实例有所帮助。
0.简介
CWinApp类中提供了一组用于读写应用程序配置的方法:
GetProfileInt
WriteProfileInt
GetProfileString
WriteProfileString
可方便的用于读写应用程序配置。
1.关于CWinApp::SetRegistryKey方法
用VC++的向导建立MFC项目之后,在InitInstance中可以看到这样的语句:
SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
该函数将为以上提到的几个方法建立工作环境,此时如果用WriteProfileInt写入数据,将会被写入到如下注册表位置:
HKEY_CURRENT_USER/Software/应用程序向导生成的本地应用程序/应用程序名称/
如果在InitInstance中不执行SetRegistryKey,则用WriteProfileInt写入数据时,将写入到
%windir%/应用程序名称.ini中。
2.用法
a.如果在InitInstance中执行了SetRegistryKey("清风居");
则对于:
WriteProfileInt("section","val1",10);
将在注册表中如下路径写入数据:
[HKEY_CURRENT_USER/Software/清风居/测试应用程序/section]
"val1"=dword:0000000a
注:“测试应用程序”是应用程序的名称。
b.如果在InitInstance中没执行SetRegistryKey
则对于:
WriteProfileInt("section","val1",10);
将在“%windir%/测试应用程序.ini”中写入:
[section]
val1=10
3.实例:保存应用程序的窗口大小和位置
//改变大小时
void CMyDlg::OnSize(UINT nType, int cx, int cy)
if (m_bInitDialog && cx<nX && cy<nY)
{
//保存当前大小位置
CRect rcWindow;
GetWindowRect(&rcWindow);
theApp.WriteProfileInt("Settings","left",rcWindow.left);
theApp.WriteProfileInt("Settings","top",rcWindow.top);
theApp.WriteProfileInt("Settings","right",rcWindow.right);
theApp.WriteProfileInt("Settings","bottom",rcWindow.bottom);
}
//移动窗口时
void CMyDlg::OnMove(int x, int y)
{
if (m_bInitDialog && x!=0 && y!=0)
{
//保存当前大小位置
CRect rcWindow;
GetWindowRect(&rcWindow);
theApp.WriteProfileInt("Settings","left",rcWindow.left);
theApp.WriteProfileInt("Settings","top",rcWindow.top);
theApp.WriteProfileInt("Settings","right",rcWindow.right);
theApp.WriteProfileInt("Settings","bottom",rcWindow.bottom);
}
}
//初始化
BOOL CMyDlg::OnInitDialog()
{
CRect rcWindow;
rcWindow.left=theApp.GetProfileInt("Settings","left",200);
rcWindow.top=theApp.GetProfileInt("Settings","top",120);
rcWindow.right=theApp.GetProfileInt("Settings","right",800);
rcWindow.bottom=theApp.GetProfileInt("Settings","bottom",600);
MoveWindow(&rcWindow);
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言VC/MFC频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号