首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用c++和windows api以编程方式更改墙纸

使用c++和windows api以编程方式更改墙纸
EN

Stack Overflow用户
提问于 2010-07-26 09:00:48
回答 3查看 22.3K关注 0票数 7

我一直在尝试编写一个应用程序,使用Qt和mingw32来下载图片并将其设置为背景墙纸。我在网上读了几篇文章,介绍了如何用VB和C#实现这一点,在某种程度上,也介绍了如何用c++实现。我目前正在使用似乎所有正确的参数(没有编译器错误)调用SystemParametersInfo,但它失败了。没有很大的锣声崩溃,只是返回了一个0GetLastError()返回一个同样具有启发性的0

下面是我使用的代码(略微修改过的形式,这样您就不必查看对象的内部结构)。

代码语言:javascript
运行
复制
#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;
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-07-26 09:20:14

可能是SystemParametersInfo需要一个LPWSTR (指向wchar_t的指针)。

试试这个:

代码语言:javascript
运行
复制
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

票数 9
EN

Stack Overflow用户

发布于 2010-07-26 09:08:35

代码语言:javascript
运行
复制
"C:\Documents and Settings\Owner\My Documents\Wallpapers\wallpaper.png";

这不应该是:

代码语言:javascript
运行
复制
"C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";
票数 2
EN

Stack Overflow用户

发布于 2012-08-31 17:15:56

您可以使用SetTimer来触发更改。

代码语言:javascript
运行
复制
#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;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3331682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档