利用Windows注册表存储信息
一、TRegIniFile方法:(特点:操作简单,可用于Win95/98 Window NT)
首先,在源文件(cpp)中加入
#include <vcl\registry.hpp> //for C++Builder 3.0
__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
{ // 当Form1打开时,从注册表读入Left值
TRegIniFile *pIni = new TRegIniFile("SOFTWARE\\MyName\\MyProg");
Left = pIni->ReadInteger("Config","Left",200);
delete pIni;
}
void __fastcall TForm1::FormClose(TObject *Sender,TCloseAction &Action)
{ // 当Form1关闭时,往注册表存放Left值
TRegIniFile *pIni = new TRegIniFile("SOFTWARE\\MyName\\MyProg");
pIni->WriteInteger("Config","Left",Left);
delete pIni;
}
注:在缺省状态下,信息存放HKEY_CURRENT_USER根目录下。实际上,它代替了Windows3.1中的Ini文件.若存放在其它根目录下,可设置Registry->RootKey,例:
Registry->RootKey = HKEY_LOCAL_MACHINE;
除了可以存放整型值,还可以存放字符串值和布尔值。
二、如何操作注册表
#include <Registry.hpp>
TRegIniFile *R=new TRegIniFile("SOFTWARE\\ahao''s Softwares\\Wenku\\Config");
//读:
Edit2->Text=R->ReadString("SMTP","Host","");
Edit3->Text=IntToStr(R->ReadInteger("SMTP","Port",25));
Edit4->Text=R->ReadString("SMTP","UserID","");
//写:
R->WriteString("SMTP","Host",Edit2->Text);
R->WriteInteger("SMTP","Port",StrToInt(Edit3->Text));
R->WriteString("SMTP","UserID",Edit4->Text);
别忘了 R->Free();
- 最新文章
- 利用C++Builder自定义Windows窗体“系统菜单”[01-03]
- 利用C++Builder进行精确计时[01-03]
- 利用C++ Builer编制控制台程序[01-03]
- 利用C++ Builder让软件字幕动起来[01-03]
- 利用BCB巧建WEB浏览器[01-03]
- 关于文件复制的程序代码[01-03]
- 相关文章
- 利用C++Builder自定义Windows窗体“系统菜单”[01-03]
- 关于捕获VCL没有处理的Windows消息[01-03]
- 用托管C++监视Windows事件日志[01-03]
- Windows下C语言网络编程快速入门[01-03]
- C编写Windows服务程序的五个步骤[01-03]
- 初级 WINDOWS API C++语言版 编程(2)[01-03]
