C#实现动态桌面背景图片切换 |
您所在的位置:网站首页 › 闹钟屏幕图片壁纸 › C#实现动态桌面背景图片切换 |
问题描述:凝视桌面背景,突感如果桌面背景是变化的像win7一样,该多有意思啊。闹钟瞬间产生一个念头,用C#写一个动态切换桌面背景的图片。如何实现这个想法了,我思考了一会儿,想到了如下的一些需要解决的问题: (1):以何种方式实现切换图片,才能更符合客户的要求。 (2):C#使用什么技术来切换桌面的背景图片。 (3):如何存储和读取图片。 问题解决: (1):以何种方式呈现呢?控制台?winform?最后思考后,决定用windows services来实现。因为它可以随windows启动而启动,而且在用户不知不觉中改变,默默无为做自己的事情,简单。我以前没搞过windows services,在网上search一下,大致明白了其中的原理。于是想做一个简单的例子来实现。例子实现功能:写一个服务,定时提示一个message。代码很快就写完了,可注册后,总是不能启动服务,也没啥提示信息。问题很犀利,我Search一个小时,才知道需要设置服务的属性:允许服务与桌面交互。我设置了,并且重启服务,果然出现提示信息,但这个问题总不能每次都让客户去解决吧,在网上Search解决方案。关键代码如下: 代码 private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e) { ConnectionOptions coOptions = new ConnectionOptions(); coOptions.Impersonation = ImpersonationLevel.Impersonate; ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", coOptions); mgmtScope.Connect(); ManagementObject wmiService; wmiService = new ManagementObject("Win32_Service.Name='" + this.serviceInstaller1.ServiceName + "'"); ManagementBaseObject InParam = wmiService.GetMethodParameters("Change"); InParam["DesktopInteract"] = true; ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null); }
第一个问题总算解决了。 (2):如何切换图片,Search一下,似乎就一个办法,调用系统的API,不过图片只能是BMP格式。代码如下: 代码 #region System Innerface [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")] public static extern int SystemParametersInfo( int uAction, int uParam, string lpvParam, int fuWinIni ); #endregion #region Timer Elapsed private void dynamicTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (picIndex |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |