Click here to Skip to main content
Click here to Skip to main content

WallPaper Changer for .NET

, 29 Apr 2005
Rate this:
Please Sign up or sign in to vote.
An article about changing the desktop wallpaper.

Sample Image

Introduction

This is the second version to the application. I made changes according to the remarks posted by the viewers.

This is an attempt to create a simple application that has a simple GUI and still does all the desired functionality. I wanted an application that collects the images by itself and spares me that feature.

The following subjects are discussed in this article:

  1. Setting a wallpaper using WinAPI.
  2. Using xmlWriter for file writing.
  3. Image File type changing.
  4. Getting all the files from a directory recursively.
  5. Separating files from directories in a collection.
  6. Implementing Drag & Drop scenario.
  7. Showing the application in the traybar.

Background

To change the Desktop background, the application needs to use the WinAPI SystemParametersInfo and supply it with the desired image parameters. The only problem is that the API accepts only the BMP image type. My solution was to create a BMP from each JPG or JPEG file in the directory.

Using the code

  1. Setting a wallpaper using WinAPI:

    The API function "SystemParametersInfo" has many usages. I will use the SPI_SETDESKWALLPAPER parameter which tells the system to change the wallpaper. The SPIF_SENDCHANGE parameter tells the system to send all the open windows an acknowledgement message about the wallpaper changing.

    //
    //
    nResult = WinAPI.SystemParametersInfo(SPI_SETDESKWALLPAPER, 
                                 1, fileName, SPIF_SENDCHANGE);
    //
  2. Using xmlTextWriter for file writing:

    My xml file holds the last selected image that was wallpapered. After each wallpaper changing, the xml file will be updated so that the next time the application uses the proper image. The xmlTextwriter must start with writer.WriteStartElement and end with writer.WriteEndElement. In the middle, each element is added using its name and value: "picIndex", tempPicIndex.ToString(). The flush method forces the writer to immediately write the data. The Close method closes the writer.

    XmlTextWriter writer = 
           new XmlTextWriter(m_settingsPath + "\\settings.xml", null);
    writer.Formatting = Formatting.Indented;
    writer.Indentation = 4;
    writer.WriteComment("Settings for WallPaper changer program");
    writer.WriteStartElement("settings");
    writer.WriteElementString("picIndex", tempPicIndex.ToString());
    writer.WriteElementString("Interval", tempGlobalInterval.ToString());
    writer.WriteEndElement();
    writer.Flush();
    writer.Close();
  3. Image File type changing:

    First, you need to create a BMP file in the memory using the Bitmap constructor, and then you need to save the new file to the local HD.

    Bitmap theImage = new Bitmap(fileName);
    theImage.Save (localPath + "\\new.bmp",
          System.Drawing.Imaging.ImageFormat.Bmp);
  4. Getting all the files from a directory recursively:

    In order to get all the files from all sub-directories, the application needs to use the Directory class. I used a recursive method to gather all files. I got all the files from each sub-directory to a string array and added each one of them to the ListBox.

    string[] dirs = Directory.GetFiles(localPath , fileType);
    foreach (string dir in dirs){
        mycheckedListBox.Items.Add (dir ,CheckState.Checked);
    }
  5. Separating file from directories in a collection:

    For each of the files, the application checks if it is a file or a directory by the FileAttributes property. In the following code, the application casts the string named dir and checks if it is file or directory by its fileattributes.Directory flag:

    if (File.GetAttributes(dir) == FileAttributes.Directory) bAns = true;
  6. Implementing Drag & Drop scenario:

    First of all, in the Form Designer mode, the listbox.allowDrag and the form.allowDrag must be set to true. The next step is to change the DragAndDrop effect (and the icon of the cursor) and allow the drop scenario to continue. Otherwise the DragDrop event will never be reached.

    private void mycheckedListBox_DragEnter(object sender, 
                       System.Windows.Forms.DragEventArgs e) 
    {
      if (e.Data.GetDataPresent(DataFormats.FileDrop, false)==true) 
          e.Effect = DragDropEffects.All;
    }

    Finally, I have implemented the DragDrop event to get all the files to a string ArrayList:

    private void mycheckedListBox_DragDrop(object sender, 
                                System.Windows.Forms.DragEventArgs e)
    {
       string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
       foreach (string dirfile in files) { ....}
    }
  7. Showing the application in the traybar:

    C# has wrapped all the procedures to a very simple one. All you need to do is add notifyIcon to your application's form and to relate it to contextMenu. The next step is to implement the Form.resize and make sure that when minimized the window will disappear:

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      if (WindowState == FormWindowState.Minimized) 
        this.Hide();
    }

    You will probably want to implement the notifyIcon.DoubleClick also.

    private void notifyIcon1_DoubleClick(object sender, System.EventArgs e) 
    {
      this.Show(); 
      this.WindowState = FormWindowState.Normal;
    }
    

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

arbel kfir
Web Developer
Israel Israel
partying, girls, partying, girls
 
and a bit of c#
 
homepage : http:\\www.myi4u.com

Comments and Discussions

 
Generalhi Pinmembersandeepparekh2-Jun-11 22:19 
Generalno worky with my redirected desktop and offline files.. Pinmemberehp39-Oct-07 7:04 
GeneralI want to have a calendar in desktop PinmemberDungVinh17-Aug-06 21:54 
QuestionCentered Image ? PinmemberAntheo15-Jul-05 7:15 
AnswerRe: Centered Image ? Pinmembertombala20-Feb-06 9:07 
AnswerRe: Centered Image ? Pinmembertombala20-Feb-06 9:34 
GeneralRe: Centered Image ? Pinmembertombala20-Feb-06 10:09 
QuestionMemory? PinsussAnonymous12-Jul-05 6:46 
AnswerRe: Memory? Pinsusskfir arbel25-Aug-05 5:15 
GeneralRe: Memory? PinmemberSantosh K Sahoo7-Aug-06 21:08 
GeneralRe: Memory? Pinmemberaskhenry11-Aug-07 5:48 
GeneralRecursive Directory Import PinsussSven Rieke12-Apr-05 23:22 
GeneralRe: Recursive Directory Import PinmemberJens Scheidtmann13-Apr-05 4:17 
GeneralRe: Recursive Directory Import PinmemberJens Scheidtmann13-Apr-05 4:18 
GeneralRe: Recursive Directory Import Pinmembermorphix13-Apr-05 22:45 
GeneralRe: Recursive Directory Import Pinmemberarbel kfir14-Apr-05 10:06 
GeneralRe: Recursive Directory Import Pinmemberjames32517-Apr-05 15:43 
GeneralRe: Recursive Directory Import PinmemberVladislav Borovikov2-May-05 4:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 29 Apr 2005
Article Copyright 2005 by arbel kfir
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid