How to Change My Windows Desktop Wallpaper Programmatically in Java/Groovy

Can I change my Windows desktop wallpaper programmatically in Java/Groovy?

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.

References

  • Setting Wallpaper - Coding4Fun
  • How to determine if a screensaver is running in Java?
  • W32API.java

Edit (2010/01/20):

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.

How to change windows desktop background

Can I change my Windows desktop wallpaper programmatically in Java/Groovy?

So the correct way is with JNA if you are not familiar with JNA or if you haven't used JNA the link above is for you. Otherwise you are right about using natives but you never stated how you are using them so I am just taking shots in the dark here.

Setting the wallpaper in Windows 10 using Java

You've copied an answer from this question. Windows sets background as black screen if the path is rubbish, so you should test the path you supply exists using Files.isRegularFile(Path.of(xyz)) or new File(xyz).isFile() before calling SystemParametersInfoA.

private static final int SPI_SETDESKWALLPAPER  = 0x0014;
private static final int SPIF_UPDATEINIFILE = 0x01;
private static final int SPIF_SENDCHANGE = 0x02;
User32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);

Is it possible to change the desktop background with Java for different operating systems?

You can just use:

String os = System.getProperty("os.name");

to determine what OS the app is running on, and decide what to do from there. Like so:

if (os.startsWith("Windows")) {
// includes all Windows versions
} else if (os.startsWith("Mac")) {
// includes all Mac OS versions
} else {
// all others
}

I suggest looking up all of the different values os.name can have to be able to handle as many as possible. You might want to use enums for these values instead of checking startsWith like I did. Here is a list of values you might want to consider (although not very up to date).

Setting the wallpaper in Windows using Java

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.

Change Windows 10 Wallpaper in Java

You are using incompatible versions of jna.jar and jna-platform.jar. You can download a latest version of these jars at https://github.com/java-native-access/jna

Using Java to change Windows 7 desktop background from inside a runnable jar

Your issue is the path of the image. The path you used is inside of your jar file, and as such windows can't locate it. Thus, it just ignores the request.

Change it to something like this:

String path = "C:\\picture.jpg";

and put the picture in that location.

how to set windows wallpaper in java

Assuming the code you've posted is exactly what you're trying to use... you've not included the body of the SPI interface. It's a shot in the dark considering you didn't tell us what error eclipse is throwing. If you want more specific help, you'll need to be more specific than saying some parts of your code are "red." That does not help us.

Copy the full code from the stackoverflow link you've posted and it ought to work.

Also, while unrelated to your problem, your class name should be changed to be consistent with the Java standards. See http://www.oracle.com/technetwork/java/codeconv-138413.html



Related Topics



Leave a reply



Submit