Activer/désactiver le proxy

Description

Ce petit code permet la lecture et la modification de l'état du proxy, en passant par la base de registre.

Source / Exemple :


unit ProxyOneOff;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Registry, ShellApi;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    CheckBox1: TCheckBox;
    HttpE: TEdit;
    ProxyOverrideEdit: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    FtpE: TEdit;
    SecuriseE: TEdit;
    GopherE: TEdit;
    SocksE: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    procedure CheckBox1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Label3MouseEnter(Sender: TObject);
    procedure Label3MouseLeave(Sender: TObject);
    procedure Label3Click(Sender: TObject);
    procedure Label4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure DetectProxy;
    procedure EnableProxy(Enable : Boolean);
  end;

var
  Form1: TForm1;
  Proxy: boolean;
  ProxyOverride: String;
  ProxyServer: String;
  Actif : integer;
  Protocoles : String;
  Http, Ftp, Securise, Gopher, Socks : String;
implementation

{$R *.dfm}

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
 if CheckBox1.Checked then
 begin
  EnableProxy(True);
  CheckBox1.Caption :='Désactiver le proxy';
 end
 else
  begin
  EnableProxy(False);
  CheckBox1.Caption :='Activer le proxy';
 end;
end;

procedure TForm1.DetectProxy;
var Reg : TRegistry;
    Pos1, Pos2 : integer;
    Car : Char;
begin
 Reg := tRegistry.Create;
 Reg.RootKey := HKEY_CURRENT_USER;
 Reg.OpenKey('\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',True);
 ProxyServer:= Reg.ReadString('ProxyServer');
 ProxyOverrideEdit.Text := Reg.ReadString('ProxyOverride');
 Protocoles := Reg.ReadString('ProxyServer');
 Actif := Reg.ReadInteger('ProxyEnable');
 if Actif = 0 then
 begin
  CheckBox1.Checked := False;
 end
 else
 begin
   CheckBox1.Checked := True;
 end;

 Reg.CloseKey;
 Reg.Free;
// Série de booooooooooocle idiôte ;) 
//***** Http
Pos1 := Pos('=', ProxyServer)+1;
Pos2 := Pos(';', ProxyServer);
Http:=Copy(ProxyServer,Pos1,Pos2-Pos1);
HttpE.Text:= Http;
Delete(ProxyServer, 1,Pos2);
//***** FTP
Pos1 := Pos('=', ProxyServer)+1;
Pos2 := Pos(';', ProxyServer);
Ftp:=Copy(ProxyServer,Pos1,Pos2-Pos1);
FtpE.Text := Ftp;
Delete(ProxyServer, 1,Pos2);
//***** Sécurisé
Pos1 := Pos('=', ProxyServer)+1;
Pos2 := Pos(';', ProxyServer);
Securise:=Copy(ProxyServer,Pos1,Pos2-Pos1);
SecuriseE.Text:=Securise;
Delete(ProxyServer, 1,Pos2);
//***** Gopher
Pos1 := Pos('=', ProxyServer)+1;
Pos2 := Pos(';', ProxyServer);
Gopher:=Copy(ProxyServer,Pos1,Pos2-Pos1);
GopherE.Text := Gopher;
Delete(ProxyServer, 1,Pos2);
//***** Socks
Pos1 := Pos('=', ProxyServer)+1;
Pos2 := Pos(';', ProxyServer);
Socks:=Copy(ProxyServer,Pos1,Length(ProxyServer));
SocksE.Text:= Socks;
end;

procedure TForm1.EnableProxy(Enable: Boolean);
var
 reg : TRegistry;
begin
 Reg := TRegistry.Create;
 Reg.RootKey := HKEY_CURRENT_USER;
 Reg.OpenKey('\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings',True);
 if Enable then
  Reg.WriteInteger('ProxyEnable',1)
 else
  Reg.WriteInteger('ProxyEnable',0);
  
 Reg.CloseKey;
 Reg.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 DetectProxy;
  CheckBox1.Checked := Actif =1;
end;

procedure TForm1.Label3MouseEnter(Sender: TObject);
begin
 TLabel(Sender).Font.Color := ClBlue;
end;

procedure TForm1.Label3MouseLeave(Sender: TObject);
begin
 TLabel(Sender).Font.Color := ClBlack;
end;

procedure TForm1.Label3Click(Sender: TObject);
begin
 ShellExecute(Handle, 'OPEN', 'http://www.phenixcreations.com','','',SW_SHOWNORMAL);
end;

procedure TForm1.Label4Click(Sender: TObject);
begin
 ShellExecute(Handle, 'OPEN', 'mailto:farid.ferhati@phenixcreations.com','','',SW_SHOWNORMAL);
end;

end.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.