using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using .Drawing2D;
using Microsoft.Win32;
using ;
using ;
using ;
using ;
namespace SetWallpaper
{
public partial class FrmMain : Form
{
[DllImport("", CharSet = )]
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
int screenWidth = ;
int screenHeight = ;
FileInfo[] picFiles;
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
List<DictionaryEntry> list = new List<DictionaryEntry>(){
new DictionaryEntry(1, "Center Show"),
new DictionaryEntry(2, "Tile Display"),
new DictionaryEntry(3, "Stretch Display")
};
= "Value";
= "Key";
= list;
= XmlNodeInnerText("");
+= new EventHandler(timer_Tick);
Text = ("Set desktop wallpaper (current computer resolution {0}×{1})", screenWidth, screenHeight);
}
/// <summary>
/// Browse individual images
/// </summary>
private void btnBrowse_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
= "Images (*.BMP;*.JPG)|*.BMP;*.JPG;";
= true;
= true;
if (() == )
{
Bitmap img = (Bitmap)();
= img;
string msg = ( != screenWidth || != screenHeight) ? ", it is recommended to select pictures that are consistent with the desktop resolution" : "";
= ("Current image resolution {0}×{1}{2}", , , msg);
}
}
}
/// <summary>
/// Manually set wallpaper
/// </summary>
private void btnSet_Click(object sender, EventArgs e)
{
if ( == null)
{
("Please select a picture first.");
return;
}
Image img = ;
SetWallpaper(img);
}
private void SetWallpaper(Image img)
{
Bitmap bmp = (img);
string filename = + "/";
(filename, );
string tileWallpaper = "0";
string wallpaperStyle = "0";
string selVal = ();
if (selVal == "1")
tileWallpaper = "1";
else if (selVal == "2")
wallpaperStyle = "2";
//Write to the registry to avoid failure after system restart
RegistryKey regKey = ;
regKey = ("Control Panel\\Desktop");
//Display method, centered: 0 0, tiling: 1 0, stretching: 0 2
("TileWallpaper", tileWallpaper);
("WallpaperStyle", wallpaperStyle);
("Wallpaper", filename);
();
SystemParametersInfo(20, 1, filename, 1);
}
/// <summary>
/// Browse folders
/// </summary>
private void btnBrowseDir_Click(object sender, EventArgs e)
{
string defaultfilePath = XmlNodeInnerText("");
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
if (defaultfilePath != "")
= defaultfilePath;
if (() == )
XmlNodeInnerText();
= ;
}
}
/// <summary>
/// Get or set the selection directory in the configuration file
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public string XmlNodeInnerText(string text)
{
string filename = + "/";
XmlDocument doc = new XmlDocument();
if (!(filename))
{
XmlDeclaration dec = ("1.0", "UTF-8", null);
(dec);
XmlElement elem = ("WallpaperPath");
= text;
(elem);
(filename);
}
else
{
(filename);
XmlNode node = ("//WallpaperPath");
if (node != null)
{
if ((text))
return ;
else
{
= text;
(filename);
}
}
}
return text;
}
/// <summary>
/// Automatically set wallpapers
/// </summary>
private void btnAutoSet_Click(object sender, EventArgs e)
{
string path = ;
if (!(path))
{
("The selected folder does not exist");
return;
}
DirectoryInfo dirInfo = new DirectoryInfo(path);
picFiles = ("*.jpg");
if ( == 0)
{
("There is no picture in the selected folder");
return;
}
if ( == "Start")
{
();
= "Stop";
= ("Regularly automatic wallpaper change...");
}
else
{
();
= "Start";
= "";
}
}
/// <summary>
/// Set wallpapers at random time
/// </summary>
private void timer_Tick(object sender, EventArgs e)
{
= 1000 * 60 * (int);
FileInfo[] files = picFiles;
if ( > 0)
{
Random random = new Random();
int r = (1, );
Bitmap img = (Bitmap)(files[r].FullName);
= img;
SetWallpaper(img);
}
}
/// <summary>
/// Double-click the tray icon to display the form
/// </summary>
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowForm();
}
/// <summary>
/// Hide the form and display the tray icon
/// </summary>
private void HideForm()
{
= false;
= ;
= true;
}
/// <summary>
/// Display the form
/// </summary>
private void ShowForm()
{
= true;
= ;
= false;
}
private void ToolStripMenuItemShow_Click(object sender, EventArgs e)
{
ShowForm();
}
/// <summary>
/// quit
/// </summary>
private void toolStripMenuItemExit_MouseDown(object sender, MouseEventArgs e)
{
();
}
/// <summary>
/// Hide the form when minimized and display the tray icon
/// </summary>
private void FrmMain_SizeChanged(object sender, EventArgs e)
{
if ( == )
{
HideForm();
}
}
}
}