if (SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "C:\\1.jpg", SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE))
{
cout << "成功" << endl;
}
else
{
cout << "失败" << GetLastError() << endl;
}
使用com接口解决这个问题
VOID SetWallpaper(TCHAR *szFilename, INT nStyle)
{
CoInitializeEx(0, COINIT_APARTMENTTHREADED);
IActiveDesktop* iADesktop;
HRESULT status = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**)&iADesktop);
WALLPAPEROPT wOption;
ZeroMemory(&wOption, sizeof(WALLPAPEROPT));
wOption.dwSize = sizeof(WALLPAPEROPT);
switch (nStyle)
{
case 0:
{
wOption.dwStyle = WPSTYLE_STRETCH;
}break;
case 1:
{
wOption.dwStyle = WPSTYLE_TILE;
}break;
case 2:
{
wOption.dwStyle = WPSTYLE_CENTER;
}break;
default:
wOption.dwStyle = WPSTYLE_STRETCH;
}
status = iADesktop->SetWallpaper(szFilename, 0);
status = iADesktop->SetWallpaperOptions(&wOption, 0);
status = iADesktop->ApplyChanges(AD_APPLY_ALL);
iADesktop->Release();
CoUninitialize();
}
引用资料:(V)C++ Set Desktop Wallpaper ( WIN 7 x64 ... [SOLVED] | DaniWeb