Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Changing Desktop Wallpaper (Win32)

This thread is locked; no one can reply to it. rss feed Print
Changing Desktop Wallpaper (Win32)
Derezo
Member #1,666
April 2001
avatar

I have a huge wallpaper collection.
A friend had suggested something kewl: A program that changes your wallpaper to a random wallpaper each time you startup your computer.

I know there are apps like this out there already, but I like to make my own things! ;D
So, here is what I have:

1#include <windows.h>
2#include <stdio.h>
3#include <time.h>
4 
5#define MAX_FILES 1024
6 
7char file_list[MAX_FILES][1024];
8 
9int main()
10 {
11 HANDLE file;
12 WIN32_FIND_DATA wfi;
13 int i = 0;
14 
15 srand(time(NULL));
16 
17 file = FindFirstFile("*.bmp",&wfi);
18 
19 if(file == INVALID_HANDLE_VALUE)
20 {
21 MessageBox(NULL,"Could not find a file.\nBe sure there is a BMP file within the directory.","Error",MB_OK|MB_ICONWARNING);
22 return 0;
23 }
24 
25 while(1)
26 {
27 strncpy(file_list<i>,wfi.cFileName,1024);
28 i++;
29 if(!FindNextFile(file,&wfi)) break;
30 }
31 
32 FindClose(file);
33 
34 SystemParametersInfo(SPI_SETDESKWALLPAPER,0,file_list[rand()%i],SPIF_UPDATEINIFILE);
35 
36 return 0;
37 }

It works, to some extent. However, if I run something that changes the screen resolution, the desktop is removed. It doesn't set it 'permanently'.

I found SystemParametersInfo() from a VB example, so I really don't know much about it.

Any ideas?
I've got this app in a folder with ~40 of my best wallpapers, and stuck it in the startup folder. It's currently a console app, but I'll change that later so it runs in the background..

"He who controls the stuffing controls the Universe"

Inphernic
Member #1,111
March 2001

Maybe this would help (for the last argument)? I have no idea. :)

SPIF_SENDWININICHANGE: Broadcasts the WM_WININICHANGE message after updating WIN.INI so that any running applications can react to it if necessary.

spellcaster
Member #1,493
September 2001
avatar

Here's what I use:
Create a "auomation.bmp" in your windows folder. Make this the default wallpaper. Now simply copy the new wallpaper at this position.
The big plus is that you can easily switch to a fixed wallpaper if you want to (by selecting another bmp) and back again.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Derezo
Member #1,666
April 2001
avatar

Ahh, very good idea spellcaster. Don't know why I didn't think of that - I use to do it all the time with the sounds on my 486 because I didn't know how to change them ;)

Quote:

SPIF_SENDWININICHANGE: Broadcasts the WM_WININICHANGE message after updating WIN.INI so that any running applications can react to it if necessary.

I meant to mention that: Originally I was using SPIF_SENDCHANGE (which is defined as SPIF_SENDWININICHANGE ;)). I switched to the other, but nothing seemed to be different.

... I wonder if SPIF_SENDCHANGE|SPIF_UPDATEINIFILE would work... I'll try that. If it doesn't work, I'll use spellcaster's idea. :)
[Edit: It didn't work :(]

Thanks

"He who controls the stuffing controls the Universe"

kdevil
Member #1,075
March 2001
avatar

Actually, I made something exactly like this just last week. The only difference was that it was in Visual Basic and took only about 10 lines / 2 minutes to make.

-----
"I am the Black Mage! I casts the spells that makes the peoples fall down!"

spellcaster
Member #1,493
September 2001
avatar

You still need to send the msg...

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Derezo
Member #1,666
April 2001
avatar

Yeah, I've been playing with it.
I added these lines (didn't change much else, other than the file to change the bg to is now C:\\windows\\default-bg.bmp):

  i = rand()%i;

  if(!CopyFile(file_list<i>,"C:\\WINDOWS\\default-bg.bmp",FALSE))
  MessageBox(NULL,"Error Copying File.","Error",MB_OK|MB_ICONWARNING);
  else
  MessageBox(NULL,"Copy Successful.",file_list<i>,MB_OK|MB_ICONWARNING);

[Snip/Edit]
Ok, I got it working! :)
It retains it after a resolution change. Very nice.
Thanks again :)

I dunno what the problem was with MSVC. It wasn't compiling properly or something. I changed build configurations and it worked fine (???).

"He who controls the stuffing controls the Universe"

spellcaster
Member #1,493
September 2001
avatar

Are you sure that image isn't copied? I know this is a stupid quetsion... but it really should yopy the image given that code :)

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Derezo
Member #1,666
April 2001
avatar

I edited my above post. :)

I don't think MSVC was compiling the program. I switched build configurations and it worked fine.

First time that's ever happened to me though. I must have done something that screwed it up.

Btw, if anyone wants this app, you can download it from my site now (it's in the newest news article, added it for the heck of it)... even though the source is here and you can compile it yourself ;D

"He who controls the stuffing controls the Universe"

Goodbytes
Member #448
June 2000
avatar

If you use spellcaster's method in a program that runs in autoexec.bat, shouldn't it be possible to avoid having to deal with the Windows API? Or does Windows store a separate copy of your wallpaper somewhere?


--
~Goodbytes

Derezo
Member #1,666
April 2001
avatar

Quote:

If you use spellcaster's method in a program that runs in autoexec.bat, shouldn't it be possible to avoid having to deal with the Windows API?

Part of the reason I use the Windows API is because it's very easy to find all the *.bmp files, and only the *.bmp files, using the file searching functions. It's also necessary to 'refresh' the background immediately. I don't use autoexec.bat, because I'd like to be able to run the program multiple times when necessary. This requires that the background be refreshed by calling SystemParametersInfo().

Plus, I'm trying to get comfy with Win32 8-)

Quote:

Or does Windows store a separate copy of your wallpaper somewhere?

It does only if the original is not a BMP, which in this case it is anyway :)
(Unsure of other versions of windows, but positive it's like this in XP)
Changing the bmp does not give an immediate change to your desktop wallpaper.

"He who controls the stuffing controls the Universe"

Go to: