Funcao - escrevendo dados binarios no registro |
Top Previous Next |
|
// Coloque no uses: Registry // Coloque no Form: // - três edits; // - dois botões.
// Logo abaixo da palavra implementation declare:
type
{ Declara um tipo registro } TFicha = record Codigo: integer; Nome: string[40]; DataCadastro: TDateTime; end;
// Escreva o evento OnClick do Button1 conforme abaixo:
procedure TForm1.Button1Click(Sender: TObject); var Reg: TRegistry; Ficha: TFicha; begin { Coloca alguns dados na variável Ficha } Ficha.Codigo := StrToInt(Edit1.Text); Ficha.Nome := Edit2.Text; Ficha.DataCadastro := StrToDate(Edit3.Text);
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;
- Escreva o evento OnClick do Button2 conforme abaixo:
procedure TForm1.Button2Click(Sender: TObject); var Reg: TRegistry; Ficha: TFicha; 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)); Edit1.Text := IntToStr(Ficha.Codigo); Edit2.Text := Ficha.Nome; Edit3.Text := DateToStr(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;
// Observações: // Qualquer tipo de dado pode ser gravado e lido de forma binária no registro do Windows. // Para isto você precisa saber o tamanho do dado. Para dados de tamanho fixo, // use SizeOf(). Lembrete: não grave dados muito extensos no Registro do Windows (ex: imagens),
|