【发布时间】:2016-12-11 03:22:01
【问题描述】:
我在这里找到了这段代码
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 test {
public static void main(String[] args) {
//supply your own path instead of using this one
String png = "C:\\Overwatch\\690653.png";
String jpg = "C:\\Witcher\\616521.jpg";
String path = png;
System.out.println(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
);
}
}
但它似乎不适用于 png。我只能用它来将壁纸设置为 jpg。我一直无法弄清楚 UINT_PTR 在做什么,我想知道是否需要根据图像类型更改其中一个。
我尝试更改为 UINT 并使用 LPWSTR,但它仍然只更改为 jpg。这是我目前拥有的...
public static void main(String[] args){
String[] paths = {
"C:\\Overwatch\\690653.png",
"C:\\Witcher\\616521.jpg",
"C:\\wallpapers\\mario.gif",
"C:\\wallpapers\\Mystic_Tree.wmv"
};
for(String path : paths)
System.out.println(change(path) ? "Worked!\n" + path : "Didn't work :(\n" + path);
}
public static interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.UNICODE_OPTIONS);
boolean SystemParametersInfo (UINT uiAction, UINT uint, Pointer imagePath, UINT fWinIni);
}
public static boolean change(String path){
Pointer imagePath = new LPWSTR(path).getPointer();
UINT uiAction = new UINT(0x0014l);
UINT userPolicy = new UINT(0l);
long updateIni = 0x01l;
long sendChange = 0x02l;
return User32.INSTANCE.SystemParametersInfo(uiAction, userPolicy, imagePath , new UINT(updateIni | sendChange));
}
【问题讨论】:
标签: java windows windows-7 jna