Results 1 to 20 of 20

Thread: Setting wallpaper on windows

  1. #1
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Setting wallpaper on windows

    I've googled around a bit to try to find out how to set the desktop background in windows. The following is code that looks like it should work, but for some reason it won't.
    Qt Code:
    1. #include "wallpapr.h"
    2. #include <QSettings>
    3. #include <QVariant>
    4. #ifdef WIN32
    5. #include <windows.h>
    6.  
    7.  
    8. Wallpapr::Wallpapr(QWidget *parent)
    9. : QDialog(parent)
    10. {
    11. //ui.setupUi(this);
    12.  
    13. // String *path = "C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp";
    14. QString *path = new QString("C:\\Documents and Settings\\Marius\\Local Settings\\Application Data\\Microsoft\\Wallpaper1.bmp");
    15. //C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    16. QSettings *settings = new QSettings("HKEY_CURRENT_USER\\Control Panel\\Desktop",
    17. QSettings::NativeFormat);
    18. //ui.oldWallpaper->setText(settings->value("Wallpaper").toString());
    19. settings->setValue("Wallpaper", QVariant(*path));//"C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    20. //settings->setValue("ConvertedWallpaper", QVariant(*path));//"C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    21. //settings->setValue("OriginalWallpaper", QVariant(*path));//"C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    22. //ui.tempWallpaper->setText(settings->value("Wallpaper").toString());
    23. SystemParametersInfo(SPI_SETDESKWALLPAPER, 1, path, SPIF_SENDWININICHANGE);
    24. //ui.newWallpaper->setText(settings->value("Wallpaper").toString());
    25.  
    26. }
    27.  
    28. Wallpapr::~Wallpapr()
    29. {
    30.  
    31. }
    32. #endif
    To copy to clipboard, switch view to plain text mode 
    It changes the registry values, but only removes the current wallpaper. The desktop is a clear color after running the program.

    Anyone know how I could make it work?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting wallpaper on windows

    I doubt the WinAPI function takes a pointer to a QString. Are you sure this is correct?

  3. #3
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: Setting wallpaper on windows

    Well, it needs a pointer at least. And I thought QString was a subclass of String anyways.

    edit: I tried with a char pointer instead, but it didn't work either.

    Qt Code:
    1. #ifdef WIN32
    2. #include <windows.h>
    3.  
    4.  
    5. Wallpapr::Wallpapr(QWidget *parent)
    6. : QDialog(parent)
    7. {
    8. ui.setupUi(this);
    9. char *path = "C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp";
    10. // QString *path = new QString("C:\\Documents and Settings\\Marius\\Local Settings\\Application Data\\Microsoft\\Wallpaper1.bmp");
    11. //C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    12. QSettings *settings = new QSettings("HKEY_CURRENT_USER\\Control Panel\\Desktop",
    13. QSettings::NativeFormat);
    14. //ui.oldWallpaper->setText(settings->value("Wallpaper").toString());
    15. settings->setValue("Wallpaper", QString(path));//"C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    16. //settings->setValue("ConvertedWallpaper", QString(path));//"C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    17. //settings->setValue("OriginalWallpaper", QVariant(*path));//"C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    18. //ui.tempWallpaper->setText(settings->value("Wallpaper").toString());
    19. SystemParametersInfo(SPI_SETDESKWALLPAPER, 1, path, 0);//SPIF_SENDWININICHANGE);
    20. //ui.newWallpaper->setText(settings->value("Wallpaper").toString());
    21.  
    22. }
    23.  
    24. Wallpapr::~Wallpapr()
    25. {
    26.  
    27. }
    28. #endif
    To copy to clipboard, switch view to plain text mode 






    [Second edit] Is there a way to run a dll file from a qt program? According to this article there is a way to do it in a batch file. That means it should be a way to do it in c++ too.
    Last edited by ihoss; 10th June 2008 at 14:37.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting wallpaper on windows

    Quote Originally Posted by ihoss View Post
    Well, it needs a pointer at least.
    But probably not any pointer. Did you read the function's docs at msdn?

    And I thought QString was a subclass of String anyways.
    You were wrong. And char* is not a subclass of String as well (nor the other way round).

  5. #5
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: Setting wallpaper on windows

    I tried making a string from the char pointer. It doesn't change the current wallpaper, but it doesn't remove the previous one either. The function returns false. But if I set the second parameter to NULL it returns true. Nothing happens on the desktop though.

    Qt Code:
    1. #include "wallpapr.h"
    2. #include <QSettings>
    3. #include <QVariant>
    4. #include <QDirModel>
    5. #include <QFileDialog>
    6. #include <QPixmap>
    7. #include <iostream>
    8. #include <stdio.h>
    9. #include <string>
    10. #ifdef WIN32
    11. //#include <winuser.h>
    12. #include <Windows.h>
    13.  
    14. using namespace std;
    15.  
    16. Wallpapr::Wallpapr(QWidget *parent)
    17. : QDialog(parent)
    18. {
    19. ui.setupUi(this);
    20. char *path = "C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp";
    21. // QString *path = new QString("C:\\Documents and Settings\\Marius\\Local Settings\\Application Data\\Microsoft\\Wallpaper1.bmp");
    22. //C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    23. QSettings *settings = new QSettings(WALLPAPER, //"HKEY_CURRENT_USER\\Control Panel\\Desktop",
    24. QSettings::NativeFormat);
    25. //ui.oldWallpaper->setText(settings->value("Wallpaper").toString());
    26. settings->setValue("Wallpaper", QString(path));//"C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    27. settings->setValue("ConvertedWallpaper", QString(path));//"C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    28. settings->setValue("OriginalWallpaper", QString(path));//"C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.bmp");
    29. //ui.tempWallpaper->setText(settings->value("Wallpaper").toString());
    30. string *pathStr // = new string();
    31. = new string(path);
    32. //string *wp = new string();
    33. //SystemParametersInfo(SPI_GETDESKWALLPAPER, 100, wp, 0);
    34. //qdebug()<<wp;
    35. bool ret = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,
    36. pathStr,
    37. //NULL,
    38. //0);
    39. //SPIF_SENDCHANGE);
    40. //SPIF_SENDWININICHANGE);
    41. SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
    42. ui.newWallpaper->setText(ret ? "true" : "false");
    43.  
    44.  
    45. }
    46.  
    47. Wallpapr::~Wallpapr()
    48. {
    49.  
    50. }
    51. #endif
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting wallpaper on windows

    Did you read the function's description at msdn?

  7. #7
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: Setting wallpaper on windows

    Yup, if this is what you mean.

    Here is a list of the information I have found about setting the wallpaper:
    http://www.codeguru.com/forum/archiv.../t-415808.html
    http://www.gamedev.net/community/for...opic_id=365096
    http://blogs.msdn.com/coding4fun/arc...31/912569.aspx
    http://www.chami.com/tips/delphi/111696d.html
    http://windowsitpro.com/article/arti...er-policy.html

    Most of them also have links to other useful articles. But I stil can't get it to work.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting wallpaper on windows

    But have you identified your problem? Does the registry change when you use the function or not? If you relogin, do you see the old or the new wallpaper?

  9. #9
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: Setting wallpaper on windows

    Calling the function alone seems to have no effect; it doesn't change the current wallpaper, nor the wallpaper when I log off and log on again. The function also returns false. Changing the registry files only changes the wallpaper when I log off and log on again, but not immediately.


    I tried adding this to see what error I got, but all it returned was T.
    Qt Code:
    1. char* str = NULL; // function allocates the mem with LocalAlloc(), used with LocalFree()
    2. int msg_ret = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | // let the function alloc string mem
    3. FORMAT_MESSAGE_FROM_SYSTEM | // use system message and not a passed one
    4. FORMAT_MESSAGE_IGNORE_INSERTS, // ignore and "inserts"
    5. NULL,
    6. GetLastError(), // get the error
    7. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
    8. (WCHAR*)&str,
    9. 0,
    10. NULL);
    11. printf("The error is: %s", str);
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: Setting wallpaper on windows

    Ok, I took a few steps back and made a single c++ file that only changes the wallpaper, no GUI.

    Qt Code:
    1. #include <iostream>
    2. #include <stdio.h>
    3. #include <string>
    4. #include <Windows.h>
    5. using namespace std;
    6.  
    7. int main () {
    8. char *path = "C:\\WINDOWS\\Web\\Wallpaper\\Windows XP.jpg";
    9. string *pathStr = new string(path);
    10. bool ret = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,
    11. pathStr,
    12. //NULL,
    13. //0);
    14. //SPIF_SENDCHANGE);
    15. //SPIF_SENDWININICHANGE);
    16. SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
    17. cout<<"the function returned "<<(ret ? "true" : "false");
    18. char* str = NULL; // function allocates the mem with LocalAlloc(), used with LocalFree()
    19. int msg_ret = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | // let the function alloc string mem
    20. FORMAT_MESSAGE_FROM_SYSTEM | // use system message and not a passed one
    21. FORMAT_MESSAGE_IGNORE_INSERTS, // ignore and "inserts"
    22. NULL,
    23. GetLastError(), // get the error
    24. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
    25. (CHAR*)&str,
    26. 0,
    27. NULL);
    28. cout<<"\n"<<str;
    29. cout<<"\n"<<path;
    30. return 0;
    31. }
    To copy to clipboard, switch view to plain text mode 
    at it returns
    Qt Code:
    1. the function returned false
    2. The system cannot find the file specified.
    3.  
    4. C:\WINDOWS\Web\Wallpaper\Windows XP.jpg
    To copy to clipboard, switch view to plain text mode 
    But the file exists. I have also tried with bmp files, getting the same error message.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting wallpaper on windows

    As far as I know you can't set jpeg files as wallpapers this way. But if it doesn't work with BMP files, then something else has to be wrong. Try a simpler path such as C:\xyz.bmp

  12. #12
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: Setting wallpaper on windows

    Nope, not C:\Bliss.bmp or Bliss.bmp in the same folder. It could be that the functon expects something else than a char pointer.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting wallpaper on windows

    It might require a wchar pointer. I suggest using this function first to query for the existing wallpaper. This way you'll be able to see how the path is stored in the buffer you provide.

  14. #14
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: Setting wallpaper on windows

    Qt Code:
    1. #include <iostream>
    2. #include <stdio.h>
    3. #include <string>
    4. #include <Windows.h>
    5. using namespace std;
    6.  
    7. int main () {
    8. char *path = "C:\\Bliss.bmp";
    9. bool ret = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,
    10. path,
    11. //NULL,
    12. //0);
    13. //SPIF_SENDCHANGE);
    14. //SPIF_SENDWININICHANGE);
    15. SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
    16. cout<<"the function returned "<<(ret ? "true" : "false");
    17. char* str = NULL; // function allocates the mem with LocalAlloc(), used with LocalFree()
    18. int msg_ret = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | // let the function alloc string mem
    19. FORMAT_MESSAGE_FROM_SYSTEM | // use system message and not a passed one
    20. FORMAT_MESSAGE_IGNORE_INSERTS, // ignore and "inserts"
    21. NULL,
    22. GetLastError(), // get the error
    23. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
    24. (CHAR*)&str,
    25. 0,
    26. NULL);
    27. cout<<"\n"<<str;
    28. cout<<"\n"<<path;
    29. return 0;
    30. }
    To copy to clipboard, switch view to plain text mode 
    That code actually works! Great!

    But it does not work in QT.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting wallpaper on windows

    What do you mean "in Qt"? There is no "in Qt" The only problem that might arise is that some macro is included that influences the behaviour of something else, but this is not really a problem with Qt. Reordering includes might help in such situation.

  16. The following user says thank you to wysota for this useful post:

    ihoss (11th June 2008)

  17. #16
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: Setting wallpaper on windows

    Well, it works in its own little cpp file, but when I try to include it in the QT project, it won't work. I have tried to rearrange the includes, but it has no effects. I've also set the namespace to std, but it does not seem to be required to make the little cpp compile.

    edit: There is one strange thing though. In the little cpp i have to use CHAR as the 5th argument in the FormatMessage function, but in the "qt" program i have to use WCHAR. That is kinda strange when I include the same files and compile it with the same compiler.
    Last edited by ihoss; 11th June 2008 at 01:57.

  18. #17
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: Setting wallpaper on windows

    I tried to include the little cpp in the main project and just call the function from the application. It didn't work. I've also made sure both are compiled with mingw, and it only works when it is on its own.

  19. #18
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting wallpaper on windows

    Use SystemParametersInfoA or convert your QString properly to a WCHAR array with QString::utf16().

  20. The following 2 users say thank you to ChristianEhrlicher for this useful post:

    IGHOR (18th September 2009), ihoss (11th June 2008)

  21. #19
    Join Date
    Aug 2007
    Posts
    39
    Thanks
    8

    Default Re: Setting wallpaper on windows

    Wow, adding that A fixed the problem! Thanks a lot Christian. Thanks also to wysota for keeping dialog going

  22. #20
    Join Date
    Mar 2008
    Location
    Ukraine@online
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting wallpaper on windows

    Quote Originally Posted by ChristianEhrlicher View Post
    Use SystemParametersInfoA or convert your QString properly to a WCHAR array with QString::utf16().
    Thank's for this!
    IT work's:
    QString screenPath="C:\\screen.bmp";
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID)screenPath.utf16(), SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE)

Similar Threads

  1. Windows not appearing in XP.
    By beardybloke in forum Qt Programming
    Replies: 7
    Last Post: 24th October 2007, 18:32
  2. Setting default icon for all windows / dialogs
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2007, 11:04
  3. Windows change hardware acceleration setting
    By jakamph in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2006, 08:10
  4. converting unix exe to windows binary
    By deekayt in forum General Programming
    Replies: 2
    Last Post: 17th September 2006, 02:00
  5. Qt and windows vista
    By munna in forum General Discussion
    Replies: 8
    Last Post: 11th January 2006, 23:33

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.