Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change my Windows desktop wallpaper programmatically in Java/Groovy?

Tags:

java

groovy

Is there a way I can use Java (or Groovy) to change my desktop wallpaper in Windows XP? I have a program that creates a new image every day (or whenever) and I would like a way to automatically update my desktop.

I've seem some questions on this site about C++ or .NET, but I did not see anything specific to Java.

like image 300
Matt N Avatar asked Jan 20 '11 17:01

Matt N


People also ask

Can you change the desktop background if yes how?

Click Start, type Power Options in the Search box, and then click Power Options on the list. In the Select a power plan window, click Change plan settings next to your selected Power plan. Click Change advanced power settings, and then expand the Desktop background settings option.

How do I change my desktop background in Explorer?

Right-Click an Image File Even if the image is not open, you can make it your background. From File Explorer (called Windows Explorer in Windows 7), right-click the file you want to use. Then, from the context menu, select Set as desktop background.


2 Answers

Sorry I'm a bit behind @ataylor's answer because I was preparing a snippet to do it. Yes, JNA is a correct approach. Here you go:

import java.util.HashMap;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.UINT_PTR;
import com.sun.jna.win32.*;

public class WallpaperChanger {
   public static void main(String[] args) {
      //supply your own path instead of using this one
      String path = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg";

      SPI.INSTANCE.SystemParametersInfo(
          new UINT_PTR(SPI.SPI_SETDESKWALLPAPER), 
          new UINT_PTR(0), 
          path, 
          new UINT_PTR(SPI.SPIF_UPDATEINIFILE | SPI.SPIF_SENDWININICHANGE));
   }

   public interface SPI extends StdCallLibrary {

      //from MSDN article
      long SPI_SETDESKWALLPAPER = 20;
      long SPIF_UPDATEINIFILE = 0x01;
      long SPIF_SENDWININICHANGE = 0x02;

      SPI INSTANCE = (SPI) Native.loadLibrary("user32", SPI.class, new HashMap<Object, Object>() {
         {
            put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
            put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE);
         }
      });

      boolean SystemParametersInfo(
          UINT_PTR uiAction,
          UINT_PTR uiParam,
          String pvParam,
          UINT_PTR fWinIni
        );
  }
}

You need to have the JNA libraries on the classpath for this to work. This was tested in Windows 7, there might be some nuances in XP but I think it should work. That API is presumably stable.

References

  • Setting Wallpaper - Coding4Fun
  • How to determine if a screensaver is running in Java?
  • W32API.java

Edit (2010/01/20):

I had previously omitted the options SPIF_UPDATEINIFILE and SPIF_SENDWININICHANGE. These are now being used as they were suggested in the Coding4Fun MSDN article.

like image 171
Mark Peters Avatar answered Oct 13 '22 00:10

Mark Peters


You can do it easier:

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.PVOID;
import com.sun.jna.win32.W32APIOptions;
public class Wallpaper {    
 public static interface User32 extends Library {
     User32 INSTANCE = (User32) Native.loadLibrary("user32",User32.class,W32APIOptions.DEFAULT_OPTIONS);        
     boolean SystemParametersInfo (int one, int two, String s ,int three);         
 }
public static void main(String[] args) {   
   User32.INSTANCE.SystemParametersInfo(0x0014, 0, "C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg" , 1);
   }
 }
like image 21
Andrey Avatar answered Oct 13 '22 00:10

Andrey