用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 description for Form1.
///
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 = 0x2;
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;
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows Form Designer generated code
///
/// The main entry point for the application.
///
[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.EndsWith (".Jpeg")
)
{
Bitmap theImage = new Bitmap(fileName);
theImage.Save (localPath + " ew.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
fileName = localPath + " ew.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 + " ew.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 + " ew.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
// fileName = localPath + " ew.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 = 0x2;
} 

}



浙公网安备 33010602011771号