【问题标题】:Change wallpaper programmatically using c++ and windows api使用 c++ 和 windows api 以编程方式更改壁纸
【发布时间】:2011-03-20 21:19:46
【问题描述】:

我一直在尝试使用 Qt 和 mingw32 编写一个应用程序来下载图像并将它们设置为背景墙纸。我已经在网上阅读了几篇关于如何在 VB 和 C# 中执行此操作的文章,以及在某种程度上如何在 C++ 中执行此操作。我目前正在调用SystemParametersInfo,似乎所有的参数都是正确的(没有编译器错误),但它失败了。没有大钹崩溃,只是返回了0GetLastError() 返回同样具有启发性的 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;
    }
}

【问题讨论】:

  • 你试过用位图文件而不是 png/jpg 吗?

标签: c++ winapi qt wallpaper desktop-wallpaper


【解决方案1】:

可能是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

【讨论】:

    【解决方案2】:
    "C:\Documents and Settings\Owner\My Documents\Wallpapers\wallpaper.png";
    

    这不应该是:

    "C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";
    

    【讨论】:

    • 哦,真的。但这不是错误。在实际程序中,QString 由不同的函数正确填充 :) 但是发现我的错误是值得称赞的 :)
    【解决方案3】:

    您可以使用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;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-03
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多