1

I'm trying to set the wallpaper in Windows 7 using Java. I've tried using the code from the answers here and here. It works perfectly in Windows 8 and 10, but not in 7. There are no errors, it just doesn't do anything. I've tried setting different 1920x1080 wallpapers (that's the resolution set in the Control panel) and different file formats (png, jpg, bmp) and running the program on a few different computers. The code I have after the line that's supposed to set the wallpaper runs fine. I'm using JNA version 4.2.0 and Java 8 update 60.

Is there any way I can set the wallpaper in Windows 7 using Java?

EDIT:

Here's my code:

import java.util.HashMap;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.UINT_PTR;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIFunctionMapper;
import com.sun.jna.win32.W32APITypeMapper;

public class WallpaperChanger {

    public interface SPI extends StdCallLibrary {

        long SPI_SETDESKWALLPAPER = 20;
        long SPIF_UPDATEINIFILE = 0x01;
        long SPIF_SENDWININICHANGE = 0x02;

        @SuppressWarnings("serial")
        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);
    }

    public static void main(String[] args) {
        System.out.println("changing");

        String filename = "C:\\wallpapers\\wallpaper.jpg";

        SPI.INSTANCE.SystemParametersInfo(
                new UINT_PTR(SPI.SPI_SETDESKWALLPAPER), new UINT_PTR(0),
                filename, new UINT_PTR(SPI.SPIF_UPDATEINIFILE
                        | SPI.SPIF_SENDWININICHANGE));
        System.out.println("changed");
    }

}

By 'it does not work' I mean that the code runs but wallpaper doesn't change.

Community
  • 1
  • 1
marti201
  • 376
  • 2
  • 13
  • You should post the code here on which you have a question and please be more specific than 'it does not work'. – hotzst Oct 03 '15 at 08:24
  • @hotzst Sorry, I knew I forgot something – marti201 Oct 03 '15 at 08:53
  • Check the return value and Native.getLastError(). Windows may well be telling you what's wrong. – technomage Oct 03 '15 at 12:55
  • @technomage On Windows 7 Native.getLastError() always returns 0 but the return value is false and the wallpaper stays the same it was before. On Windows 8 the Native.getLastError() returns either 0 or 1460 (seems random) but the return value is always true and the wallpaper is set. – marti201 Oct 03 '15 at 15:13

1 Answers1

2

Turns out that Windows 7 doesn't like setting jpeg images as a wallpaper. You need to convert the image file to Bitmap first and then set the bmp image as background.

marti201
  • 376
  • 2
  • 13