用注册表保存程序配置的通用方法,源码大奉送!!!

hhmmdd 2001-12-04 07:45:37
一般程序需要保存到注册表的配置用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);
}
...全文
218 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyb_45 2001-12-11
  • 打赏
  • 举报
回复
除了加密,我不太常用注册表来储存信息,一般用ini。
软件么,让别人改配置也容易,自己改配置也省事。
goldenfinger413 2001-12-09
  • 打赏
  • 举报
回复
哇,慢慢来吧。我看看啊
hhmmdd 2001-12-07
  • 打赏
  • 举报
回复
大家讨论一下,怎么异常处理?
shen630 2001-12-06
  • 打赏
  • 举报
回复
有用,收藏
781014 2001-12-06
  • 打赏
  • 举报
回复

luhongjun 2001-12-05
  • 打赏
  • 举报
回复
还有,使用INI文件很容易被误改,对一些重要的配置不好。
luhongjun 2001-12-05
  • 打赏
  • 举报
回复
使用注册表比较好,你能写里多少东西,使用INI还的多带一个文件不好。

Richardw 2001-12-05
  • 打赏
  • 举报
回复
我喜欢INI
不过注册表好像保密性好一些,反正程序是给别人用的,不管了!
Bird1945 2001-12-05
  • 打赏
  • 举报
回复
WIN2000里的注册表太大了,而每天都在增加,我的机子还没有三个星期,
它就从10M到20多M了.我还并没有装什么东西.

注册表里的东西是删不掉的,
你所有删除的东西并没有真正从注册表里删除;
只是做了删除的标记.

如果你不想自己的注册太大,最好用INI文件!
Chxis 2001-12-05
  • 打赏
  • 举报
回复
意见就是增加异常处理
FeiHuiwxr 2001-12-05
  • 打赏
  • 举报
回复
upupup ok
Sephil 2001-12-05
  • 打赏
  • 举报
回复
我喜欢INI
gxgxfish0813 2001-12-05
  • 打赏
  • 举报
回复
UPUPUPUPUPhhmmdd(懵懂) 说得太对了。
xhfjy 2001-12-05
  • 打赏
  • 举报
回复
呵呵,那就Up了
hhmmdd 2001-12-05
  • 打赏
  • 举报
回复
我记得Windows下编程推荐使用注册表,ini是过时的东西。
hhmmdd 2001-12-05
  • 打赏
  • 举报
回复
我不想争论用ini好还是注册表好,喜欢用ini的不要用上面的代码好了,喜欢用注册表的可以就上面的代码提点意见,有什么可改进之处。
真的希望下面的帖子别争这个问题了,如果你觉得好,就up一下,让更多人受益。


ajun21century 2001-12-04
  • 打赏
  • 举报
回复
我比较同意gloom的看法!
gzc 2001-12-04
  • 打赏
  • 举报
回复
写注册表和写ini文件各有好处,看你出于什么目的。

不管啦,先Copy.......
gloom 2001-12-04
  • 打赏
  • 举报
回复
我觉得还是保存在INI文件中比较好,保存在注册表中的话容易造成注册表过于庞大,而且会生成很多垃圾键,不如INI文件绿色环保啊!

551

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 茶馆
社区管理员
  • 茶馆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧