• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Set Desktop Wallpaper via a MSVC++ Program

I would like to be able to write a program or batch file that sets my desktop wall paper to a preset bitmap.  Anyone have a function call that allows this?  I'm hoping there's a MS API call to do so.  If I need to edit the registry, it's not worth the trouble.
0
cbeattie
Asked:
cbeattie
  • 6
  • 5
1 Solution
 
nietodCommented:
Forget the batch file.  You can easily do it from a windows program though.  You just use the SystemParametersInfo() fucntion with an index of SPI_SETDESKPATERN.

Example follows.
0
 
nietodCommented:
This program console program can be called from a batch file.  It will set the wallpaper to the bitmap whose file name and path are specified in the 1st parameter to the program.  

#include <windows.h>
#include <iostream>
using namespace std;

int main(int argc,char *argv[],char *envp[])
{
   if (argc < 2)
   {
        cout <<"Error: bitmap file name must be specifed in the parametes.";
        return -1;
   }
   SystemParametersInfo(SPI_SETDESKWALLPAPER,0,argv[1],SPIF_SENDCHANGE);
   return 0;
}
0
 
cbeattieAuthor Commented:
nietod,

I compiled your code and called from a dos prompt as the following:

SetWallpaper C:\Winnt\Cloud.bmp

but, alas, nothing happend.  any other ideas?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
cbeattieAuthor Commented:
nietod,

I compiled your code and called from a dos prompt as the following:

SetWallpaper C:\Winnt\Cloud.bmp

but, alas, nothing happend.  any other ideas?
0
 
cbeattieAuthor Commented:
BTW, if I use the SPIF_UPDATEINIFILE parameter, when I right click on the desktop and choose properties, the new wallpaper is selected, but it does not appear on the desktop unless I click on it and hit apply.
0
 
nietodCommented:
That probably means that the change won't take affect until you reboot.  You can try specifying SPIF_UPDATEINIFILE, in the last parameter, but that probably won't help.
0
 
nietodCommented:
Several examples I've seen all seem to be using SPIF_UPDATEINIFILE | SPIF_SENDCHANGE as the last parameter.  Althoguh the docs's don't indicate that this parameter is a bit flag type parameter.  Try that.
0
 
cbeattieAuthor Commented:
Bingo, just had to OR them together.  Be nice if the documentation told you that the parameter was a bit flag type.

Thanks!
0
 
nietodCommented:
MS likes to make things hard for programers.  We don't complain because then we get paid more.  So be quite, or you'll cost us money.  :-)
0
 
cbeattieAuthor Commented:
As a matter of fact, the damn docs say "This parameter can be zero or can be one of the following values".  ONE OF THE FOLLOWING VALUES.  Bastards.  But you're right.  If anyone could figure this out, we wouldn't get paid what we do.

Thanks :-)
0
 
nietodCommented:
yeah, that's what threw me too.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now