使用 Java 在 Windows 中设置壁纸

Setting the wallpaper in Windows using Java

我正在尝试使用 Java 在 Windows 7 中设置墙纸。我试过使用答案 here and here 中的代码。它在 Windows 8 和 10 中完美运行,但在 7 中运行不佳。没有错误,它只是什么都不做。我试过在几台不同的计算机上设置不同的 1920x1080 壁纸(这是在控制面板中设置的分辨率)和不同的文件格式(png、jpg、bmp)和 运行 程序。我在应该设置墙纸的行之后的代码运行良好。我正在使用 JNA 版本 4.2.0 和 Java 8 更新 60。

有什么方法可以使用 Java 在 Windows 7 中设置壁纸?

编辑:

这是我的代码:

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

}

'it does not work' 我的意思是代码运行但墙纸没有改变。

原来 Windows 7 不喜欢将 jpeg 图片设置为墙纸。您需要先将图像文件转换为Bitmap,然后将bmp图像设置为背景。