阿牛 - 专注.NET开发

如果梦想与实现之间有一道不可逾越的鸿沟,那么“执行力”就是跨越这道鸿沟的桥梁。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

参考了这篇文章:

http://blog.csdn.net/sonyicn/archive/2006/05/20/746280.aspx

 

现实代码如下:

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;

namespace RockLib
{
    
public class Utils
    
{
        
/// <summary>
        
/// Change user's desktop wallpaper
        
/// </summary>
        
/// <param name="picturePath"></param>
        
/// <param name="style"></param>
        
/// <returns></returns>

        public static bool ChangeWallPaper(string picturePath, WallPaperStyle style)
        
{
            RegistryKey myRegKey 
= Registry.CurrentUser.OpenSubKey("Control Panel\\desktop"true);
            
switch (style)
            
{
                
case WallPaperStyle.Center:
                    myRegKey.SetValue(
"TileWallpaper""0");
                    myRegKey.SetValue(
"WallpaperStyle""0");
                    
break;
                
case WallPaperStyle.Tile:
                    myRegKey.SetValue(
"TileWallpaper""1");
                    myRegKey.SetValue(
"WallpaperStyle""0");
                    
break;
                
case WallPaperStyle.Stretch:
                    myRegKey.SetValue(
"TileWallpaper""0");
                    myRegKey.SetValue(
"WallpaperStyle""2");
                    
break;
                
default:
                    
throw new NotSupportedException("style");                    
            }


          
            myRegKey.Close();

            
int intResult;
            
            
if (!File.Exists(picturePath))
            
{
                
throw new ArgumentException(string.Format("Picture {0} cannot be found.", picturePath));
            }


            intResult 
= SystemParametersInfo(203, picturePath, 0x1 | 0x2);

            
return intResult > 0;

        }


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


    
public enum WallPaperStyle
    
{
        Center,
        Tile,
        Stretch
    }

}

 

 

测试代码:

 

Code
posted on 2008-08-07 17:39  阿牛-专注金融行业开发  阅读(393)  评论(0编辑  收藏  举报