我可以在 Java/Groovy 中以编程方式更改 Windows 桌面墙纸吗? [英] Can I change my Windows desktop wallpaper programmatically in Java/Groovy?
问题描述
有没有办法在 Windows XP 中使用 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.
我在这个网站上看到了一些关于 C++ 或 .NET 的问题,但我没有看到任何特定于 Java 的内容.
I've seem some questions on this site about C++ or .NET, but I did not see anything specific to Java.
推荐答案
抱歉,我有点落后于 @ataylor 的答案,因为我正在准备一个片段来完成它.是的,JNA 是一种正确的方法.给你:
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
);
}
}
你需要在类路径上有 JNA 库才能工作.这是在 Windows 7 中测试过的,在 XP 中可能会有一些细微差别,但我认为它应该可以工作.该 API 大概是稳定的.
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.
我之前省略了选项 SPIF_UPDATEINIFILE
和 SPIF_SENDWININICHANGE
.现在正在按照 Coding4Fun MSDN 文章中的建议使用这些方法.
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.
这篇关于我可以在 Java/Groovy 中以编程方式更改 Windows 桌面墙纸吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!