MFC对注册表的操作
2015-07-03 18:58
459 查看
1 、MFC对注册表操作
在新建的MFC应用程序的CXXXXXApp::InitInstance()函数中通常都会存在这样一条语句:SetRegistryKey(_T("XXXXXXX"))。
根据MSDN,SetRegistryKey函数的功能是设置MFC应用程序的注册表访问键,并把读写.ini文件的成员函数映射到读写注册表,只需要调用SetRegistryKey函数设置注册表键值(这个键值通常是Company的名字),那么下面6个成员函数就被映射到注册表读取了!但是有一点需要注意:这个六个函数是CWinApp的成员函数,如果在对话框中调用这6个函数,要写出以下形式: AfxGetApp()->GetProfileString("XXX","XXXX");
WriteProfileBinary Writes binary data to an entry in the application's .INI file.
WriteProfileInt Writes an integer to an entry in the application's .INI file.
WriteProfileString Writes a string to an entry in the application's .INI file.
GetProfileBinary Retrieves binary data from an entry in the application's .INI file.
GetProfileInt
Retrieves an integer from an entry in the application's .INI file.
GetProfileString Retrieves a string from an entry in the application's .INI file.
数据在注册表的存储形式就如下所示:
HKEY_CURRENT_USER\Software\<company name>\<application name>\<section name>\<value name>.
<section name>是字段名,比如我们要存储登录信息,我们就可以建一个字段“LogInfo",然后再往这个字段里面写数据,比如登录名和密码,这些数据是以键值对的形式存放。
下面上代码:
当然,MFC对注册表的操作其实是对Win32 API的封装,对于我们使用者来说只需要直接调用MFC API就行了。
在新建的MFC应用程序的CXXXXXApp::InitInstance()函数中通常都会存在这样一条语句:SetRegistryKey(_T("XXXXXXX"))。
根据MSDN,SetRegistryKey函数的功能是设置MFC应用程序的注册表访问键,并把读写.ini文件的成员函数映射到读写注册表,只需要调用SetRegistryKey函数设置注册表键值(这个键值通常是Company的名字),那么下面6个成员函数就被映射到注册表读取了!但是有一点需要注意:这个六个函数是CWinApp的成员函数,如果在对话框中调用这6个函数,要写出以下形式: AfxGetApp()->GetProfileString("XXX","XXXX");
WriteProfileBinary Writes binary data to an entry in the application's .INI file.
WriteProfileInt Writes an integer to an entry in the application's .INI file.
WriteProfileString Writes a string to an entry in the application's .INI file.
GetProfileBinary Retrieves binary data from an entry in the application's .INI file.
GetProfileInt
Retrieves an integer from an entry in the application's .INI file.
GetProfileString Retrieves a string from an entry in the application's .INI file.
数据在注册表的存储形式就如下所示:
HKEY_CURRENT_USER\Software\<company name>\<application name>\<section name>\<value name>.
<section name>是字段名,比如我们要存储登录信息,我们就可以建一个字段“LogInfo",然后再往这个字段里面写数据,比如登录名和密码,这些数据是以键值对的形式存放。
下面上代码:
SetRegistryKey(_T("HIT EasyLiu"));//设置注册表键值
CWinApp* pApp = AfxGetApp(); //获取应用程序类,用来调用注册表操作函数 CString strSection = _T("LogInfo"); CString strStringItem = _T("UserName"); CString strIntItem = _T("PassWord"); pApp->WriteProfileString(strSection, strStringItem, _T("EasyLiu")); //往注册表对应字段写入字符串键值对 CString strValue; strValue = pApp->GetProfileString(strSection, strStringItem); //获取对应字段里面,对应键的键值 ASSERT(strValue == _T("EasyLiu")); //判断读取和写入的字符串是否一致 pApp->WriteProfileInt(strSection, strIntItem, 1234); //往注册表对应字段写入整数 int nValue; nValue = pApp->GetProfileInt(strSection, strIntItem, 0); ASSERT(nValue == 1234);然后查看注册表,快捷键Win+R,然后输入regedit就可以打开注册表,切换到支HKEY_CURRENT_USER\Software\下面就看到如下结果:其中HIT EasyLiu是我设置的注册表键值,TeeChartTest是MFC应用程序的名字,LogInfo是上面程序里建的字段,说明对注册表读写成功!
当然,MFC对注册表的操作其实是对Win32 API的封装,对于我们使用者来说只需要直接调用MFC API就行了。
相关文章推荐
- Visual C++中MFC消息的分类
- MFC中Radio Button的用法详解
- MFC创建右键弹出菜单的方法
- MFC中动态创建控件以及事件响应实现方法
- C++ 关于MFC多线程编程的注意事项
- MFC程序对文件的处理方法
- MFC自定义消息的实现方法
- MFC之ComboBox控件用法实例教程
- MFC绘制不规则窗体的方法
- IE8引发 VS2005/2008 MFC向导出错的解决方案
- MFC实现全屏功能代码实例
- 使用VS2010创建MFC ActiveX工程项目
- C++中MFC Tab Control控件的使用详解
- MFC程序设计常用技巧汇总
- MFC扩展DLL中导出类和对话框的实现方法
- VC中SDK与MFC的区别浅析
- 浅谈MFC 改变控件大小和位置
- VC MFC非模态对话框的实现方法
- MFC程序执行过程深入剖析
- C和MFC巧妙获取外网IP的两种实现方法