我已经尝试了这些帖子中指定的所有解决方案:
- How to change the windows 10 wallpaper with C++?
- How to change desktop background using VC++
- SystemParametersInfo sets wallpaper completly black (using SPI_SETDESKWALLPAPER)
而且我仍然似乎无法让它工作....这是我的代码:
const wchar_t* path = L"C:\\imagge.png";
bool result = SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, (void*)path, SPIF_UPDATEINIFILE);
std::cout << result;
此外,如果我指定一个无效的路径,当它在 docs 上明确说明时,它仍会打印 1(true) SPI_SETDESKWALLPAPER 位,如果他们有问题,它应该返回 0(false)
我也试过调用打印 GetLastError();
并返回 0....
Note When the SPI_SETDESKWALLPAPER flag is used, SystemParametersInfo returns TRUE unless there is an error (like when the specified file doesn't exist).
最佳答案
IInspectable 建议使用 IDesktopWallpaper interface
我成功了!
这是我的代码:
int main() {
std::wstring x = L"C:\\Users\\danie\\OneDrive\\Pictures\\pixelArt\\Sample.png";
HRESULT ad;
CoInitialize(NULL);
IDesktopWallpaper* p;
if(SUCCEEDED(CoCreateInstance(__uuidof(DesktopWallpaper), 0, CLSCTX_LOCAL_SERVER, __uuidof(IDesktopWallpaper), (void**)&p))) {
ad = p->SetWallpaper(NULL, x.c_str());
p->Release();
}
CoUninitialize();
return 0;
}
关于c++ - SystemParametersInfo 将背景设置为纯色而不是实际设置图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58921544/