.:: Rotate Windows XP Wallpaper using C# .NET (CSharp) ::.
Project Name:
JD’s Wallpaper Changer (download complete solution)
Situation:
Displaying wallpaper on the Windows XP desktop can enhance the day-to-day experience of using the PC. However, it’s easy to desktop images to rapidly become stale and maintaining the rotation of images manually can be a pain. Multiple programs are available to help manage desktop wallpaper, but most of these applications are ridden with spy-ware and ad-ware. Based on these factors, I decided to write a basic, simple application to help me rotate images I would like to set as my wallpaper.
Goals:
- Rotate wallpaper images on a user set interval
- Retrieve available wallpaper images from a single directory allowing for easy maintenance
- Facilitate all UI using a NotifyIcon
Underlying Technology:
Capabilities to adjust the Windows desktop wallpaper are not included with v1.1 of the .NET framework. To set the wallpaper, Microsoft has provided an API that requires a series of P/Invoke commands. To eliminate the complication of this process I have created a class file using code from Steve Dunn’s Desktop Decorator. I have created a class file that encapsulates his code. Download Wallpaper.cs by clicking here.
The only UI JD’s Wallpaper Changer utilizes is a NotifyIcon which displays an icon in the SysTray. All commands and messages can be relayed using this technology.
Changing the desktop image on a specified interval requires use of the Timer control.
The Code:
Hiding the Windows From app and displaying only the NotifyIcon:
Hide();
sysTrayIcon.Visible = true;
The first user method launched is retrieving the application settings from the App.config file distributed with the application. App.config has just three settings which are self explanatory.
<appSettings>
<add key="ImagesDirectory"
value="C:\My Documents\My Pictures\Desktop Images" />
<add key="ImageChangeInterval" value="1800" />
<add key="AllowedExtensions"
value=".jpg;.jpeg;.gif;.bmp" />
</appSettings>The method to load the application settings:
private int imageChangeInt;
private string imageDirectory;
private string[] allowedExts;
private void loadConfigSettings()
{
try
{
imageDirectory =
ConfigurationSettings.AppSettings["ImagesDirectory"];
imageChangeInt = Convert.ToInt32(
ConfigurationSettings.AppSettings["ImageChangeInterval"]);
allowedExts =
ConfigurationSettings.AppSettings[
"AllowedExtensions"].Split(
";".ToCharArray());
}
catch (Exception ex)
{
MessageBox.Show("The application encountered an error
loading the confiuration settings.
Please verify the settings in the App.config file",
"JD's Wallpaper Changer",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
}Finally, setting the wallpaper is relatively easy:
private void setWallPaper()
{
string fileName = getNextImageFilename();
Wallpaper.Set(new Uri(fileName), Wallpaper.Style.Stretched);
if (fileName.Length >= 64)
sysTrayIcon.Text =
string.Format("... {0}",
fileName.Substring(fileName.Length - 59, 59));
else
sysTrayIcon.Text = fileName;
}Links and downloads:
JD’s Wallpaper Changer complete Visual Studio 2003 C# solution downloadCode Project