设置桌面墙纸

    /// <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);
        }
    }
posted on 2008-09-19 17:37  迷你软件  阅读(427)  评论(0编辑  收藏  举报

本网站绝大部分资源来源于Internet,本站所有作品版权归原创作者所有!!如有以下内容:章节错误、非法内容、作者署名出错、版权疑问、作品内容有违相关法律等请及时与我联系. 我将在第一时间做出响应!本站所有文章观点不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。