#include <windows.h>
#include <shobjidl.h> // 包含IDesktopWallpaper接口定义
#include <wincodec.h> // WIC头文件
#include <iostream>
bool SetWallpaper1(const wchar_t* imagePath) {
// SPI_SETDESKWALLPAPER = 0x0014
BOOL result = SystemParametersInfoW(
SPI_SETDESKWALLPAPER,
0,
(PVOID)imagePath,
SPIF_UPDATEINIFILE | SPIF_SENDCHANGE
);
if (!result) {
std::wcerr << L"错误: " << GetLastError() << std::endl;
return false;
}
return true;
}
bool SetWallpaper2(const wchar_t* imagePath) {
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr)) return false;
IDesktopWallpaper* pWallpaper = nullptr;
hr = CoCreateInstance(
__uuidof(DesktopWallpaper),
nullptr,
CLSCTX_ALL,
__uuidof(IDesktopWallpaper),
reinterpret_cast<void**>(&pWallpaper)
);
if (SUCCEEDED(hr)) {
hr = pWallpaper->SetWallpaper(nullptr, imagePath);
pWallpaper->Release();
}
CoUninitialize();
return SUCCEEDED(hr);
}