Wallpaper Changer in C#

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace WallChange
{

/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button2;
public const int SPI_SETDESKWALLPAPER = 20;
public const int SPIF_SENDCHANGE = 0×2;
public int picIndex ;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.CheckedListBox mycheckedListBox;
private System.Windows.Forms.Button btnDel;
private System.Windows.Forms.Timer timer1;
string fileName;
string localPath;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.ComboBox cmbTime;
private int GlobalTimer;
private int timerInterval;
int[] myArray;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
GlobalTimer = 0;
timerInterval = 20;

myArray = new int[] {1, 2, 5, 10, 30 ,60 ,120 ,1440, 2880};
localPath = System.IO.Directory.GetCurrentDirectory();
picIndex = 0;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button2 = new System.Windows.Forms.Button();
this.mycheckedListBox = new System.Windows.Forms.CheckedListBox();
this.btnDel = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.label1 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.cmbTime = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 0;
this.button1.Text = "Change";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(8, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(48, 24);
this.button2.TabIndex = 2;
this.button2.Text = "&Open";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// mycheckedListBox
//
this.mycheckedListBox.ColumnWidth = 300;
this.mycheckedListBox.HorizontalScrollbar = true;
this.mycheckedListBox.Location = new System.Drawing.Point(8, 48);
this.mycheckedListBox.Name = "mycheckedListBox";
this.mycheckedListBox.Size = new System.Drawing.Size(440, 154);
this.mycheckedListBox.Sorted = true;
this.mycheckedListBox.TabIndex = 3;
this.mycheckedListBox.SelectedIndexChanged += new System.EventHandler(this.mycheckedListBox_SelectedIndexChanged);
//
// btnDel
//
this.btnDel.Location = new System.Drawing.Point(136, 8);
this.btnDel.Name = "btnDel";
this.btnDel.Size = new System.Drawing.Size(64, 24);
this.btnDel.TabIndex = 4;
this.btnDel.Text = "Delete";
this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 5000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// label1
//
this.label1.Location = new System.Drawing.Point(376, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 24);
this.label1.TabIndex = 5;
this.label1.Text = "Written By : Kfir Arbel";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(208, 8);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(80, 24);
this.checkBox1.TabIndex = 6;
this.checkBox1.Text = "Random";
//
// cmbTime
//
this.cmbTime.Items.AddRange(new object[] {
"1 min",
"2 min",
"5 min",
"10 min",
"30 min",
"1 hour",
"2 hour",
"1 Day",
"2 Day"});
this.cmbTime.Location = new System.Drawing.Point(280, 8);
this.cmbTime.Name = "cmbTime";
this.cmbTime.Size = new System.Drawing.Size(72, 21);
this.cmbTime.TabIndex = 7;
this.cmbTime.Text = "Time";
this.cmbTime.SelectedIndexChanged += new System.EventHandler(this.cmbTime_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(472, 221);
this.Controls.Add(this.cmbTime);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnDel);
this.Controls.Add(this.mycheckedListBox);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Form1";
this.Text = "WallPaper";
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{

//if (File.Exists(textBox1.Text))
//{
fSetBMP();
int nResult = WinAPI.SystemParametersInfo(SPI_SETDESKWALLPAPER,1,fileName,SPIF_SENDCHANGE);

UpdateFile();

}
private void fSetBMP()
{

picIndex = (picIndex + 1) % mycheckedListBox.CheckedItems.Count;
if (checkBox1.Checked == true )
{
Random rnd = new Random();

picIndex = ( rnd.Next(mycheckedListBox.CheckedItems.Count))
% mycheckedListBox.CheckedItems.Count;
}

mycheckedListBox.SelectedIndex = picIndex;
if (fileName.EndsWith (".jpg") ||
fileName.EndsWith (".JPG") ||
fileName.EndsWith (".Jpg") ||
fileName.EndsWith (".jpeg") ||
fileName.EndsWith (".JPEG") ||
fileName.En
dsWith (".Jpeg")
)
{
Bitmap theImage = new Bitmap(fileName);
theImage.Save (localPath + "\new.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
fileName = localPath + "\new.bmp";
}
}
private void UpdateFile()
{
using (FileStream fs = new FileStream (localPath + "\My.ini", FileMode.OpenOrCreate))
{

StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.Flush();
m_streamWriter.BaseStream.Seek(0,SeekOrigin.Begin);
m_streamWriter.WriteLine(picIndex.ToString());
m_streamWriter.Flush();
m_streamWriter.Close();

}
}
private void button2_Click(object sender, System.EventArgs e)
{
//change the initial directory if required.
openFileDialog1.InitialDirectory = @"c:\";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog1.FileName;
mycheckedListBox.Items.Add (fileName,CheckState.Checked);
UpdateFile();
// using (FileStream fs = new FileStream ("My.ini", FileMode.OpenOrCreate))
// {
//
// StreamWriter m_streamWriter = new StreamWriter(fs);
// m_streamWriter.Flush();
// m_streamWriter.BaseStream.Seek(0,SeekOrigin.Begin);
// //m_streamWriter.WriteLine(DateTime.Now.ToString() + ":");
// m_streamWriter.WriteLine(picIndex.ToString());
//
// for (int i=0; i< mycheckedListBox.CheckedItems.Count;i++)
// {
// //listBox1.Items.Add(mycheckedListBox.CheckedItems[i]);
// m_streamWriter.WriteLine(mycheckedListBox.Items[i].ToString());
// }
// m_streamWriter.Flush();
// m_streamWriter.Close();
// }

}

}

private void Form1_Load(object sender, System.EventArgs e)
{
if (File.Exists("My.ini"))
{

using (FileStream fs = new FileStream ("My.ini", FileMode.OpenOrCreate))
{
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0,SeekOrigin.Begin);
string strLine = m_streamReader.ReadLine();
picIndex = Int32.Parse(strLine);

m_streamReader.Close();

}
}

mycheckedListBox.Items.Clear();
fSetList("*.bmp");
fSetList("*.jpg");
fSetList("*.jpeg");

mycheckedListBox.SelectedIndex = picIndex;
fileName = mycheckedListBox.Items[picIndex].ToString();

}
private void fSetList(string strExt)
{
string[] dirs = Directory.GetFiles(localPath , strExt );

foreach (string dir in dirs)
{
if (dir.Equals (localPath + "\new.bmp") == false)
{
mycheckedListBox.Items.Add (dir ,CheckState.Checked);
}
}

}

public bool m_bcheckChangePossible = true;
private void mycheckedListBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (m_bcheckChangePossible)
fileName = mycheckedListBox.SelectedItem.ToString();
}

private void btnDel_Click(object sender, System.EventArgs e)
{
m_bcheckChangePossible = false;
int Index = mycheckedListBox.SelectedIndex;
mycheckedListBox.Items.RemoveAt(Index);
fileName = mycheckedListBox.SelectedIndex.ToString();
m_bcheckChangePossible = true;
}

private void timer1_Tick(object sender, System.EventArgs e)
{
GlobalTimer = GlobalTimer + 1;
if (GlobalTimer > timerInterval)
{
// int nResult ;
// if (fileName.EndsWith (".jpg"))
// {
// Bitmap theImage = new Bitmap(fileName);
// theImage.Save (localPath + "\new.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
// fileName = localPath + "\new.bmp";
// }
// //if (File.Exists(textBox1.Text))
// //{
// nResult = WinAPI.SystemParametersInfo(SPI_SETDESKWALLPAPER,1,fileName,SPIF_SENDCHANGE);
// picIndex = (picIndex + 1 ) % mycheckedListBox.CheckedItems.Count;
// mycheckedListBox.SelectedIndex = picIndex;

fSetBMP();
int nResult = WinAPI.SystemParametersInfo(SPI_SETDESKWALLPAPER,1,fileName,SPIF_SENDCHANGE);

UpdateFile();

GlobalTimer = 0;
}
}

private void cmbTime_SelectedIndexChanged(object sender, System.EventArgs e)
{

timerInterval = myArray [cmbTime.SelectedIndex] * 12;

}

}

public class WinAPI
{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SystemParametersInfo (int uAction , int uParam , string lpvParam , int fuWinIni) ;
public const int SPI_SETDESKWALLPAPER = 20;
public const int SPIF_SENDCHANGE = 0×2;

}

}

Most Commented Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!