如何通过编程方式更改Wallpaper? [英] How to change the Wallpaper programmatically?

查看:141
本文介绍了如何通过编程方式更改Wallpaper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主要壁纸目录的几个子目录中有很多壁纸。


在Windows 7中,如果选择幻灯片,它只会使用图像当前目录,子目录中没有图像。


现在我的想法是编写一个程序,让我选择多个目录并创建一个新的临时目录,其中包含所有这些图像,设置窗口使用所有这些图像的幻灯片,然后删除临时目录。


即使原始文件被删除,Windows也会以某种方式缓存当前的壁纸图片,我认为如果它的幻灯片显示有很多图像会不会发生?


现在的问题是,如何在Windows 7中使用C#设置壁纸幻灯片?


并且谷歌搜索我发现很多人只说bmp或者只是bmp和jpg文件是支持的,这是真的吗?如果是这样,那么当我手动设置壁纸时我怎能只使用png图像呢?




解决方案

您好Ishiirou,


感谢您在此发帖。


>> 谷歌搜索我发现很多人说只支持bmp或只支持bmp和jpg文件,这是
是真的吗?如果是这样,那么当我手动设置壁纸时我怎么能只使用png图像呢?


对于你的问题,你可以将png图像设置为背景图像。请尝试以下代码。

使用Microsoft.Win32; 
使用System;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices;
使用System.Text;
使用System.Threading.Tasks;

命名空间change_Wallpaper2
{
class Program
{
///< summary>
///设置系统参数
///< / summary>
///< param name =" uAction">< / param>
///< param name =" uParam">< / param>
///< param name =" lpvParam">< / param>
///< param name =" fuWinIni">< / param>
///< example>< / example>
///< returns>< / returns>
[DllImport(" user32.dll",EntryPoint =" SystemParametersInfo")]
public static extern int SystemParametersInfo(UAction uAction,int uParam,StringBuilder lpvParam,int fuWinIni);
static void Main(string [] args)
{
GetBackgroud();
SetBackgroud(@" C:\ Users \v-wezan \Desktop\Qimage\2.png");
}
public enum UAction
{
///< summary>
///设置桌面背景图片
///< / summary>
SPI_SETDESKWALLPAPER = 0x0014,
///< summary>
///设置桌面背景图片
///< / summary>
SPI_GETDESKWALLPAPER = 0x0073,
}
公共静态字符串GetBackgroud()
{
StringBuilder s = new StringBuilder(300);
SystemParametersInfo(UAction.SPI_GETDESKWALLPAPER,300,s,0);
返回s.ToString();
}
///< summary>
///设置桌面背景图片
///< / summary>
///< param name =" fileName">图片路径< / param>
///< returns>< / returns>
public static int SetBackgroud(string fileName)
{
int result = 0;
if(File.Exists(fileName))
{
StringBuilder s = new StringBuilder(fileName);
result = SystemParametersInfo(UAction.SPI_SETDESKWALLPAPER,0,s,0x2);
}
返回结果;
}
///< summary>
///设置注册表选项
///< / summary>
///< param name =" optionsName">注册表名称< / param>
///< param name =" optionsData">设置注册表数据< / param>
///< param name =" msg">< / param>
///< returns>< / returns>
public static bool SetOptions(string optionsName,string optionsData,ref string msg)
{
bool returnBool = true;
RegistryKey classesRoot = Registry.CurrentUser;
RegistryKey registryKey = classesRoot.OpenSubKey(@"Control Panel\Desktop",true);
try
{
if(registryKey!= null)
{
registryKey.SetValue(optionsName.ToUpper(),optionsData);
}
其他
{
returnBool = false;
}
}
catch
{
returnBool = false;
msg ="读取注册表时出错";
}
最后
{
classesRoot.Close();
registryKey.Close();
}
返回returnBool;
}
}
}

我的电脑在win10。我没有win7的环境来测试代码。但是在我的计算机中它运行良好。


对于方法SetBackgroud(),如果返回结果0表示它失败。如果它返回结果1,则表示成功。


如果您成功设置了背景图像,但在桌面中没有更改,则可以在我的桌面中使用  SetOptions()方法代码设置  HKEY_CURRENT_USER \ Control
面板\Desktop 
注册表。


我将.png图像设置为我的计算机背景图像。我使用gif来显示结果。




我希望这会有所帮助。


最好的问候,


温迪


I have a lot of wallpapers I have in several sub directories of a main wallpaper directory.

Sadly in Windows 7 if you select a slideshow it only uses the images in the current directory and no images in sub directories.

Now my idea was to write a program that lets me select multiple directories and create a new temp directory with all the images in these, set windows to use a slideshow of all these images and then delete the temp directory.

Windows somehow caches the current wallpaper image even if the original file is deleted, I assume that doesn't happen if its a slideshow with many images?

Now the question is, how do I set a wallpaper slideshow in Windows 7 with C#?

And googling this I found a lot of people saying only bmp or only bmp and jpg files are supported, is this true? If so, how can that be when I can only use png images if I manually set a wallpaper?


解决方案

Hi Ishiirou,

Thank you for posting here.

>>And googling this I found a lot of people saying only bmp or only bmp and jpg files are supported, is this true? If so, how can that be when I can only use png images if I manually set a wallpaper?

For your question, you could set the png image as background image. Please try the following code.

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace change_Wallpaper2
{
    class Program
    {
        /// <summary>
        /// set the parameter of system
        /// </summary>
        /// <param name="uAction"></param>
        /// <param name="uParam"></param>
        /// <param name="lpvParam"></param>
        /// <param name="fuWinIni"></param>
        /// <example></example>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern int SystemParametersInfo(UAction uAction, int uParam, StringBuilder lpvParam, int fuWinIni);
        static void Main(string[] args)
        {
            GetBackgroud();
            SetBackgroud(@"C:\Users\v-wezan\Desktop\Qimage\2.png");
        }
        public enum UAction
        {
            /// <summary>
            /// set the desktop background image
            /// </summary>
            SPI_SETDESKWALLPAPER = 0x0014,
            /// <summary>
            /// set the desktop background image
            /// </summary>
            SPI_GETDESKWALLPAPER = 0x0073,
        }
        public static string GetBackgroud()
        {
            StringBuilder s = new StringBuilder(300);
            SystemParametersInfo(UAction.SPI_GETDESKWALLPAPER, 300, s, 0);
            return s.ToString();
        }
        /// <summary>
        /// set the desktop background image
        /// </summary>
        /// <param name="fileName">the path of image</param>
        /// <returns></returns>
        public static int SetBackgroud(string fileName)
        {
            int result = 0;
            if (File.Exists(fileName))
            {
                StringBuilder s = new StringBuilder(fileName);
                result = SystemParametersInfo(UAction.SPI_SETDESKWALLPAPER, 0, s, 0x2);
            }
            return result;
        }
        /// <summary>
        /// set the option of registry
        /// </summary>
        /// <param name="optionsName">the name of registry</param>
        /// <param name="optionsData">set the data of registry</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool SetOptions(string optionsName, string optionsData, ref string msg)
        {
            bool returnBool = true;
            RegistryKey classesRoot = Registry.CurrentUser;
            RegistryKey registryKey = classesRoot.OpenSubKey(@"Control Panel\Desktop", true);
            try
            {
                if (registryKey != null)
                {
                    registryKey.SetValue(optionsName.ToUpper(), optionsData);
                }
                else
                {
                    returnBool = false;
                }
            }
            catch
            {
                returnBool = false;
                msg = "Error when read the registry";
            }
            finally
            {
                classesRoot.Close();
                registryKey.Close();
            }
            return returnBool;
        }
    }
}

My computer in win10. I do not have environment of win7 to test the code. But in my computer it works well.

For the method SetBackgroud(), if it return the result 0 means it fails. If it return the result 1, it means success.

If you set the background image successfully, but it does not changed in desktop, you could to use the SetOptions() method in my code to set the HKEY_CURRENT_USER\Control Panel\Desktop registry.

I set the .png image to my computer background image. I use a gif to show the result.

I hope this would be helpful.

Best Regards,

Wendy


这篇关于如何通过编程方式更改Wallpaper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆