今天闲来无事,觉得自己的桌面很是单调,于是自己动手做了一个自动换桌面墙纸的小工具。工具的作法相当简单,主要是想跟大家分享一下。 大家口下留情,我知道很简单,所以,高手就Pass了吧。
用到的最主要的函数就是Windows API中的SystemParametersInfo函数。首先在。Net中调用API函数:
2 static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, string lpvparam, Int32 fuwinIni);
然后定义常变量
为什么是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