用注册表保存程序配置的通用方法,源码大奉送!!!
一般程序需要保存到注册表的配置用TEdit,TCheckBox,TRadioGroup就能表示了。如果不够,可自行扩充。
#ifndef globalH
#define globalH
#include <Registry.hpp>
//---------------------------------------------------------------------------
class TReg {
String RegPath;
TRegistry *reg;
public:
void SetInt(String name,int value,String path="");
int GetIntDef(String name,int defvalue,String path="");
void SetBool(String name,bool value,String path="");
bool GetBoolDef(String name,bool defvalue,String path="");
void SetStr(String name,String value,String path="");
String GetStrDef(String name,String defvalue,String path="");
public:
TReg();
~TReg();
void SaveToRegistry(TWinControl *Parent, String path="");
void ReadFromRegistry(TWinControl *Parent, String path="");
};
extern TReg Reg;
//---------------------------------------------------------------------------
#endif
//-------------global.cpp----------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "global.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
TReg Reg;
//---------------------------------------------------------------------------
TReg::TReg()
{
RegPath="\\Software\\YourName\\"+Application->Title+"\\";
reg=new TRegistry;
reg->RootKey=HKEY_LOCAL_MACHINE;
}
TReg::~TReg()
{
delete reg;
}
//---------------------------------------------------------------------------
void TReg::SetInt(String name,int value,String path)
{
reg->OpenKey(RegPath+path,true);
reg->WriteInteger(name,value);
reg->CloseKey();
}
int TReg::GetIntDef(String name,int defvalue,String path)
{
int iRet;
reg->OpenKey(RegPath+path,true);
if(!reg->ValueExists(name)) reg->WriteInteger(name,defvalue);
iRet = reg->ReadInteger(name);
reg->CloseKey();
return iRet;
}
void TReg::SetBool(String name,bool value,String path)
{
reg->OpenKey(RegPath+path,true);
reg->WriteBool(name,value);
reg->CloseKey();
}
bool TReg::GetBoolDef(String name,bool defvalue,String path)
{
bool bRet;
reg->OpenKey(RegPath+path,true);
if(!reg->ValueExists(name)) reg->WriteBool(name,defvalue);
bRet = reg->ReadBool(name);
reg->CloseKey();
return bRet;
}
void TReg::SetStr(String name,String value,String path)
{
reg->OpenKey(RegPath+path,true);
reg->WriteString(name,value);
reg->CloseKey();
}
String TReg::GetStrDef(String name,String defvalue,String path)
{
String ret;
reg->OpenKey(RegPath+path,true);
if(!reg->ValueExists(name)) reg->WriteString(name,defvalue);
ret = reg->ReadString(name);
reg->CloseKey();
return ret;
}
//--------------------------------------------------------------------------
void TReg::SaveToRegistry(TWinControl *Parent, String path)
{
TControl *ctl;
for(int i=0; i<Parent->ControlCount; i++)
{
ctl = Parent->Controls[i];
if (ctl->ClassNameIs("TEdit")) {
SetStr(ctl->Name, ((TEdit*)ctl)->Text, path);
} else if (ctl->ClassNameIs("TCheckBox")) {
SetBool(ctl->Name, ((TCheckBox*)ctl)->Checked, path);
} else if (ctl->ClassNameIs("TRadioGroup")) {
SetInt(ctl->Name, ((TRadioGroup*)ctl)->ItemIndex, path);
}
}
}
void TReg::ReadFromRegistry(TWinControl *Parent, String path)
{
TControl *ctl;
for(int i=0; i<Parent->ControlCount; i++)
{
ctl = Parent->Controls[i];
if (ctl->ClassNameIs("TEdit")) {
((TEdit*)ctl)->Text = GetStrDef(ctl->Name, ((TEdit*)ctl)->Text, path);
} else if (ctl->ClassNameIs("TCheckBox")) {
((TCheckBox*)ctl)->Checked = GetBoolDef(ctl->Name, ((TCheckBox*)ctl)->Checked, path);
} else if (ctl->ClassNameIs("TRadioGroup")) {
((TRadioGroup*)ctl)->ItemIndex = GetIntDef(ctl->Name, ((TRadioGroup*)ctl)->ItemIndex, path);
}
}
}
//---例子------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Reg.ReadFromRegistry(TabSheet1); //或Panel
}
//-------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
Reg.SaveToRegistry(TabSheet1);
}