使用Java在Windows中设置墙纸 [英] Setting the wallpaper in Windows using Java

查看:96
本文介绍了使用Java在Windows中设置墙纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java在Windows 7中设置墙纸.我已经尝试使用答案此处

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.

我可以使用Java在Windows 7中设置墙纸吗?

Is there any way I can set the 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");
    }

}

不起作用"是指代码可以运行,但墙纸不会更改.

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

推荐答案

事实证明Windows 7不喜欢将jpeg图像设置为墙纸.您需要先将图像文件转换为Bitmap,然后将bmp图像设置为背景.

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.

这篇关于使用Java在Windows中设置墙纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆