Thread: Wallpaper Woes

  1. #1
    Not just a squid...
    Join Date
    Sep 2004
    Posts
    25

    Wallpaper Woes

    I know there is probably something out there that does this already, but I'm working on a program that selects a random wallpaper from a user-defined list each time Windows starts. I assumed the best way to do this is by replacing the path to the current wallpaper in the registry so I searched for any key that contained "wallpaper" or the name of my current image and started experimenting.

    The problem is the only key that seems to cause any change is WallpaperStyle (0 centered, 1 tiled, 2 stretched,) and nothing else seems to have any affect even after a restart. I have listed the keys I have played around with below. Does anyone know of any keys that I have missed or something else I can try?

    Code:
    HKEY_CURRENT_USER\Control Panel\Desktop\
    	ConvertedWallpaper
    	ConvertedWallpaper Last Write Time
    	TileWallpaper
    	WallpaperStyle
    
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\LastTheme
    
    HKEY_USERS\ <some long #> \Control Panel\Desktop\
    	ConvertedWallpaper
    	ConvertedWallpaper Last Write Time
    		
    HKEY_USERS\ <another long #> \Microsoft\Windows\CurrentVersion\Themes\
    	LastTheme

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I assumed the best way to do this is by replacing the path to the current wallpaper in the registry so I searched for any key that contained "wallpaper" or the name of my current image and started experimenting.
    Unfortunately, that approach is all too common. Unless documented, values in the registry should be considered internal to Windows and may change from version to version.

    The best approach is to search for 'change desktop wallpaper code' which will give you several good results on the first page. Make sure you also search google groups which will often give you better results than a web search.

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    I also found this at MSDN:

    Code:
    int nAction = com.ms.win32.win.SPI_SETDESKWALLPAPER;
    String strBMP = "c:\\winnt\\Blue Lace 16.bmp";
    int bSaveChange = 0;
    SystemParametersInfo(nAction,0,strBMP,nSaveChange);

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by andyhunter
    I also found this at MSDN:

    Code:
     
    int nAction = com.ms.win32.win.SPI_SETDESKWALLPAPER;
    String strBMP = "c:\\winnt\\Blue Lace 16.bmp";
    int bSaveChange = 0;
    SystemParametersInfo(nAction,0,strBMP,nSaveChange);
    I tried to use that method via windows scripting host once and it isnt too reliable. Sometimes it works, sometimes it doesnt.

    The best route is to rely on the IActiveDesktop interface. If you have VC++, then this should be what you need - http://cboard.cprogramming.com/showt...ight=wallpaper

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Give this a try:
    Code:
    int SetWallpaper(LPCTSTR szPath) 
    {
    	HKEY hkRoot,hSubKey;
    	TCHAR szItemKey[256];
    	TCHAR szValue[MAX_PATH];
    	long cb;
    
    	char szTargetName[MAX_PATH];        //target file name
    	char szWinPath[MAX_PATH];  //Windows path
    
    	//Get the Windows path
    	GetWindowsDirectory(szWinPath,MAX_PATH);  
    
    	//Generate the full path of the target file 
    	_stprintf(szTargetName, _T("%s\\WallPaper.BMP"), szWinPath);
    
    	//Copy the selected picture to Windows path
    	if(!CopyFile(szPath,szTargetName,FALSE))
    	{
    		::MessageBox(NULL,_T("Unable to copy file"),_T("ERROR"),MB_OK);
    		return 0;
    	}
         
    	//Update the registry
    	_tcscpy(szItemKey,"Wallpaper");
    	_tcsncpy(szValue,szTargetName);
    	cb=_tcslen(szValue);
         
    	if(RegOpenKey(HKEY_CURRENT_USER,NULL,&hkRoot)==ERROR_SUCCESS)
    	{
    		if(RegOpenKeyEx(hkRoot,
    			_T("Control Panel\\Desktop\\"),
    			0,
    			KEY_ALL_ACCESS,
    			&hSubKey)==ERROR_SUCCESS)
    		{
    			RegSetValueEx(hSubKey, (LPCTSTR)szItemKey,
    				0, REG_SZ,(CONST BYTE *)szValue,cb);
    		}
    		else
    		{
    			::MessageBox(NULL, _T("Failed to update the registry!"), _T("Error"), MB_OK);
    			return 0;
    		}
    	}
    	else
    	{
    		MessageBox(NULL, _T("Failed to open the registry!"), _T("Error"), MB_OK);
    		return 0;
    	}
    
    	//Notify Wondows to update the wallpaper
    	SystemParametersInfo(SPI_SETDESKWALLPAPER,0,NULL,SPIF_SENDCHANGE);
    
    	//Close registry
    	RegCloseKey(hSubKey);
    	RegCloseKey(hkRoot);
    	return 1;
    }
    Last edited by bithub; 12-21-2004 at 02:21 PM.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    bithub, did you write that code? It differs from your typically excellent posts, in that there appears to be a few problems, not the least of which is an obvious buffer overrun.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    bithub, did you write that code?
    No, that code was posted from a google search. All I did was put it in a compiler, and verify that it did indeed change the wallpaper. I'll edit the code to make it a bit safer though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SystemParametersInfo set wallpaper issues
    By A10 in forum Windows Programming
    Replies: 5
    Last Post: 03-14-2008, 07:39 PM
  2. Desktop Wallpaper - Animated Tiff
    By Rune Hunter in forum C# Programming
    Replies: 3
    Last Post: 02-08-2008, 06:59 PM
  3. Sticky wallpaper!
    By SMurf in forum Windows Programming
    Replies: 4
    Last Post: 02-21-2003, 12:34 PM
  4. wallpaper problem ....
    By goran in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 12-21-2001, 08:55 PM
  5. wallpaper option
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 08-29-2001, 03:55 PM