win7自动壁纸切换小工具AutoDesk二:fade effect的壁纸切换

要实现壁纸切换,可以用函数WinAPI.SystemParametersInfo(20, 1, strSavePath, 1)来实现。

strSavePath为图片的位置,实际测下来该函数并不要求图片一定是bmp格式,jpg|png也都可以的。

函数的原型声明如下:

1 class WinAPI

2     {

3         

4         [DllImport("user32.dll", CharSet=CharSet.Auto)]

5         public static  extern int SystemParametersInfo (int uAction , int uParam , string lpvParam , int fuWinIni);

6     }

该函数的问题就是没有fade效果,只直接换壁纸,非常的突兀。要实现fade效果需要活动桌面,即接口IActiveDesktop.

msdn的介绍说是:

Allows a client program to manage the desktop items and wallpaper on a local computer。

设计到COM编程。下面是IActiveDesktop的c#的实现。

  1 using System;

  2 using System.Collections.Generic;

  3 using System.Linq;

  4 using System.Text;

  5 using System.Runtime.InteropServices;

  6 

  7 namespace AutoDesk

  8 {

  9         public struct WALLPAPEROPT

 10         {

 11             public static readonly int SizeOf = Marshal.SizeOf(typeof(WALLPAPEROPT));

 12         }

 13 

 14         public enum WallPaperStyle : int

 15         {

 16             WPSTYLE_CENTER = 0,

 17             WPSTYLE_TILE = 1,

 18             WPSTYLE_STRETCH = 2,

 19             WPSTYLE_MAX = 3

 20         }

 21 

 22         [Flags]

 23         public enum AD_Apply : int

 24         {

 25             SAVE = 0x00000001,

 26             HTMLGEN = 0x00000002,

 27             REFRESH = 0x00000004,

 28             ALL = SAVE | HTMLGEN | REFRESH,

 29             FORCE = 0x00000008,

 30             BUFFERED_REFRESH = 0x00000010,

 31             DYNAMICREFRESH = 0x00000020

 32         }

 33 

 34         [StructLayout(LayoutKind.Sequential)]

 35         public struct COMPONENTSOPT

 36         {

 37             public static readonly int SizeOf = Marshal.SizeOf(typeof(COMPONENTSOPT));

 38             public int dwSize;

 39             [MarshalAs(UnmanagedType.Bool)]

 40             public bool fEnableComponents;

 41             [MarshalAs(UnmanagedType.Bool)]

 42             public bool fActiveDesktop;

 43         }

 44 

 45         [Flags]

 46         public enum CompItemState : int

 47         {

 48             NORMAL = 0x00000001,

 49             FULLSCREEN = 00000002,

 50             SPLIT = 0x00000004,

 51             VALIDSIZESTATEBITS = NORMAL | SPLIT | FULLSCREEN,

 52             VALIDSTATEBITS = NORMAL | SPLIT | FULLSCREEN | unchecked((int)0x80000000) | 0x40000000

 53         }

 54 

 55         [StructLayout(LayoutKind.Sequential)]

 56         public struct COMPSTATEINFO

 57         {

 58             public static readonly int SizeOf = Marshal.SizeOf(typeof(COMPSTATEINFO));

 59             public int dwSize;

 60             public int iLeft;

 61             public int iTop;

 62             public int dwWidth;

 63             public int dwHeight;

 64             public CompItemState dwItemState;

 65         }

 66 

 67         [StructLayout(LayoutKind.Sequential)]

 68         public struct COMPPOS

 69         {

 70             public const int COMPONENT_TOP = 0x3FFFFFFF;

 71             public const int COMPONENT_DEFAULT_LEFT = 0xFFFF;

 72             public const int COMPONENT_DEFAULT_TOP = 0xFFFF;

 73             public static readonly int SizeOf = Marshal.SizeOf(typeof(COMPPOS));

 74 

 75             public int dwSize;

 76             public int iLeft;

 77             public int iTop;

 78             public int dwWidth;

 79             public int dwHeight;

 80             public int izIndex;

 81             [MarshalAs(UnmanagedType.Bool)]

 82             public bool fCanResize;

 83             [MarshalAs(UnmanagedType.Bool)]

 84             public bool fCanResizeX;

 85             [MarshalAs(UnmanagedType.Bool)]

 86             public bool fCanResizeY;

 87             public int iPreferredLeftPercent;

 88             public int iPreferredTopPercent;

 89         }

 90 

 91         public enum CompType : int

 92         {

 93             HTMLDOC = 0,

 94             PICTURE = 1,

 95             WEBSITE = 2,

 96             CONTROL = 3,

 97             CFHTML = 4

 98         }

 99 

100         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 8)]

101         public struct COMPONENT

102         {

103             private const int INTERNET_MAX_URL_LENGTH = 2084; // = INTERNET_MAX_SCHEME_LENGTH (32) + "://\0".Length +   INTERNET_MAX_PATH_LENGTH (2048)

104             public static readonly int SizeOf = Marshal.SizeOf(typeof(COMPONENT));

105 

106             public int dwSize;

107             public int dwID;

108             public CompType iComponentType;

109             [MarshalAs(UnmanagedType.Bool)]

110             public bool fChecked;

111             [MarshalAs(UnmanagedType.Bool)]

112             public bool fDirty;

113             [MarshalAs(UnmanagedType.Bool)]

114             public bool fNoScroll;

115             public COMPPOS cpPos;

116             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]

117             public string wszFriendlyName;

118             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = INTERNET_MAX_URL_LENGTH)]

119             public string wszSource;

120             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = INTERNET_MAX_URL_LENGTH)]

121             public string wszSubscribedURL;

122 

123             public int dwCurItemState;

124             public COMPSTATEINFO csiOriginal;

125             public COMPSTATEINFO csiRestored;

126         }

127 

128         public enum DtiAddUI : int

129         {

130             DEFAULT = 0x00000000,

131             DISPSUBWIZARD = 0x00000001,

132             POSITIONITEM = 0x00000002,

133         }

134 

135         [Flags]

136         public enum ComponentModify : int

137         {

138             TYPE = 0x00000001,

139             CHECKED = 0x00000002,

140             DIRTY = 0x00000004,

141             NOSCROLL = 0x00000008,

142             POS_LEFT = 0x00000010,

143             POS_TOP = 0x00000020,

144             SIZE_WIDTH = 0x00000040,

145             SIZE_HEIGHT = 0x00000080,

146             POS_ZINDEX = 0x00000100,

147             SOURCE = 0x00000200,

148             FRIENDLYNAME = 0x00000400,

149             SUBSCRIBEDURL = 0x00000800,

150             ORIGINAL_CSI = 0x00001000,

151             RESTORED_CSI = 0x00002000,

152             CURITEMSTATE = 0x00004000,

153             ALL = TYPE | CHECKED | DIRTY | NOSCROLL | POS_LEFT | SIZE_WIDTH |

154                 SIZE_HEIGHT | POS_ZINDEX | SOURCE |

155                 FRIENDLYNAME | POS_TOP | SUBSCRIBEDURL | ORIGINAL_CSI |

156                 RESTORED_CSI | CURITEMSTATE

157         }

158 

159         [Flags]

160         public enum AddURL : int

161         {

162             SILENT = 0x0001

163         }

164 

165         [ComImport]

166         [Guid("F490EB00-1240-11D1-9888-006097DEACF9")]

167         [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]

168         public interface IActiveDesktop

169         {

170             [PreserveSig]

171             int ApplyChanges(AD_Apply dwFlags);

172             [PreserveSig]

173             int GetWallpaper([MarshalAs(UnmanagedType.LPWStr)]  System.Text.StringBuilder pwszWallpaper,

174                 int cchWallpaper,

175                 int dwReserved);

176             [PreserveSig]

177             int SetWallpaper([MarshalAs(UnmanagedType.LPWStr)] string pwszWallpaper, int dwReserved);

178             [PreserveSig]

179             int GetWallpaperOptions(ref WALLPAPEROPT pwpo, int dwReserved);

180             [PreserveSig]

181             int SetWallpaperOptions(ref WALLPAPEROPT pwpo, int dwReserved);

182             [PreserveSig]

183             int GetPattern([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pwszPattern, int cchPattern, int dwReserved);

184             [PreserveSig]

185             int SetPattern([MarshalAs(UnmanagedType.LPWStr)] string pwszPattern, int dwReserved);

186             [PreserveSig]

187             int GetDesktopItemOptions(ref COMPONENTSOPT pco, int dwReserved);

188             [PreserveSig]

189             int SetDesktopItemOptions(ref COMPONENTSOPT pco, int dwReserved);

190             [PreserveSig]

191             int AddDesktopItem(ref COMPONENT pcomp, int dwReserved);

192             [PreserveSig]

193             int AddDesktopItemWithUI(IntPtr hwnd, ref COMPONENT pcomp, DtiAddUI dwFlags);

194             [PreserveSig]

195             int ModifyDesktopItem(ref COMPONENT pcomp, ComponentModify dwFlags);

196             [PreserveSig]

197             int RemoveDesktopItem(ref COMPONENT pcomp, int dwReserved);

198             [PreserveSig]

199             int GetDesktopItemCount(out int lpiCount, int dwReserved);

200             [PreserveSig]

201             int GetDesktopItem(int nComponent, ref COMPONENT pcomp, int dwReserved);

202             [PreserveSig]

203             int GetDesktopItemByID(IntPtr dwID, ref COMPONENT pcomp, int dwReserved);

204             [PreserveSig]

205             int GenerateDesktopItemHtml([MarshalAs(UnmanagedType.LPWStr)] string pwszFileName, ref COMPONENT pcomp, int dwReserved);

206             [PreserveSig]

207             int AddUrl(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)] string pszSource, ref COMPONENT pcomp, AddURL dwFlags);

208             [PreserveSig]

209             int GetDesktopItemBySource([MarshalAs(UnmanagedType.LPWStr)] string pwszSource, ref COMPONENT pcomp, int dwReserved);

210 

211         }

212 

213         /// <summary>

214         /// Summary description for shlobj.

215         /// Written by: Eber Irigoyen

216         /// on: 11/23/2005

217         /// </summary>

218         public class shlobj

219         {

220             public static readonly Guid CLSID_ActiveDesktop =

221                 new Guid("{75048700-EF1F-11D0-9888-006097DEACF9}");

222 

223             public static IActiveDesktop GetActiveDesktop()

224             {

225                 Type typeActiveDesktop = Type.GetTypeFromCLSID(shlobj.CLSID_ActiveDesktop);

226                 return (IActiveDesktop)Activator.CreateInstance(typeActiveDesktop);

227             }

228 

229             public shlobj()

230             {

231                 //

232                 // TODO: Add constructor logic here

233                 //

234             }

235         }

236     

237 }

其使用方法如前面的函数

1 private void switch_to_next_waller()

2         {

3             string strSavePath = Path.Combine(m_deskpick_path, get_random_pick_name());

4             //WinAPI.SystemParametersInfo(20, 1, strSavePath, 1);

5             IActiveDesktop iad = shlobj.GetActiveDesktop();

6             iad.SetWallpaper(strSavePath, 0);

7             iad.ApplyChanges(AD_Apply.ALL | AD_Apply.FORCE | AD_Apply.BUFFERED_REFRESH);

8             System.Runtime.InteropServices.Marshal.ReleaseComObject(iad);

9         }

只需要调用SetWallpaper函数,然后释放该COMObject就可以实现fade效果的切换壁纸。

调用该函数的实际效果是,在win7的主题中有一个未保存的主题,该主题总是包含你切换的最后一张图片。

win7自动壁纸切换小工具AutoDesk二:fade effect的壁纸切换

附上项目文件,供有兴趣的同学参考

http://files.cnblogs.com/linbirg/AutoDesk.rar

你可能感兴趣的:(effect)