我一直在尝试使用Qt和mingw32编写一个应用程序来下载图像并将其设置为背景壁纸。我在网上阅读了几篇关于如何在 VB 和 C# 中执行此操作的文章,以及在某种程度上如何在 C++ 中执行此操作。我目前正在使用似乎所有正确的参数(没有编译器错误)调用
SystemParametersInfo
,但它失败了。没有巨大的铙钹撞击声,只是返回了0
。 GetLastError()
返回同样具有启发性的 0
。
下面是我正在使用的代码(稍微修改过的形式,因此您不必查看对象内部)。
#include <windows.h>
#include <iostream>
#include <QString>
void setWall()
{
QString filepath = "C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";
char path[150];
strcpy(path, currentFilePath.toStdString().c_str());
char *pathp;
pathp = path;
cout << path;
int result;
result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, pathp, SPIF_UPDATEINIFILE);
if (result)
{
cout << "Wallpaper set";
}
else
{
cout << "Wallpaper not set";
cout << "SPI returned" << result;
}
}
可能
SystemParametersInfo
正在等待 LPWSTR
(指向 wchar_t
的指针)。
试试这个:
LPWSTR test = L"C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";
result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, test, SPIF_UPDATEINIFILE);
如果这有效(尝试使用几个不同的文件以确保),您需要将
char *
转换为 LPWSTR
。我不确定 Qt 是否提供这些服务,但一个可能有帮助的功能是 MultiByteToWideChar
。
"C:\Documents and Settings\Owner\My Documents\Wallpapers\wallpaper.png";
这不应该是:
"C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";
您可以使用
SetTimer
触发更改。
#define STRICT 1
#include <windows.h>
#include <iostream.h>
VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
LPWSTR wallpaper_file = L"C:\\Wallpapers\\wallpaper.png";
int return_value = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, wallpaper_file, SPIF_UPDATEINIFILE);
cout << "Programmatically change the desktop wallpaper periodically: " << dwTime << '\n';
cout.flush();
}
int main(int argc, char *argv[], char *envp[])
{
int Counter=0;
MSG Msg;
UINT TimerId = SetTimer(NULL, 0, 2000, &TimerProc); //2000 milliseconds
cout << "TimerId: " << TimerId << '\n';
if (!TimerId)
return 16;
while (GetMessage(&Msg, NULL, 0, 0))
{
++Counter;
if (Msg.message == WM_TIMER)
cout << "Counter: " << Counter << "; timer message\n";
else
cout << "Counter: " << Counter << "; message: " << Msg.message << '\n';
DispatchMessage(&Msg);
}
KillTimer(NULL, TimerId);
return 0;
}
std::string file_path = "C:\Image.png"; LPVOID FilePath = (LPVOID)file_path.c_str(); SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, FilePathBuffer, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);