How can I change the desktop wallpaper programmatically?

Hi there! This is my first post on this board, so bear with me. If there is additional information need to clarify my problem just let me know.

I am creating a Win32 Form program in VC++ that changes the desktop wallpaper. I have gotten as far as getting all the necessary related values in the registry changed properly. I am at the point where I just need the desktop to update without a restart (which does update it BTW). I've researched this problem for eons it seems and will recount what I have attempted.

First off, SystemParametersInfo() seems to be the most correct way of updating the desktop, but in my experience I've tried everything to get this API function to work but cannot get past the barrier of calling native code from managed code. I've tried using the Marshalling class in attempts to inject the native code but still end up with calling method errors. Same thing with the IActiveDesktop, which I am not certain would work anyway. Importing the function will work in C# and such that I've seen it done this way by many creating the same type of application.

Here is how I am applying my updates to the registry to jump the discussion off:
System::Void btn_APPLY_Click(System::Object^ sender, System::EventArgs^ e)
RegistryKey^ settings = nullptr;
Image^ wallpaper = nullptr;
String^ savePath = Environment::GetEnvironmentVariable("USERPROFILE" ) + "\Local Settings\Application Data\Microsoft\Wallpaper1.bmp";

//Convert and save the selected image into bitmap format
wallpaper = pic_preview->Image;
wallpaper->Save( savePath, Imaging::ImageFormat::Bmp );

//Set the Wallpaper registry key value
settings = Registry::CurrentUser->OpenSubKey( L"Control Panel\Desktop\WindowMetrics", true );
settings->SetValue( L"Wallpaper", savePath );

//Update desktop

This is the Form's "Apply" button click event. If someone could provide me with a way to import the SystemParametersInfo() function into this code without the disgruntled compiler complaining about managed and native conglomerating I would greatly appreciate it! :D


  • This code is not Win32 at all.
    SystemParametersInfo is the official way.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion