问题描述
有没有一种方法可以使用Java(或Groovy)在Windows XP中更改桌面墙纸?我有一个程序每天创建一个新的图像(或任何时候),我想要一种方法来自动更新我的桌面。我在这里看到了一些问题网站关于C ++或.NET,但我没有看到任何特定于Java的内容。
我在这里看到了一些问题网站关于C ++或.NET,但我没有看到任何特定于Java的内容。
阿泰勒的回答是因为我正在准备一个片段来做到这一点。是的,JNA是一个正确的方法。这里你去:
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){
//提供自己的路径而不是使用这个
String path = C:\\\\\\\\\\\\\\\公司\\\\\\\\\\\'
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扩展StdCallLibrary {
//从MSDN文章
long SPI_SETDESKWALLPAPER = 20;
长SPIF_UPDATEINIFILE = 0x01;
长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);
}
});
布尔SystemParametersInfo(
UINT_PTR uiAction,
UINT_PTR uiParam,
字符串pvParam,
UINT_PTR fWinIni
);
$ / code>
您需要在类路径中包含JNA库这工作。这在Windows 7中进行了测试,XP中可能存在一些细微差别,但我认为它应该起作用。该API大概是稳定的。
省略了选项 SPIF_UPDATEINIFILE
和 SPIF_SENDWININICHANGE
。现在正按照Coding4Fun MSDN文章中的建议使用它们。
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.
I've seem some questions on this site about C++ or .NET, but I did not see anything specific to Java.
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
);
}
}
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.
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桌面墙纸吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!