unit USSaver;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Spin;

type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    GroupBox1: TGroupBox;
    BFind: TButton;
    BTest: TButton;
    LName: TLabel;
    ComboBox1: TComboBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    BWrite: TButton;
    RadioGroup1: TRadioGroup;
    CheckBox1: TCheckBox;
    SpinEdit1: TSpinEdit;
    CheckBox2: TCheckBox;
    GroupBox3: TGroupBox;
    BCPanel: TButton;
    BParam: TButton;
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure BWriteClick(Sender: TObject);
    procedure BFindClick(Sender: TObject);
    procedure BTestClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ComboBox1CloseUp(Sender: TObject);
    procedure BCPanelClick(Sender: TObject);
    procedure BParamClick(Sender: TObject);
  private
    { Private declarations }
     procedure ReadSaver;
     procedure WMACTIVATE(var a:TWMACTIVATE); message WM_ACTIVATE;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses registry, ShellAPI;

var LPanel: Char = ' ';
    reg:TRegistry;
    List: TStringList;
    StartInfo: TStartupInfo;
    ProcInfo: TProcessInformation;

procedure TForm1.ReadSaver;
var Time, ires: integer;
    L: boolean;
    SR: TSearchRec;
    DirSaver, CurrentSaver, pass: string;
    Hand: HMODULE;
    Name: array[0..MAX_PATH] of Char;
begin
 SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, @L, 0);
 CheckBox1.Checked := L;
 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, @Time, 0);
 SpinEdit1.Value := Time div 60;
 reg.OpenKey('Control Panel\Desktop',false);
 CurrentSaver := reg.ReadString('SCRNSAVE.EXE');
 pass := reg.ReadString('ScreenSaverIsSecure');
 reg.CloseKey;
 CheckBox2.Checked := (pass = '1');
 OpenDialog1.FileName := CurrentSaver;
 DirSaver := ExtractFileDir(CurrentSaver);
 LName.Caption := 'Ôàéë ' + ExtractFileName(CurrentSaver);

 List.Clear;
 ComboBox1.Clear;
 ires := FindFirst(DirSaver + '\*.scr', faAnyFile, SR);
 while ires = 0 do
 begin
  List.Add(DirSaver + '\' + SR.Name);
  Hand := LoadLibrary(PChar(DirSaver + '\' + SR.Name));
  if Hand = 0
   then ComboBox1.Items.Add(SR.Name)
   else begin
    LoadString(Hand, 1, Name, MAX_PATH);
    FreeLibrary(Hand);
    ComboBox1.Items.Add(Name);
   end;
  ires := FindNext(SR)
 end;
 FindClose(SR);
 ComboBox1.ItemIndex := List.IndexOf(CurrentSaver);
 ComboBox1CloseUp(self);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 reg := TRegistry.Create;
 reg.RootKey := HKEY_CURRENT_USER;
 List := TStringList.Create;
 FillChar(StartInfo, Sizeof(StartInfo),#0);
 StartInfo.cb := Sizeof(StartInfo);
 StartInfo.dwFlags := STARTF_USESHOWWINDOW;
 StartInfo.wShowWindow := SW_SHOWNORMAL;
 ReadSaver;
end;

procedure TForm1.BWriteClick(Sender: TObject);
var SetSS, Update: Cardinal;
begin
 if CheckBox1.Checked
  then SetSS := 1
  else SetSS := 0;
 if RadioGroup1.ItemIndex = 0
  then Update := SPIF_UPDATEINIFILE
  else Update := SPIF_SENDCHANGE;
 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, SpinEdit1.Value * 60, nil, Update);
 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, SetSS, nil, Update);
 reg.OpenKey('Control Panel\Desktop',false);
 reg.WriteString('SCRNSAVE.EXE', List.Strings[ComboBox1.ItemIndex]);
 if CheckBox2.Checked
  then reg.WriteString('ScreenSaverIsSecure', '1')
  else  reg.WriteString('ScreenSaverIsSecure', '0');
 reg.CloseKey;
end;

procedure TForm1.BFindClick(Sender: TObject);
var Hand: HMODULE;
    Name: array[0..MAX_PATH] of Char;
begin
 if OpenDialog1.Execute
 then begin
  LName.Caption := 'Ôàéë ' + ExtractFileName(OpenDialog1.FileName);
  List.Add(OpenDialog1.FileName);
  Hand := LoadLibrary(PChar(OpenDialog1.FileName));
  if Hand = 0
   then ComboBox1.Items.Add(OpenDialog1.FileName)
   else begin
    LoadString(Hand, 1, Name, MAX_PATH);
    FreeLibrary(Hand);
    ComboBox1.Items.Add(Name);
   end;
   ComboBox1.ItemIndex := ComboBox1.Items.Count - 1;
   ComboBox1CloseUp(Sender);
 end;
end;

procedure TForm1.BParamClick(Sender: TObject);
begin
 WinExec(PChar(OpenDialog1.FileName + ' -c'),SW_RESTORE);
// WinExec(PChar(OpenDialog1.FileName),SW_RESTORE);
 LPanel := 'p';
end;

procedure TForm1.BTestClick(Sender: TObject);
begin
 ShellExecute(Handle, nil, PChar(OpenDialog1.FileName), '', nil,
             SW_SHOWNORMAL);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 reg.Free;
 List.Free;
end;

procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
 OpenDialog1.FileName := List.Strings[ComboBox1.ItemIndex];
 LName.Caption := 'Ôàéë ' + ExtractFileName(OpenDialog1.FileName);
 if (ProcInfo.hProcess <> 0)
 then TerminateProcess(ProcInfo.hProcess,0);
 CreateProcess(nil, PChar(OpenDialog1.FileName +' -p' + IntToStr(Panel1.Handle)),
                      nil, nil, false, 0, nil, nil, StartInfo, ProcInfo);
// WinExec(PChar(OpenDialog1.FileName +' -p' + IntToStr(Panel1.Handle)),SW_RESTORE);
end;

procedure TForm1.BCPanelClick(Sender: TObject);
begin
 WinExec('Control.exe desk.cpl',SW_RESTORE);
 LPanel := 'c';
end;

procedure TForm1.WMACTIVATE(var a:TWMACTIVATE);
begin
 inherited;
 if (a.Active = WA_ACTIVE) then
  case LPanel of
   'c': ReadSaver;
   'p': ComboBox1CloseUp(self);
  end;
 LPanel := ' ';
 a.Result:=0;
end;

end.
