Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change wallpaper programmatically using c++ and windows api

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;
    }
}
like image 574
Blue Peppers Avatar asked Jul 26 '10 01:07

Blue Peppers


People also ask

How do I change my background in C?

On most computers, you can change your background by right-clicking the desktop and selecting Personalize. Then select Desktop Background. By default, you'll see the images that were included with your computer. If you're looking for something specific, might we suggest Google Image Search?

How do I change the background of a GPO?

Click Start, click Run, and then type gpedit. msc. Under Local Computer Policy, expand User Configuration, expand Administrative Templates, expand Desktop, and then click Active Desktop. Double-click Active Desktop Wallpaper.

How do I change my background in Task Scheduler?

In Task Scheduler, click Action > Create Basic Task to create a new task. Enter the time when you want the wallpaper to appear. For example, if sunset takes place around 9 p.m. in your location, you might set the wallpaper to appear at 8 p.m.. Select whatever time you like.


2 Answers

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.

like image 138
dreamlax Avatar answered Oct 06 '22 09:10

dreamlax


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

shouldn't this be:

"C:\\Documents and Settings\\Owner\\My Documents\\Wallpapers\\wallpaper.png";
like image 44
sukru Avatar answered Oct 06 '22 09:10

sukru