Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

set windows background picture

by pg (Canon)
on Jul 12, 2004 at 15:21 UTC ( [id://373625] : perlquestion . print w/replies, xml ) Need Help??

pg has asked for the wisdom of the Perl Monks concerning the following question:

I thought this would change my windows background picture to yahoo.gif. The image exists in \perl582\bin directory, as well as the perl script:

use strict; use warnings; use Win32::API; my $api = Win32::API->new('user32', 'SystemParametersInfo', ['I', 'I', + 'P', 'I'], 'I') || die 'failed\n'; print $api->Call(20, 0, "yahoo.gif", 3);

But it didn't...

Please help, thanks.


Forgot to mention, this is windows 2000, and the return code printed was zero.

Update 2:

Didn't work even if I specify the full path of the picture.

Replies are listed 'Best First'.
Re: set windows background picture
by roju (Friar) on Jul 12, 2004 at 15:40 UTC

    The code is fine, works here on WinXP. Convert yahoo.gif to yahoo.bmp and try again.

    Update: Also, take a look at Win32::Wallpaper and this article by Dave Roth - there's some code there to do it.

    Update 2: Also, I know I mentioned this in a PM, but Active Desktop doesn't play nice - make sure to get rid of it. Looks like your code is the same as Simple script to change win32 wallpaper, which is two years old and hasn't had a complaint - so it's my guess that, as I originally suspected - it's not the code.

      Thanks roju for all your helps, including those chats you sent me.

      For your update 2, It still does not work for me even with active desktop disabled.

      For update 1, I looked at the source code for Win32::Wallpaper, and made up this to mimic it:

      use strict; use warnings; use Win32::API; my $SystemParametersInfo = new Win32::API(qw(user32 SystemParametersIn +foA NNPN N)) || die $^E; $SystemParametersInfo->Call( 20, 0, "c:\\perl582\\bin\\yahoo.bmp",1 ) +|| die $^E;

      Unfortunately it still does not work. But yes, I agree with you, my source code was most likely alright. Something is wrong ;-)

        I tested here in a Win 2000 and it didn't work also...
        (so it is not only you :))
        I really really really hate this answer, but did you try rebooting? I remember seeing code up here to change the wallpaper and thinking it was very cool, only to find that the change was not effected immediately.

      Converted the picture to .bmp, but it didn't work, and return code became 1.

      Tried to set through windows by right click on desktop, both .gif and .bmp worked...

Re: set windows background picture
by jacques (Priest) on Jul 12, 2004 at 22:16 UTC
    I am the author of Win32::Wallpaper.

    Please note that version 0.03 of my module has a bug. You should use version 0.04 which is available at CPAN. The last time I checked, ActiveState's repository still had the old version. It has been almost a year. The knuckleheads at ActiveState will never fix their repositories. It is pointless to get upset at them...

    Why would you copy my code, when you can just use the module? Do you not have permissions to install modules? You can always install the module in your own directory.

    Also please use Super Search. There are other ways of doing this...

Re: set windows background picture
by pg (Canon) on Jul 13, 2004 at 02:30 UTC

    I am now the happiest man. Finally, I figured this out. There is a restriction on file size. The code was alright, once I test with small .bmp files, it works.

    The bad thing is that, with big images, it does not work, but the API call still returns successfully. But not much one can do, that's just how it works.

    Guys, thanks for all the helps. By the way, jacques, the reason I didn't install your module, instead only copied a piece is that, the whole point is to figure out what was wrong, not just workaround ;-) I wanted to know why.


    When I say small .bmp file, I actually meant the file size. It probably not only has something to do with the size (width, height) of the image, but also could be the color schema (certain types of bmp), resolution. Any way, when I test with those wallpaper images coming with windows, they all worked. and I noticed that their sizes are small. (This moment, I am on windows 98).

Re: set windows background picture
by bart (Canon) on Jul 13, 2004 at 11:04 UTC
    For all I know, the following basic rules apply:
    • Use an absolute path for the image file
    • Use backslashes for the path
    • Only .BMP files will work, at least on Win98
    The last condition may have been relaxed for later editions of Windows. I doubt it. (They use a workaround in VB: "If the file is not a bitmap file, the program loads it into a hidden PictureBox and then saves the image as a bitmap file.") If you want to use something other than a .BMP file, convert it to a .BMP first, and use that.

    For the first two items, you can use rel2abs() from File::Spec/File::Spec::Functions, it will turn any path into a nice, absolute, native, Windows file path — with backslashes.