现在的位置: 首页 > 综合 > 正文

设置桌面墙纸

2011年12月14日 ⁄ 综合 ⁄ 共 2163字 ⁄ 字号 评论关闭
    /// <summary>
    
/// 设置桌面墙纸
    
/// </summary>
    public class Wallpaper
    {
        Wallpaper() { }

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

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

        public enum Style : int
        {
            
/// <summary>
            
/// 平铺
            
/// </summary>
            Tiled,
            
/// <summary>
            
/// 居中
            
/// </summary>
            Centered,
            
/// <summary>
            
/// 拉伸
            
/// </summary>
            Stretched
        }

        /// <summary>
        
/// 设置
        
/// </summary>
        
/// <param name="uri">URL</param>
        
/// <param name="style">位置类型</param>
        public static void Set(string uri, Style style)
        {
            System.IO.Stream s 
= new System.Net.WebClient().OpenRead(uri);

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

            Microsoft.Win32.RegistryKey key = Microsoft.Win32.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);
        }
    }

抱歉!评论已关闭.