设为首页 收藏本站
查看: 917|回复: 0

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

[复制链接]

尚未签到

发表于 2015-5-14 11:54:34 | 显示全部楼层 |阅读模式
  要实现壁纸切换,可以用函数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         ///
214         /// Summary description for shlobj.
215         /// Written by: Eber Irigoyen
216         /// on: 11/23/2005
217         ///
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的主题中有一个未保存的主题,该主题总是包含你切换的最后一张图片。
DSC0000.png
  附上项目文件,供有兴趣的同学参考
  http://files.iyunv.com/linbirg/AutoDesk.rar

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-66914-1-1.html 上篇帖子: Win7下Bitmap.Clone方法处理CMYK图片OutOfMemory异常的解决办法 下篇帖子: Win7 激活密钥
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表