如何在注册表中保存复选框值 [英] How do I save check boxes values in registry

查看:60
本文介绍了如何在注册表中保存复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有6个复选框,我希望在关闭应用程序时将值存储在注册表中,并在启动应用程序时进行后退。我该怎么做...?



我尝试过的事情:



 //这些是子对话框上的复选框设置
BOOL m_chkAddStudentVal;
BOOL m_chkUpdateStudentVal;
BOOL m_chkDeleteStudentVal;
BOOL m_chkAddTeacherVal;
BOOL m_chkUpdateTeacherVal;
BOOL m_chkDeleteTeacherVal;





 typedef struct {
void Set(int * _chkBoxes [ ],CWnd * settingwindow,int nFlag = 1)
{
//这里我要从注册表中恢复保存的数据
settingwindow-> UpdateData();
for(int i = 0; i< 6; i ++)
{
chkBoxes [i] = _ chkBoxes [i];
* chkBoxes [i] = nFlag;
}
settingwindow-> UpdateData(FALSE);
}
int * chkBoxes [6];
} CHKSETTINGS;
CHKSETTINGS m_chkSettings;





 void CDialogControlDlg :: OnExit()
{
//这里我的主要应用程序将关闭所以在这里我想将复选框的状态保存到注册表
m_setWindows.DeleteWindows();
Shell_NotifyIcon(NIM_DELETE,& niData);
退出(0);
}

解决方案

当你创建一个新的MFC项目时,会调用 SetRegistryKey( )放置在 InitInstance 。将字符串更改为您的公司名称。这将使用注册表而不是INI文件进行配置文件设置。使用的注册表路径为 HKCU \Software \< company_name> \< application_name>



然后只需使用 CWinApp 获取和设置配置文件功能。要从 CWinApp 派生类以外的其他类中调用它们,请使用 AfxGetApp()

  //  保存 
AfxGetApp() - > WriteProfileInt (_T( 设置),_ T( AddStudent),m_chkAddStudentVal);

// 恢复
m_chkAddStudentVal = AfxGetApp() - > ; GetProfileInt(_T( 设置),_ T( AddStudent), 0 )? 1 0 ;



选择合适的部分和关键名称。我使用设置作为部分名称,因为在程序终止时存储最近的设置是很常见的。


这里的 CodeProject 你可以找到一个关于从/向注册管理机构读取/写入价值的文章 [ ^ ]。



您也可以使用 WriteProfileString函数(Windows) [ ^ ]出于此目的。


现在通常不鼓励使用注册表。如果您不使用数据库,有更好的方法来保存应用程序信息。请参阅我应该在哪里存储数据? [ ^ ]。

I have 6 check boxes and I wanted to store the value's in the registry while closing application and set back while starting the application. how do I do that...?

What I have tried:

	//These are the check boxes on the child dialog Settings
BOOL m_chkAddStudentVal;
	BOOL m_chkUpdateStudentVal;
	BOOL m_chkDeleteStudentVal;
	BOOL m_chkAddTeacherVal;
	BOOL m_chkUpdateTeacherVal;
	BOOL m_chkDeleteTeacherVal;



typedef struct {
		void Set(int* _chkBoxes[],CWnd* settingwindow, int nFlag=1)
		{
//Here I want to restore saved data from registry
			settingwindow->UpdateData();
			for(int i=0; i<6; i++)
			{
				chkBoxes[i]=_chkBoxes[i];
				*chkBoxes[i]=nFlag;
			}
			settingwindow->UpdateData(FALSE);
		}
		int* chkBoxes[6];
	}CHKSETTINGS;
	CHKSETTINGS m_chkSettings;



void CDialogControlDlg::OnExit()
	{
// Here My main application is going to close so here I want to save status of the check boxes to the registry
		m_setWindows.DeleteWindows();
		Shell_NotifyIcon(NIM_DELETE,&niData);
		exit(0);
	}

解决方案

When you create a new MFC project there will be a call to SetRegistryKey() placed at InitInstance. Change the string to your company name. This will use the registry instead of INI files for profile settings. The used registry path will be HKCU\Software\<company_name>\<application_name>.

Then just use the CWinApp Get and Set profile functions. To call them from other classes than your CWinApp derived class use AfxGetApp():

// Save
AfxGetApp()->WriteProfileInt(_T("Settings"), _T("AddStudent"), m_chkAddStudentVal);

// Restore
m_chkAddStudentVal = AfxGetApp()->GetProfileInt(_T("Settings"), _T("AddStudent"), 0) ? 1 : 0;


Choose appropriate section and key names. I have used "Settings" as section name because it is common to store recent settings at program termination.


Here at CodeProject you may find a lof of articles on reading/writing values from/to the registry[^].

You might also use the WriteProfileString function (Windows)[^] for such purpose.


Use of the registry is now generally discouraged. There are better ways to save application information if you are not using a database. See Where should I store my data?[^] by OriginalGriff.


这篇关于如何在注册表中保存复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆