Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my code to set wallpaper in Windows 7 using Java: 

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");

    }

}

This code changes the wallpaper on Windows 8 and 10. But it doesn't change the wallpaper on Windows 7.  I'm using JNA version 4.2.0 and Java 8 update 60. Can anyone tell me what I’m doing wrong here?

1 Answer

0 votes
by (19.7k points)

If you are using it on Windows 7, you need to convert the image file to Bitmap and set the bmp image as background. Windows 7 doesn’t use JPEG format for wallpaper.

Interested in Java? Check out this Java Certification by Intellipaat.   

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 31, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

Browse Categories

...