前面
python实现更换壁纸后发现打包一言难尽,日常使用很困难,还是得用系统级别的……
1. winapi 依赖
[package]
edition = "2021"
[dependencies]
winapi = { version = "0.3.9", features = ["winuser"] }
2. 主体
use winapi::um::winuser::{
SystemParametersInfoA as set,
SPI_SETDESKWALLPAPER as WALLPAPER,
SPIF_UPDATEINIFILE as UPDATE
};
unsafe {
set(WALLPAPER, 0, path, UPDATE);
}
本来以为很简单,但是不知道函数定义里的PVOID应该怎么传字符串进去,直接传肯定是不行的,看起来是C语言里面的(没想到怕麻烦选了rust,最后还是避不开C的东西)
pub fn SystemParametersInfoA(
uiAction: UINT,
uiParam: UINT,
pvParam: PVOID,
fWinIni: UINT,
) -> BOOL;
3. 传参
use std::ffi::CStr;
use winapi::ctypes::c_void;
let path = CStr::from_bytes_with_nul(b"D:\\1.png\0").unwrap();
let path = path.as_ptr() as *mut c_void;
另:部分情况下,在调用SystemParametersInfo
之前可能需要通过在注册表设置壁纸的拉伸或者铺平等参数
参考