自动更新Windows壁纸的Java脚本

准备

jna下载地址https://repo1.maven.org/maven2/net/java/dev/jna/

这里我下载的是jna-5.9.0.jar

注意jna-platform也要下载下载对应版本

jna安装参考

调用api参考

https://www.iteye.com/blog/xpenxpen-2178860

https://www.cnblogs.com/new-life/p/9345840.html

Java: 操作读写注册表(Registry)_RaySunWHUT-CSDN博客_java registry

最终函数写法主要参考https://tieba.baidu.com/p/4743466337

项目创建

一、创建Java项目

二、把jna的包导入库中,jar包添加方法参考https://www.cnblogs.com/yulia/p/6824058.html,具体如下

(1)File——Project Structure

image-20211226005832346

(2)Modules——Dependencies——Add——JARS

image-20211226005651080

image-20211226005735303

OK后可以看到Project窗口有了这个Jar包

image-20211226005809036

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 测试程序
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

public class test {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
void printf(String format, Object... args);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i=0;i < args.length;i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}

三、把需要使用的dll文件放入到java工程目录下的bin目录下,比如user32.dll,这个文件默认位置为C:\Windows\System32

image-20211226010908944

查看dll文件中有哪些函数,参考https://blog.csdn.net/zztoll/article/details/105325155

代码编写

注册表换壁纸

一下代码可以成功通过代码更换壁纸,后续再整理思路和改进自动脚本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;

public class test {
public interface CLibrary extends Library{
CLibrary INSTANCE = (CLibrary) Native.loadLibrary((Platform.isWindows() ?"user32" : "c"), CLibrary.class);
boolean SystemParametersInfoA(int uiAction, int uiParam, String fnm, int fWinIni);
}
public static void reFreshWallPaper(String path) {
Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER,
"Control Panel\\Desktop", "Wallpaper", path);
//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;

boolean result = CLibrary.INSTANCE.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0,
path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE );
}

public static void main(String[] args) {
reFreshWallPaper("C:\\Users\\victor\\Downloads\\IMG_20211128_164530.jpg");
}
}

从指定目录中读取图片文件名列表

Java 获取指定目录下指定后缀名的文件名称_清平调、其N 的博客-CSDN博客_java获取指定后缀的文件

1
2
3
4
5
6
7
8
9
10
11
12
public static ArrayList<String> getFileName(String path) {
File desktop = new File(path);
String[] arr = desktop.list();
assert arr != null;
ArrayList<String> jpgFileName = new ArrayList<>();
for (String s : arr) {
if(s.endsWith(".png") || s.endsWith(".jpg") ) {
jpgFileName.add(s);
}
}
return jpgFileName;
}

定时更新,目前暂时实现指定秒钟读数后换壁纸

另外,只顺序遍历目录中图片文件一次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class test {
public interface CLibrary extends Library{
CLibrary INSTANCE = (CLibrary) Native.loadLibrary((Platform.isWindows() ?"user32" : "c"), CLibrary.class);
boolean SystemParametersInfoA(int uiAction, int uiParam, String fnm, int fWinIni);
}
public static void reFreshWallPaper(String path) {
Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER,
"Control Panel\\Desktop", "Wallpaper", path);
//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;

boolean result = CLibrary.INSTANCE.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0,
path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE );
}

public static ArrayList<String> getFileName(String path) {
File desktop = new File(path);
String[] arr = desktop.list();
assert arr != null;
ArrayList<String> jpgFileName = new ArrayList<>();
for (String s : arr) {
if(s.endsWith(".png") || s.endsWith(".jpg") ) {
jpgFileName.add(s);
}
}
return jpgFileName;
}

public static void main(String[] args) throws InterruptedException {
ArrayList<String> name = getFileName("C:\\Users\\victor\\Downloads");
for (String s:name) {
reFreshWallPaper("C:\\Users\\victor\\Downloads\\" + s);
TimeUnit.SECONDS.sleep(3);
}
}
}

IDEA打包jar包的方法

(1)右键项目名,Open Module Settings

image-20211227003040279

(2)Artifacts,JAR,From modules with dependencies

image-20211227003142012

(3)选择要导出的类

image-20211227003244230

(4)其他默认,OK即可

image-20211227003450528

(5)Include in project build

image-20211227003514604

(6)build

image-20211227003549800

image-20211227003627902

随机可在对应目录找到jar包



----------- 本文结束 -----------




0%