大概代码如下,从程序中摘出来的,没有整理
procedure TFrm_Main.WriteProxy(aEnabled: Boolean; aPort: Integer);
var
Registry: TRegistry;
OldProxy, NewProxy: String;
CharPos: Integer;
begin
Registry := TRegistry.Create;
try
Registry.RootKey := HKEY_CURRENT_USER;
Registry.OpenKey('Software/Microsoft/Windows/CurrentVersion/Internet Settings', True);
if not aEnabled then
begin
{如果禁止使用代理,则只需要设置ProxyEnable=0}
Registry.WriteInteger('ProxyEnable', 0);
end
else
begin
{如果是使用代理,则需要设置ProxyEnable=1,并且设置代理IP和端口}
Registry.WriteInteger('ProxyEnable', 1);
{得到当前ProxyServer的字符,进行替换http段,组合成新的ProxyServer}
OldProxy := Registry.ReadString('ProxyServer');
CharPos := Pos('http', OldProxy);
if CharPos <> 0 then
begin
NewProxy := Copy(OldProxy, 1, CharPos - 1) + 'http=127.0.0.1:' + IntToStr(aPort);
OldProxy := Copy(OldProxy, CharPos, Length(OldProxy) - CharPos + 1);
CharPos := Pos(';', OldProxy);
if CharPos <> 0 then
begin
OldProxy := Copy(OldProxy, CharPos, Length(OldProxy) - CharPos + 1);
NewProxy := NewProxy + OldProxy;
end
else
end
else
NewProxy := 'http=127.0.0.1:' + IntToStr(aPort);
Registry.WriteString('ProxyServer', NewProxy);
end;
finally
Registry.Free;
end;
end;