8.31.2014 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:
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:
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:

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:
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 "TSpinEdit"s (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 onCreate. So, double click on the form and enter:

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.

uses
  ..., windows, registry;

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

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.

procedure SetWallpaperExt(
  sWallpaperBMPPath : string;
  nStyle,
  nX, nY : integer );

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

//
// 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 Panel\Desktop
  //     TileWallpaper (REG_SZ)
  //     Wallpaper (REG_SZ)
  //     WallpaperStyle (REG_SZ)
  //     WallpaperOriginX (REG_SZ)
  //     WallpaperOriginY (REG_SZ)
  //
  reg := TRegIniFile.Create(
           'Control Panel\Desktop' );

  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:

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.

Download Sample Code ZIP

You can download the above example tutorial project's source code from here
Or here: http://bit.ly/wallpaper-setter
Size: 726 KB
The package contains compiled executable EXE file.

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

1 comments:

David Kenny said...

Loading iPhone wallpapers is essential for many users as well as expressing their own personality on the device. So, with iPhone 8, 8 Plus, iPhone X you know how to create your own wallpaper to your liking? Wallpaper for iPhone 8, 8 Plus, iPhone X by downloading is simple and easy even if you do not know much about it:
Red wallpaper iphone
Pink wallpaper iphone
Green wallpaper iphone
black wallpaper 4k
white wallpaper iphone
You can also refer to other wallpapers at home:top wallpaper

 
Copyright 2013 LazPlanet
Carbon 12 Blogger template by Blogger Bits. Supported by Bloggermint