Wednesday, November 23, 2005

set the wallpaper in C# using IActiveDesktop SetWallpaper

someone was wondering how to set the wall paper using the SetWallpaper function and didn't find any examples on the web, I decided to give it a try myself, armed with PInvoke and some Delphi sample code I found on the web, I came up with this solution.

You have to create a bunch of interfaces, enums, structs, etc that are defined in the IActiveDesktop interface and were originally in the shlobj.h, so I decided to create a new class to put all this stuff

you can download the class file here
after adding that to your project and adding
using Windows.Native; to the top of your class file, is as simple as:
private void button1_Click(object sender, System.EventArgs e) {
IActiveDesktop activeDesktop = shlobj.GetActiveDesktop();
activeDesktop.SetWallpaper(@"F:\somepic.jpg", 0);
activeDesktop.ApplyChanges(AD_Apply.ALLAD_Apply.FORCE);
}

No comments: