我一直在尝试编写一个应用程序,使用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;
}
}发布于 2010-07-26 09:20:14
可能是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。
发布于 2010-07-26 09:08:35
"C:\Documents and Settings\Owner\My Documents\Wallpapers\wallpaper.png";这不应该是:
"C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";发布于 2012-08-31 17:15:56
您可以使用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;
}https://stackoverflow.com/questions/3331682
复制相似问题