0

I am trying to change the desktop background/wallpaper to a different image with a .png file. Although when I run the program, the background turns to solid black instead.

I am certain that I typed the file name, "ksa.png", correctly in my code to be the image I want to be on my background. I used an if condition to write out the last error on a file when the error occurred and used an else condition to write out "Success" if no errors occurred; but when I run the program, it writes "Success" to the file. I have thought about using a .jpg file instead, thinking that maybe .png files just don't work. I'll give an update when I tried using that.

#include <windows.h>
#include <fstream>

int main () {
    const wchar_t *filenm = L"ksa.png";
    std::ofstream log;
    if (SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)filenm, SPIF_UPDATEINIFILE) == FALSE) {
        log.open("log.txt");
        log << "Error: " << GetLastError();
        log.close();
    }
    else {
        log.open("log.txt");
        log << "Success";
        log.close();
    }
    return 0;
}

When I run this program, the desktop background is suppose to be set as the image "ksa.png". Instead it's solid black. Any help is appreciated for making this work, thank you.

UPDATE

Okay so I updated the code to where it would run a .jpg file and I'm still getting the same result. Also I moved the line log.open("log.txt") command before the SystemParametersInfo() function like Remy Lebeau suggested and it still writes out "Success" to the file. I'm still having the same problem. Here is my updated code:

#include <windows.h>
#include <fstream>

int main () {
    const wchar_t *filenm = L"3.jpg";
    std::ofstream log;
    log.open("log.txt");
    if (SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)filenm, SPIF_UPDATEINIFILE) == FALSE) {
        log << "Error: " << GetLastError();
        log.close();
    }
    else {
        log.open("log.txt");
        log << "Success";
        log.close();
    }
    return 0;
}
Community
  • 1
  • 1
  • You can't use a PNG file for the wallpaper. Use a JPG or BMP file instead. Also, if an error did occur, your call to `log.open("log.txt")` would likely wipe out the error code that `GetLastError()` would have returned. You need to call `GetLastError()` before opening the log file, or open the log file before calling `SystemParametersInfo()`. – Remy Lebeau Apr 02 '19 at 21:57
  • 2
    Where is your image file located? You probably need to use an absolute (fully-qualified) path, not a relative one. – Cody Gray - on strike Apr 03 '19 at 03:06

1 Answers1

0

Emmmm,there is a problem with your picture path. I've tried your code. You can't get pictures under relative paths unless you use absolute paths.

Like Cody Gray♦'s judgment .

const wchar_t *filenm = L"C:\\Users\\strives\\Desktop\\timg.bmp";
Strive Sun
  • 5,988
  • 1
  • 9
  • 26