CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2007
    Posts
    28

    Change Wallpaper

    I am using SystemParametersInfo(SPI_SETDESKWALLPAPER,0,path,SPIF_SENDCHANGE);
    to change the desktop in C++.

    My question is how to set the wallpaper options Position(Stretched or Centered or Titled)
    Last edited by skpskpskp; January 10th, 2008 at 02:20 PM.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Change Wallpaper

    One way:
    Code:
    USES_CONVERSION;  // First line in your function
    ...
    IActiveDesktop *pActiveDesktop = NULL;
    HRESULT hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
          IID_IActiveDesktop, (void**)&pActiveDesktop);
    if (hr == S_OK)
    {
      WALLPAPEROPT wallpaperopt = {0};
      wallpaperopt.dwSize = sizeof(WALLPAPEROPT);
      wallpaperopt.dwStyle = WPSTYLE_TILE;
      pActiveDesktop->SetWallpaperOptions(&wallpaperopt, NULL);
      LPWSTR wstrImg = T2W(pszFile);
      pActiveDesktop->SetWallpaper(wstrImg, NULL);
      pActiveDesktop->ApplyChanges(AD_APPLY_ALL);
      pActiveDesktop->Release();
    }
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Aug 2007
    Posts
    28

    Re: Change Wallpaper

    Thanks,but because i have been learning WinAPI since last week and I understand 90% of your code and if you have time to explain every line and function i will be very grateful

    Can you give me some links with more informations about it to read
    Last edited by skpskpskp; January 10th, 2008 at 02:55 PM.

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Change Wallpaper

    Well, the first line "USES_CONVERSION" is needed because I'm using T2W. See MSDN for the T2W explanation.

    Then I get a IActiveDesktop interface using CoCreateInstance. Again, see MSDN for more information on CoCreateInstance.

    Once you have your interface, we can start calling functions on it and one of its functions is SetWallpaperOptions which accepts a WALLPAPEROPT structure with information.

    Then I use the SetWallpaper function to set the actual wallpaper and use ApplyChanges to instruct Windows to apply the changes and refresh the desktop.

    When you're finished with an interface obtained through CoCreateInstance you should call Release on it.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Feb 2008
    Posts
    4

    Re: Change Wallpaper

    i couldn't figure out, is including shlobj.h enough for that?

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Change Wallpaper

    Yes, you have to include shlobj.h.
    Do you have any compilation errors?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    Join Date
    Feb 2008
    Posts
    4

    Re: Change Wallpaper

    yes i did have some, it said "error C2065: 'IActiveDesktop' : undeclared identifier" for example, but i figured out the problem about SystemParametersInfo, so everythings ok now.

    by the way, thank you for your interest in my problem, and your replies.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured