闲来无事之自动换桌面墙纸--WallpaperAutoChanger

今天闲来无事,觉得自己的桌面很是单调,于是自己动手做了一个自动换桌面墙纸的小工具。工具的作法相当简单,主要是想跟大家分享一下。 大家口下留情,我知道很简单,所以,高手就Pass了吧。

      用到的最主要的函数就是Windows API中的SystemParametersInfo函数。首先在。Net中调用API函数:

 

1         [DllImport( " user32.dll " , EntryPoint  =   " SystemParametersInfoA " )]
2          static   extern  Int32 SystemParametersInfo(Int32 uAction, Int32 uParam,  string  lpvparam, Int32 fuwinIni);

 

然后定义常变量

private   const   int  SPI_SETDESKWALLPAPER  =   20 ;

为什么是20呢?嘿嘿,查看API手册就知道了。

最后根据图片文件路径更换墙纸


 1          // 设置桌面背景
 2          public   static   void  SetDeskWallpaper( string  filename)
 3          {
 4             FileInfo file  =   new  FileInfo(filename);
 5             Image image  =  Image.FromFile(filename);
 6              string  temp  =   @" Wallpaper.bmp " ;
 7             image.Save(temp, System.Drawing.Imaging.ImageFormat.Bmp);
 8             SystemParametersInfo(SPI_SETDESKWALLPAPER,  0 , temp,  1 );
 9             File.Delete(temp);
10         }

 这里需要注意的是xp只支持bmp图像设成墙纸(Vista中支持jpg,MSDN说的)。所以我先把图像另存成一个bmp临时图像,然后再设置墙纸成功后删除临时文件。

墙纸更更换频率由一个Timer在确定,这么简单,我说不哆嗦了。

 

上面就是一个小截图。嘿嘿。

然后加上一个开机自启动的功能。方法就是在注册表LocalMachine//SOFTWARE//Microsoft//Windows//CurrentVersion//Run中加入自己的启动路径就是了,是不是很简单?下面就是代码


 1          bool  AutoRun  =   false ;
 2         RegistryKey RegKey  =  Registry.LocalMachine.OpenSubKey( " SOFTWARE//Microsoft//Windows//CurrentVersion//Run " true );
 3          string  RegKeyName  =  Application.ExecutablePath; // 获得可执行文件路径
 4
 5   private   void  开机启动AToolStripMenuItem_Click( object  sender, EventArgs e)
 6          {
 7              try
 8              {
 9                 AutoRun  =   ! AutoRun;
10                  if  (RegKey  ==   null )
11                     RegKey  =  Registry.LocalMachine.CreateSubKey( " SOFTWARE//Microsoft//Windows//CurrentVersion//Run " );
12                  if  ( ! AutoRun)
13                  {
14                     RegKey.DeleteValue( " WallpaperAutoChanger " false );
15                      this .msiAutoRun.ForeColor  =  Color.Gray;
16                      this .msiAutoRun.Text  =   " 开机自启动 " ;
17                     MessageBox.Show( " 已取消开机自启动,重新启动计算机后即可生效! " " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
18                 }

19                  else
20                  {
21                     RegKey.SetValue( " WallpaperAutoChanger " , RegKeyName);
22                      this .msiAutoRun.ForeColor  =  Color.Red;
23                      this .msiAutoRun.Text  =   " √开机自启动 " ;
24                     MessageBox.Show( " 已设置开机自启动,重新启动计算机后即可生效! " " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
25                 }

26
27             }

28              catch  (Exception ex)
29              {
30                 MessageBox.Show( " 出现错误。/n详细信息: "   +  ex.Message);
31             }

32         }

 

最后附上工具的下载。(不要拍砖头哈)

http://files.cnblogs.com/liuj/WallpaperAutoChanger.rar

原文地址:http://www.cnblogs.com/liuj/archive/2009/10/18/1585678.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值