How to set the Desktop Wallpaper

Do you know how to change a single image and change the whole feeling of how your computer looks? Get your seatbelt on!

The desktop wallpaper is a great part that makes our desktop. Although its just an image shown behind the desktop icons, even after that it can totally change the way our desktop looks. What if you want to make a Desktop Wallpaper manager? Then you will have to learn how to set the wallpaper. That’s why I am writing this short article today.

Changing Wallpaper is easier with Windows API call to SystemParametersInfo function. There is another advanced method here. But for now, we’ll just focus on a basic example.

Okey, enough talk. Let’s get to coding.

Simple Example
The simplest example for changing the desktop wallpaper would be:

1
2
3
4
5
6
7
Procedure TForm1.Button1Click(Sender: TObject);
var
  PicPath : string;
begin
  PicPath := 'C:\test.bmp';
  SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, Pointer(PicPath), SPIF_SENDWININICHANGE);
end

Tutorial
Start Lazarus.

Create a new project (Project -> New Project -> Application -> OK).

Draw a TFileNameEdit (from Misc tab) and a TButton (from Standard tab). Set the Name of TButton as btnWallpaper and set its Caption as Set Wallpaper. You can use any Tlabels if you like. In that case set its Caption to something appropriate. Here is a screenshot of mine:

Now double click the TButton and enter:

1
2
3
4
procedure TForm1.btnWallpaperClick(Sender: TObject);
begin
  SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, pchar(FileNameEdit1.FileName), SPIF_SENDWININICHANGE);
end;

Scroll to the top of the code and under the uses clause add windows unit:

1
2
uses
  ..., windows;

As we are using the Windows API, the code is not cross platform. It will only work on Windows.

Now Run the project (F9 or Run -> Run).

Now open a JPG file and click the Set Wallpaper button. The wallpaper should change.

This is the simplest working example for changing the wallpaper. But it has a problem. It only changes the wallpaper image. But it does not change the wallpaper style (such as tiled, stretched). So if the last wallpaper was set to tiled, then setting the wallpaper through this program would just change the image and keep it tiled.

If you are satisfied with it then you can stop reading. But if you want to improve it then read on!

Enhancing it

In this segment we will add the Wallpaper Style option so that we can set the wallpaper as tiled, stretched etc.

Draw a TComboBox to the form and set:

1
2
3
4
5
6
7
8
9
Name = cboStyle
Items =
Tile
Center
Stretch
Custom Position

ItemIndex = 0
Style = csDropDownList

Notice the order of the text in the Items property. Such an order will help us to get the Wallpaper Style index for the registry. You will see later what I mean.

Draw 2 TSpinEdits (from Misc tab). Name one spinX and another spinY. Set both of their Value and MinValue property to 1. Don’t worry about the MaxValue. We’ll set their MaxValue on Form’s onCreate. So, double click on the form and enter:

1
2
3
4
5
procedure TForm1.FormCreate(Sender: TObject);
begin
  spinX.MaxValue:=Screen.Width;
  spinY.MaxValue:=Screen.Height;
end;

Put some labels to help the user understand which component does what. Here’s how my form looks like:

Now to coding. We will use the code from chami.com. Thanks goes to the author for his contribution. But first add the registry unit in the uses clause. Just remember we have added windows unit before, so we don’t need to add it again.

1
2
uses
  ..., windows, registry;

Now add the const clause code below after the uses clause in the unit:

1
2
3
4
5
6
const
  // WallPaperStyles
  WPS_Tile      = 0;
  WPS_Center    = 1;
  WPS_SizeToFit = 2;
  WPS_XY        = 3;

Add a forward declaration for our Wallpaper setter procedure. Enter this code somewhere after the first var clause.

1
2
3
4
procedure SetWallpaperExt(
  sWallpaperBMPPath : string;
  nStyle,
  nX, nY : integer );

Now add the procedure before the last end. line somewhere:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// sWallpaperBMPPath
//   - path to a BMP file
//
// nStyle
//   - any of the above WallPaperStyles
//
// nX, nY
//   - if the nStyle is set to WPS_XY,
//     nX and nY can be used to set the
//     exact position of the wall paper
//
procedure SetWallpaperExt(
  sWallpaperBMPPath : string;
  nStyle,
  nX, nY : integer );
var
  reg    : TRegIniFile;
  s1     : string;
  X, Y   : integer;
begin
  //
  // change registry
  //
  // HKEY_CURRENT_USER
  //   Control PanelDesktop
  //     TileWallpaper (REG_SZ)
  //     Wallpaper (REG_SZ)
  //     WallpaperStyle (REG_SZ)
  //     WallpaperOriginX (REG_SZ)
  //     WallpaperOriginY (REG_SZ)
  //
  reg := TRegIniFile.Create(
           'Control PanelDesktop' );

  with reg do
  begin
    s1 := '0';
    X  := 0;
    Y  := 0;

    case nStyle of
      WPS_Tile  : s1 := '1';
      WPS_Center: nStyle := WPS_Tile;
      WPS_XY    :
      begin
        nStyle := WPS_Tile;
        X := nX;
        Y := nY;
      end;
    end;

    WriteString( '',
      'Wallpaper',
      sWallpaperBMPPath );

    WriteString( '',
      'TileWallpaper',
      s1 );

    WriteString( '',
      'WallpaperStyle',
      IntToStr( nStyle ) );

    WriteString( '',
      'WallpaperOriginX',
      IntToStr( X ) );

    WriteString( '',
      'WallpaperOriginY',
      IntToStr( Y ) );
  end;
  reg.Free;

  //
  // let everyone know that we
  // changed a system parameter
  //
  SystemParametersInfo(
    SPI_SETDESKWALLPAPER,
    0,
    Nil,
    SPIF_SENDWININICHANGE );
end;

Now double click the TButton and enter:

1
2
3
4
5
6
7
procedure TForm1.btnWallpaperClick(Sender: TObject);
begin
  SetWallpaperExt(
      FileNameEdit1.FileName,
      cboStyle.ItemIndex,
      spinX.Value, spinY.Value );
end;

Now Run the Project (F9 or Run -> Run).

Test the Program with as many images as you want. Change the options and apply them.

Hope you had fun making this little Wallpaper setter. There is no limit of what you can do with this. You can create a Desktop Wallpaper Collection Manager or a program that can download a wallpaper everyday and set it as wallpaper. (There are also a bunch of wallpaper APIs to use: 1 2 ) Making such different variations of programs will make your coding skills improve.

Ref:
- http://stackoverflow.com/questions/1929721/how-to-change-desktop-wallpaper
- http://channel9.msdn.com/coding4fun/articles/Setting-Wallpaper
- http://www.chami.com/tips/delphi/123096D.html

Downloads

You can download the source code for the tutorial project and executable/EXE files from the links below: