请问如何用delphi5编程修改注册表的键值,请举一个实例。(40分)

Z

zhwww

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何用delphi5编程修改注册表的键值,请举一个实例。
另及为什么用delphi5编的一个小程序都有300k。
 
procedure TForm1.NewStringValue1Click(Sender: TObject);
var
phKey: hKey;
// a handle to the key
KeyName: string;
// holds the opened key name
pNewValue: array[0..255] of char;
// holds the new string value
pValueName: array[0..255] of char;
// holds the new value name
begin
{prompt the user for a new string value}
if Form2.ShowModal = IDOK then
begin
{open the selected key}
phKey := OpenNodeKey(TreeView1.Selected, KeyName);
{copy the value name and value to the null terminated strings}
StrPCopy(pNewValue,Form2.Edit2.Text);
StrPCopy(pValueName,Form2.Edit1.Text);
{create the new value in the selected key}
RegSetValueEx(phKey, pValueName, 0, REG_SZ, @pNewValue, StrLen(pNewValue));
{force the key to be written to the registry}
RegFlushKey(phKey);
{update UI elements to reflect the new value}
TreeView1Change(TreeView1,TreeView1.Selected);
Form2.Edit1.Text := '';
Form2.Edit2.Text := '';
end;
end;
 
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey(Key, True) then
Reg.WriteString('FIELD', 'Value');
finally
Reg.CloseKey;
Reg.Free;
end;
end;
 
尝试把uses里的东西一个个删除再编译,多余的都去掉后还会小一些
 
这是更改桌面墙纸的例子:
var
Reg:TRegistry;
begin
Reg := TRegistry.Create;
try
Reg.OpenKey('Control Panel/Desktop',False);//打开Control Panel下的Desktop键
Reg.WriteString('Wallpaper',FileName);//指定位图
Reg.WriteString('TileWallpaper',0);//设置平铺方式
Reg.CloseKey;
//关闭键
finally
Reg.Free;
end;

{更新用户配置文件并刷新桌面}
SystemParametersInfo(SPI_SETDESKWALLPAPER,0,nil,SPIF_SENDWININICHANGE);
end;
 
function htwReadRegisry( RegKey :string ;
Default :string ;
MyRootKey : Hkey;
MyOpenKey :string): string ;
var
Registry: TRegistry;
S : string;
begin
Registry:= TRegistry.Create;
try
begin
Registry.RootKey :=MyRootKey;
// HKEY_CURRENT_USER;
Registry.OpenKey(MYOpenKey,false);
// Registry.OpenKey('/Software/MachineAdministrator',false);
S := Registry.ReadString(RegKey);
end
finally
begin
Registry.CloseKey;
Registry.Free;
end;
if Trim(S)='' then
Result := Default
else
Result := S;
end;

end;

procedure htwWriteRegisry(RegKey :string ;asValueTobewrited : string;
MyRootKey : Hkey;
MyOpenKey :string );
var
Registry: TRegistry;
begin
Registry:= TRegistry.Create;
try
begin
Registry.RootKey :=MyRootKey;
// HKEY_CURRENT_USER;
Registry.OpenKey(MyOpenKey,true);
//Registry.OpenKey('/Software/MachineAdministrator',true);
Registry.WriteString(RegKey,asValueTobewrited);
end
finally
begin
Registry.CloseKey;
Registry.Free;
end;
end;
end;

调用方法:
try
htwWriteRegisry('sShortDate','yyyy-MM-dd' ,HKEY_CURRENT_USER, '/Control Panel/International');
htwWriteRegisry('sShortDate','yyyy-MM-dd' ,HKEY_USERS, '/.DEFAULT/Control Panel/International');
setlocaleinfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SDATE,'yyyy-MM-dd');
except
end;
 
多人接受答案了。
 
顶部