Board index » delphi » an API code snippet Changing the Wallpaper can yo Help?

an API code snippet Changing the Wallpaper can yo Help?

The following shows the code completly that I want to make work,
when I put the parameter SPIF_SENDWININICHANGE as 0 the retval function
returned True,
but now it returns False, I caunt find out why,
All I want to d is set the wallpaper, instantly
can any one find the answer?
I have tryed swapping he parameters for real value, is it the String item
thats throwing me?

//=================================
unit Main;

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, Menus;

type
  TFRM_Wallpaper = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  FRM_Wallpaper: TFRM_Wallpaper;
  Const SPI_SETDESKWALLPAPER = 20;
  //DLL exstensions
  Function SystemParametersInfo (uAction: longint; uParam : string; lpvParam :
longint; fuWinIni :Longint): Bool; External 'user32' name
'SystemParametersInfoA'
  function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer):
Integer; stdcall; external 'user32.dll' name 'MessageBoxA';

implementation
{$R *.DFM}

procedure TFRM_Wallpaper.FormCreate(Sender: TObject);
  Var
    RetVal : bool;
    Paper : String  ;
  begin
    Paper := 'e:\_Projects\Wallpaper\alien.bmp';
    retval := SystemParametersInfo( 20, 'e:\_projects\wallpaper\alien.bmp',0
 ,SPIF_SENDWININICHANGE);

    if retval = true then
      begin
      end;
  end;
end.

 

Re:an API code snippet Changing the Wallpaper can yo Help?


Quote
Jeff Jeffreys wrote:

> The following shows the code completly that I want to make work,
> when I put the parameter SPIF_SENDWININICHANGE as 0 the retval function
> returned True,
> but now it returns False, I caunt find out why,
> All I want to d is set the wallpaper, instantly
> can any one find the answer?
> I have tryed swapping he parameters for real value, is it the String item
> thats throwing me?

<SNIP>

I got this from these forums at an earlier time:

uses
  Registry, WinProcs;

procedure SetWallpaper( sWallpaperBMPPath : String; bTile : boolean );
var
  reg : TRegIniFile;
begin
  //
  // change the registry
  //
  // HKEY_CURRENT_USER\Control Panel\Desktop
  //   TileWallpaper (REG_SZ) -- '1' or '0'
  //   Wallpaper (REG_SZ) -- path to a bitmap
  //
  reg := TRegIniFile.Create( 'Control Panel\Desktop' );
  with reg do
  begin
    WriteString( '', 'Wallpaper', sWallpaperBMPPath );
    if( bTile )then
    begin
      WriteString( '', 'TileWallpaper', '1' );
    end else
    begin
      WriteString( '', 'TileWallpaper', '0' );
    end;
  end;
  reg.Free;

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

begin
  SetWallpaper( 'c:\winnt\winnt.bmp', False );
end.

Hope that helps!

Rkr

--
                   \|||/
                   /'^'\
                  ( 0 0 )
--------------oOOO--(_)--OOOo--------------
. Reid Roman                              .
. Delphi Programmer / Analyst             .
. TVisualBasic:=class(None);              .
. May the Source be With You              .
-------------------------------------------
. Auto-By-Tel (http://www.autobytel.com)  .
. Irvine, CA U.S.A                        .
. E-Mail : rkroman (at) pacbell (dot) net .
. or reidr (at) autobytel (dot) com       .
-------------------------------------------

Re:an API code snippet Changing the Wallpaper can yo Help?


Quote
>All I want to do is set the wallpaper, instantly
>can any one find the answer?

I think you were swapping the 0 and the filename parameters; it also looked
like you were passing the filename as a Pascal string (which should be OK in
32-bit but not 16), and I don't see why you'd want to define/redefine the
SPI constants yourself... This has always worked for me (coded for either 16
or 32 bit):

procedure SetWallpaper;
var
  fn: array [0..128] of char;
begin
  StrPCopy(fn, 'E:\MYDIR\ALIEN.BMP');
  SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, @fn, SPIF_UPDATEINIFILE);
end;

Other Threads