JAVA调用JNA修改桌面壁纸

一时兴起想用java定时修改桌面,折腾一番,终于成功。

其实无非是java调用jna,实现对dll的调用,控制修改了桌面,纯属娱乐,不喜勿喷。

《JAVA调用JNA修改桌面壁纸》

代码在此:

import java.util.Timer;
import java.util.TimerTask;

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 EditDesktop { 


    public static void main(String[] args) {

    final Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            private int count=0;
            private int tot=0;
            String img="E:/AAAprojectDoc/img/"; 
            @Override
            public void run() {
                this.count++;
                this.tot++;
                img="E:/AAAprojectDoc/img/"+count+".jpg";

                change(img);

                if (count ==11)count = 0;                                           
                if(tot==12){
                    System.out.println("定时器停止了,img="+img);
                    timer.cancel();// 停止定时器
                }
            }
        };
        timer.schedule(task, 0, 3000);// 1秒一次 
    }



    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 change(String img){

            Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER,   
                    "Control Panel\\Desktop", "Wallpaper", img);  
            //WallpaperStyle = 10 (Fill), 6 (Fit), 2 (Stretch), 0 (Tile), 0 (Center) 
            //For windows XP, change to 0 
            Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER,   
                    "Control Panel\\Desktop", "WallpaperStyle", "10"); //fill 
            Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER,   
                    "Control Panel\\Desktop", "TileWallpaper", "0");   // no tiling

            // refresh the desktop using User32.SystemParametersInfo(), so avoiding an OS reboot 
            int SPI_SETDESKWALLPAPER = 0x14;  
            int SPIF_UPDATEINIFILE = 0x01;  
            int SPIF_SENDWININICHANGE = 0x02;  

           // User32.System
            boolean result = MyUser32.INSTANCE.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0,   
                    img, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE );  
     }




}

下了3个jart包,4.2.1版成功可用。
所需jar包下载:http://so.csdn.net/so/search/s.do?q=JNA&t=doc

参考资料:用Java更换Windows桌面壁纸http://xpenxpen.iteye.com/blog/2178860

    原文作者:9527就是我的终身代号
    原文地址: https://blog.csdn.net/qq_36828207/article/details/78784461
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞