quinta-feira, 28 de abril de 2011

Trabalhando com o registro do windows no Delphi

Olá meus amigos e minhas amigas, já faz um bom tempo que não postamos nada relacionado a delphi, resolvi explicar/ensinar um pouco sobre "Delphi and Registro do windows ", primeiro vamos entender o que é o registro do windows, resumidamente "o registro do windows é onde todas as configurações são guardadas, tanto do próprio windows quanto da maioria dos outros softwares." Em que se aplica a utilização do registro? Se aplica simplismente a guardar informações e alterar informações de configurações do sistema operacional.

Agora vamos por a mão na massa ou melhor no teclado.


Escrevendo e lendo Strings no registro


Escrevendo

var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
try
{ Define a chave-raiz do registro }
Reg.RootKey := HKEY_CURRENT_USER;
{ Abre a chave (path). Se não existir, cria e abre. }
Reg.OpenKey('MeuProgramaProgmaster\Configuração', true);
{ Escreve uma string }
Reg.WriteString('Nome', 'teste......');
finally
Reg.Free;
end;
end;



Lendo

var
Reg: TRegistry;
nome : string;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.KeyExists('MeuProgramaProgmaster\Configuração') then
begin
Reg.OpenKey('MeuProgramaprogmaster\Configuração', false);
if Reg.ValueExists('Nome') then
nome := Reg.ReadString('Nome')
else
ShowMessage('Não existe valor com o nome "Nome"');

end else
ShowMessage('Não existe a chave no registro');
finally
Reg.Free;
end;


end;





Escrevendo e lendo Inteiros no registro

Escrevendo

var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
try
{ Define a chave-raiz do registro }
Reg.RootKey := HKEY_CURRENT_USER;
{ Abre a chave (path). Se não existir, cria e abre. }
Reg.OpenKey('MeuProgramaProgmaster\Configuração', true);
{ Escreve um Inteiro/integer }
Reg.WriteInteger('Numero', 76);
finally
Reg.Free;
end;
end;


Lendo

var
Reg: TRegistry;
numero : integer;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.KeyExists('MeuProgramaProgmaster\Configuração') then
begin
Reg.OpenKey('MeuProgramaprogmaster\Configuração', false);
if Reg.ValueExists('Numero') then
numero := Reg.ReadInteger('Numero')
else
ShowMessage('Não existe valor com o nome "Numero"');

end else
ShowMessage('Não existe a chave no registro');
finally
Reg.Free;
end;


end;


Escrevendo e lendo dados binarios no registro


Escrevendo


var
Reg: TRegistry;
Ficha: TFicha;
begin
{ Coloca alguns dados na variável Ficha }
Ficha.Codigo := 1;
Ficha.Nome := 'Jefferson Farias';
Ficha.DataCadastro := StrToDate(FormatDateTime('dd/m/yyyy',now));

Reg := TRegistry.Create;
try
{ Define a chave-raiz do registro }
Reg.RootKey := HKEY_CURRENT_USER;

{ Abre uma chave (path). Se não existir cria e abre. }
Reg.OpenKey('Cadastro\Pessoas\', true);

{ Grava os dados (o registro) }
Reg.WriteBinaryData('Dados', Ficha, SizeOf(Ficha));
finally
Reg.Free;
end;
end;



Lendo

var
Reg: TRegistry;
Ficha: TFicha;

codigo : integer;
nome : string;
data : TDate;
begin
Reg := TRegistry.Create;
try
{ Define a chave-raiz do registro }
Reg.RootKey := HKEY_CURRENT_USER;

{ Se existir a chave (path)... }
if Reg.KeyExists('Cadastro\Pessoas') then
begin
{ Abre a chave (path) }
Reg.OpenKey('Cadastro\Pessoas', false);

{ Se existir o valor... }
if Reg.ValueExists('Dados') then
begin
{ Lê os dados }
Reg.ReadBinaryData('Dados', Ficha, SizeOf(Ficha));
codigo := Ficha.Codigo;
nome := Ficha.Nome;
Data := Ficha.DataCadastro;
end else
ShowMessage('Valor não existe no registro.')
end else
ShowMessage('Chave (path) não existe no registro.');
finally
Reg.Free;
end;

end;

0 comentários:

Postar um comentário