I found this code here
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
);
}
}
but it doesn't seem to work with png. I can only use it to set the wallpaper to jpg. I have been having trouble figuring out what UINT_PTR is doing and I'm wondering if I need to change one of them depending on the image type.
I tried changing to UINT and using LPWSTR but it's still only changing to jpg. This is what I currently have...
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));
}