通过设置注册表来实现,设置完毕立即生效,不用重新启动 IE 浏览器。
参数说明:
Proxy:代理服务器 IP 地址;
Port:端口号;
ByPass:对所有的协议均使用相同的代理服务器;
bEnabled:是否使用代理服务器(代理服务器是否生效)
函数代码:
// uses Wininet, registry;
function ChangeProxy(const Proxy, Port,ByPass: string; const bEnabled: boolean = True): boolean;
var
reg: Tregistry;
info: INTERNET_PROXY_INFO;
Fproxy: string;
begin
Result := False;
FProxy :=Format('%s:%s',[Proxy,Port]);
reg :=Tregistry.Create;
try
reg.RootKey :=HKEY_CURRENT_USER;
if reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Internet Settings', True) then
begin
reg.Writestring('ProxyServer', Fproxy);
reg.WriteInteger('ProxyEnable', integer(bEnabled));
info.dwAccessType :=INTERNET_OPEN_TYPE_PROXY;
info.lpszProxy :=pchar(proxy);
info.lpszProxyBypass :=pchar(ByPass);
InternetSetOption(nil, INTERNET_OPTION_PROXY, @info, SizeOf(Info));
InternetSetOption(nil, INTERNET_OPTION_SETTINGS_CHANGED, nil, 0);
Result:=True;
end
finally
reg.CloseKey;
reg.free;
end;
end;
调用:
procedure TForm1.Button1Click(Sender: TObject);
begin
ChangeProxy('202.99.29.27','80','1',true)
end;
取消:
procedure TForm1.Button2Click(Sender: TObject);
begin
ChangeProxy('202.99.29.27','80','1',false)
end;
rocedure TForm1.Button1Click(Sender: TObject);
var
localIp,ipEnd,dir:string;
list:tstrings;
begin
localIp:=idipwatch1.LocalIP;
list:=tstringlist.Create;
extractstrings(['.'],[],pchar(localIp),list);
ipEnd:= list.Strings[list.Count-1];
list.Free;
memo1.Clear;
memo1.Lines.Add('@echo off');
memo1.Lines.Add(':start');
memo1.Lines.Add('netsh interface ip set address name="本地连接" static 172.16.0.'+ipEnd+' 255.255.0.0');
memo1.Lines.Add('netsh interface ip set address name="本地连接" gateway=172.16.0.1 gwmetric=1 >nul');
memo1.Lines.Add('netsh interface ip set dns name="本地连接" source=static addr=172.16.0.1 register=primary');
memo1.Lines.Add('del "ip.bat"');
memo1.Lines.Add('echo.');
getDir(0,dir);
memo1.Lines.SaveToFile(dir+'\ip.bat');
ShellExecute(handle,'open',pchar(dir+'\ip.bat'),nil,nil,SW_SHOWNORMAL);
end;