|
发表于 2022-10-26 09:42:00
|
显示全部楼层
bool SetDesktopWallpaperW(
const wchar_t* fileName,
EWallpaperStyle style)
{
// HKEY_CURRENT_USER\\Control Panel\\Desktop
struct SRegParam
{
const wchar_t* style; // WallpaperStyle 的值
const wchar_t* tile; // TileWallpaper 的值
};
static const SRegParam regParams[EWallpaperStyleLast] =
{
{ L"0", L"0"}, // 居中
{ L"0", L"1" }, // 平铺
{ L"2", L"0" }, // 拉伸
{ L"6", L"0" }, // 适应
{ L"10", L"0" }, // 填充
};
if (!fileName ||
!PathFileExistsW(fileName) ||
style < 0 ||
style >= EWallpaperStyleLast)
return false;
HKEY hKey = NULL;
bool ret = false;
do
{
if (ERROR_SUCCESS != RegOpenKeyW(
HKEY_CURRENT_USER,
L"Control Panel\\Desktop",
&hKey))
break;
if (ERROR_SUCCESS != RegSetValueExW(
hKey,
L"WallPaper",
0,
REG_SZ,
(const BYTE*)fileName,
wcslen(fileName) * sizeof(wchar_t)))
break;
if (ERROR_SUCCESS != RegSetValueExW(
hKey,
L"WallpaperStyle",
0,
REG_SZ,
(const BYTE*)regParams[style].style,
wcslen(regParams[style].style) * sizeof(wchar_t)))
break;
if (ERROR_SUCCESS != RegSetValueExW(
hKey,
L"TileWallpaper",
0,
REG_SZ,
(const BYTE*)regParams[style].tile,
wcslen(regParams[style].tile) * sizeof(wchar_t)))
break;
ret = true;
} while (0);
if (hKey)
RegCloseKey(hKey);
if (ret)
{
if (!SystemParametersInfoW(
SPI_SETDESKWALLPAPER,
0,
(PVOID)fileName,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE | SPIF_SENDCHANGE))
ret = false;
}
return ret;
}
|
|