Thursday 20 May 2021

How to Change Desktop background in Java using Java Native Access (JNA) Library



Java Native Access JNA

Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native .




HaileePhotoDesktopSetter.java 

package us.javaj.johny.hailee.desktop;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;
import com.sun.jna.win32.StdCallLibrary;

public class HaileePhotoDesktopSetter {

public static void main(String[] args) {
HaileePhotoDesktopSetter.changeDesktopWallpaper();

}

private interface MyUser32 extends StdCallLibrary {

MyUser32 INSTANCE = (MyUser32) Native.loadLibrary("user32", MyUser32.class);

boolean SystemParametersInfoA(int uiAction, int uiParam, String fnm, int fWinIni);
}

public static void changeDesktopWallpaper() {

// -----------------------------------

String johny = "Johny Invoke the Desktop Background Setter with Love in Hailee Stienfeld ::";
System.out.println();
System.out.println("------------------------------Hailee Johny --------------------------------------");
System.out.println();
System.out.println(johny);
System.out.println();

String filePath = "E:\\Hailee-Johny\\Hailee_Stienfeld_Johny_S\\Desktop\\Hailee.jpg";
HaileePhotoDesktopSetter.setWallpaper(filePath);
System.out.println("Desktop Images Set Succussfully ");
System.out.println("Image File Path :: "+filePath);

}

public static void setWallpaper(String filePath) {

Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, 
                                              "Control Panel\\Desktop", "FitWallpaper",filePath);
// WallpaperStyle = 10 (Fill), 6 (Fit), 2 (Stretch), 0 (Tile), 0 (Center)
// For windows XP, change to 0
int SPI_SETDESKWALLPAPER = 0x14;
int SPIF_UPDATEINIFILE = 0x01;
int SPIF_SENDWININICHANGE = 0x02;
// User32.System
boolean result =                MyUser32.INSTANCE.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0,
        filePath,SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);

}

}


Desktop Wallpaper before running above code 
















Desktop Wallpaper before running after code

Output in java console 
-------------------------------- Hailee Johny --------------------------------------

Johny Invoke the Desktop Background Setter with Love in Hailee Stienfeld ::

Desktop Images Set Succussfully 
Image File Path :: E:\Hailee-Johny\Hailee_Stienfeld_Johny_S\Desktop\Hailee.jpg 








No comments:

Post a Comment