Change wallpaper programmatically using c++ and windows api
Asked Answered
M

4

10

I've been trying to write an application, using Qt and mingw32, to download images and set them as the background Wallpaper. I have read several articles online about how to do this, in VB and C#, and to some extent how to do it in c++. I am currently calling the SystemParametersInfo with what seems to be all the correct arguments (no compiler errors) and it fails. No great crash of cymbals, just a 0 returned. GetLastError() returns an equally enlightening 0.

Below is the code I am using (In a slightly modified form, so you do not have to view the object internals).

#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;
    }
}
Millisecond answered 26/7, 2010 at 1:0 Comment(0)
R
10

It could be that SystemParametersInfo is expecting an LPWSTR (a pointer to wchar_t).

Try this:

LPWSTR test = L"C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";

result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, test, SPIF_UPDATEINIFILE);

If this works (try it with a few different files just to make sure), you'll need to convert your char * to a LPWSTR. I'm not sure if Qt offers these services, but one function that may help is MultiByteToWideChar.

Republic answered 26/7, 2010 at 1:20 Comment(0)
N
2
"C:\Documents and Settings\Owner\My Documents\Wallpapers\wallpaper.png";

shouldn't this be:

"C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";
Normie answered 26/7, 2010 at 1:8 Comment(0)
P
0

You cn use SetTimer to trigger a change.

#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;
}
Purnell answered 31/8, 2012 at 9:15 Comment(0)
C
-1
std::string file_path = "C:\\Image.png";
LPVOID FilePath = (LPVOID)file_path.c_str();
SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, FilePathBuffer, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
Clarindaclarine answered 14/9, 2023 at 19:9 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Foundry

© 2022 - 2024 — McMap. All rights reserved.