unit UWallpaper;

interface

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

type
  TForm1 = class(TForm)
    OpenPictureDialog1: TOpenPictureDialog;
    GroupBox1: TGroupBox;
    ComboBox1: TComboBox;
    Image1: TImage;
    LName: TLabel;
    BFind: TButton;
    GroupBox2: TGroupBox;
    RadioGroup1: TRadioGroup;
    CBStyle: TComboBox;
    BWrite: TButton;
    GroupBox3: TGroupBox;
    BCPanel: TButton;
    procedure BWriteClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1CloseUp(Sender: TObject);
    procedure BCPanelClick(Sender: TObject);
    procedure BFindClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
     procedure ReadInf;
     procedure WMACTIVATE(var a:TWMACTIVATE); message WM_ACTIVATE;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses registry, Jpeg;

var LPanel: boolean = false;
    reg:TRegistry;
    List: TStringList;


procedure TForm1.FormCreate(Sender: TObject);
begin
 reg := TRegistry.Create;
 reg.RootKey := HKEY_CURRENT_USER;
 List := TStringList.Create;
 Image1.Width := Screen.Width * Image1.Height div Screen.Height;
 Image1.Left := (GroupBox1.Width - Image1.Width) div 2;
 ReadInf;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 reg.Free;
 List.Free;
end;
    
procedure TForm1.ReadInf;
var ires: integer;
    SR: TSearchRec;
    DirWallpaper, CurrentWallpaper, TileWallpaper, WallpaperStyle, ext: string;
begin
 reg.OpenKey('Control Panel\Desktop',false);
 CurrentWallpaper := reg.ReadString('Wallpaper');
 TileWallpaper := reg.ReadString('TileWallpaper');
 WallpaperStyle := reg.ReadString('WallpaperStyle');
 reg.CloseKey;
 if (TileWallpaper = '0') and (WallpaperStyle = '2')
  then CBStyle.ItemIndex := 0
 else if (TileWallpaper = '0') and (WallpaperStyle = '0')
  then CBStyle.ItemIndex := 1
 else if (TileWallpaper = '1') and (WallpaperStyle = '0')
  then CBStyle.ItemIndex := 2;
 OpenPictureDialog1.FileName := CurrentWallpaper;
 DirWallpaper := ExtractFileDir(CurrentWallpaper);
 LName.Caption := 'Ôàéë "' + ExtractFileName(CurrentWallpaper) + '"';

 List.Clear;
 ComboBox1.Clear;
 ires := FindFirst(DirWallpaper + '\*.*', faAnyFile, SR);
 while ires = 0 do
 begin
  ext := ExtractFileExt(SR.Name);
  if (ext = '.bmp') or (ext = '.jpg') or (ext = '.gif')
  then begin
   List.Add(DirWallpaper + '\' + SR.Name);
   ComboBox1.Items.Add(Copy(SR.Name,1,Length(SR.Name) - 4));
  end;
  ires := FindNext(SR)
 end;
 FindClose(SR);
 ComboBox1.ItemIndex := List.IndexOf(CurrentWallpaper);
 ComboBox1CloseUp(self);
end;

procedure TForm1.BWriteClick(Sender: TObject);
var Update: Cardinal;
    TileWallpaper, WallpaperStyle: string;
begin
 if RadioGroup1.ItemIndex = 0
   then Update := SPIF_UPDATEINIFILE
   else Update := SPIF_SENDCHANGE;
 case CBStyle.ItemIndex of
  0: begin
      TileWallpaper := '0';
      WallpaperStyle := '2';
     end;
  1: begin
      TileWallpaper := '0';
      WallpaperStyle := '0';
     end;
  2: begin
      TileWallpaper := '1';
      WallpaperStyle := '0';
     end;
 end;
 reg.OpenKey('Control Panel\Desktop',false);
 reg.WriteString('TileWallpaper', TileWallpaper);
 reg.WriteString('WallpaperStyle', WallpaperStyle);
 reg.CloseKey;
 SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,
                      PChar(OpenPictureDialog1.FileName), Update);
end;

procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
 OpenPictureDialog1.FileName := List.Strings[ComboBox1.ItemIndex];
 LName.Caption := 'Ôàéë "' + ExtractFileName(List.Strings[ComboBox1.ItemIndex]) + '"';
 Image1.Picture.LoadFromFile(List.Strings[ComboBox1.ItemIndex])
end;

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

procedure TForm1.WMACTIVATE(var a:TWMACTIVATE);
begin
 inherited;
 if (a.Active = WA_ACTIVE) and LPanel
  then begin
   ReadInf;
   LPanel := false;
  end;
 a.Result:=0;
end;

procedure TForm1.BFindClick(Sender: TObject);
var Name: string;
begin
 if OpenPictureDialog1.Execute
 then begin
  Name := ExtractFileName(OpenPictureDialog1.FileName);
  LName.Caption := 'Ôàéë "' + Name + '"';
  List.Add(OpenPictureDialog1.FileName);
  ComboBox1.Items.Add(Copy(Name, 1,Length(Name) - 4));
  ComboBox1.ItemIndex := ComboBox1.Items.Count - 1;
  ComboBox1CloseUp(Sender);
 end;
end;

end.
