将bing每日壁纸设置为windows的桌面壁纸

刚刚看了 kongkongyzt  的文章 《使用java将bing每日壁纸设置为ubuntu13.04的桌面壁纸》,想法很不错。只是平时大部分时间还是在windows下工作。所以,用C#写了一个windows版本的。让你的桌面每日一新。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;
using System.Drawing;

namespace BingWallpaper
{
    class Program
    {

        const string URL_FETCH_BING_IMG = "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=8";
        private static int n;

        static void Main(string[] args)
        {
            receiveArgs(args);
            string relativePath = fetchUrl(n);
            string path = "http://www.bing.com/" + relativePath;
            Wallpaper.Set(new Uri(path), Wallpaper.Style.Stretched);
        }

        static void receiveArgs(string[] args)
        {
            int i;
            if (args.Length == 0)
            {
                i = 0;
            }
            else
            {
                try
                {
                    i = int.Parse(args[0]);
                }
                catch (System.FormatException e)
                {
                    Console.WriteLine("The first argument n must be an integer. Range of n is [0,7].");
                    i = 0;
                }
            }
            if (i < 0) i = 0;
            if (i > 7) i = 7;

            n = i;
        }

        static string fetchUrl(int n)
        {
            XElement root = XElement.Load(URL_FETCH_BING_IMG);
            IEnumerable<XElement> images = root.Elements("image");
            XElement url = (images.ToList())[n].Element("url");
            return url.Value;
        }

    }


    class Wallpaper
    {
        Wallpaper() { }

        const int SPI_SETDESKWALLPAPER = 20;
        const int SPIF_UPDATEINIFILE = 0x01;
        const int SPIF_SENDWININICHANGE = 0x02;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

        public enum Style : int
        {
            Tiled,
            Centered,
            Stretched
        }

        public static void Set(Uri uri, Style style)
        {
            System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString());

            System.Drawing.Image img = System.Drawing.Image.FromStream(s);
            string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
            img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);

            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
            if (style == Style.Stretched)
            {
                key.SetValue(@"WallpaperStyle", 2.ToString());
                key.SetValue(@"TileWallpaper", 0.ToString());
            }

            if (style == Style.Centered)
            {
                key.SetValue(@"WallpaperStyle", 1.ToString());
                key.SetValue(@"TileWallpaper", 0.ToString());
            }

            if (style == Style.Tiled)
            {
                key.SetValue(@"WallpaperStyle", 1.ToString());
                key.SetValue(@"TileWallpaper", 1.ToString());
            }

            SystemParametersInfo(SPI_SETDESKWALLPAPER,
                0,
                tempPath,
                SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }


    }
}

当然,你可以下载我直接编译后的版本(windows xp x86):

https://www.dropbox.com/s/xbljioj3dkzqmiu/BingWallpaper.exe

md5: cda11e5ad7dac08f7ccd35e281111c10

你可以把它加到启动项里面去,这样你每天开机后,看到的都是一张新桌面哦。


你可能感兴趣的:(.net,C#,bing,wallpaper)