简体   繁体   English

如何以编程方式更改我的 Windows 桌面墙纸?

[英]How do I change my Windows desktop wallpaper programmatically?

I'd wish to set a wallpaper for Windows XP using C#.我想使用 C# 为 Windows XP 设置壁纸。 I've developed the code so it perfectly works in Windows 7, but apparently it's not the same for XP.我已经开发了代码,因此它可以完美地在 Windows 7 中运行,但显然它与 XP 不同。 I add that wallpaper as a resource, set its compile action as Content and Always copy.我将该墙纸添加为资源,将其编译操作设置为内容并始终复制。 It, curiously, sets the correct Wallpaper name inside the Desktop's properties dialog.奇怪的是,它在桌面的属性对话框中设置了正确的墙纸名称。 However, the wallpaper is not set.但是,没有设置壁纸。 My code looks like this:我的代码如下所示:

public sealed class Wallpaper
{
    Wallpaper() { }

    const int SPI_SETDESKWALLPAPER = 20;
    const int SPIF_UPDATEINIFILE = 0x01;
    const int SPIF_SENDWININICHANGE = 0x02;

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

    public enum Style : int
    {
        Tiled,
        Centered,
        Stretched
    }

    public static void Set(string wpaper, Style style)
    {
        RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
        if (style == Style.Stretched)
        {
            key.SetValue(@"WallpaperStyle", 2.ToString());
            key.SetValue(@"TileWallpaper", 0.ToString());
        }

        if (style == Style.Centered)
        {
            key.SetValue(@"WallpaperStyle", 1.ToString());
            key.SetValue(@"TileWallpaper", 0.ToString());
        }

        if (style == Style.Tiled)
        {
            key.SetValue(@"WallpaperStyle", 1.ToString());
            key.SetValue(@"TileWallpaper", 1.ToString());
        }

        string tempPath = "Resources\\"+wpaper;
        SystemParametersInfo(SPI_SETDESKWALLPAPER,
            0,
            tempPath,
            SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
    }
}

When calling Wallpaper.Set("wpapername"), it gets the wallpaper from project resources.当调用Wallpaper.Set("wpapername") 时,它从项目资源中获取壁纸。 It works on Win7, but not on WinXP.它适用于 Win7,但不适用于 WinXP。 Am I doing something wrong?难道我做错了什么?

Well, this is a bit awkward, but I'll answer my own question with what I found.好吧,这有点尴尬,但我会用我的发现来回答我自己的问题。

I had to reuse more code from the accepted answer here .我不得不从这里接受的答案中重用更多代码。 Basically the problem in XP was that it needed to use a bmp file, so I managed to convert a project resource to a bmp file using that previous example and a little of tweaking.基本上,XP 中的问题是它需要使用 bmp 文件,所以我设法使用前面的示例和一些调整将项目资源转换为 bmp 文件。 The Set method works perfectly this way: Set 方法以这种方式完美运行:

public static void Set(string wpaper, Style style)
{
    using(System.Drawing.Image img = System.Drawing.Image.FromFile(Path.GetFullPath(wpaper)))
    {
        string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");

        img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);

    }

    RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);

    if (style == Style.Stretched)
    {
        key.SetValue(@"WallpaperStyle", 2.ToString());

        key.SetValue(@"TileWallpaper", 0.ToString());

    }

    if (style == Style.Centered)
    {
        key.SetValue(@"WallpaperStyle", 1.ToString());

        key.SetValue(@"TileWallpaper", 0.ToString());

    }

    if (style == Style.Tiled)
    {
        key.SetValue(@"WallpaperStyle", 1.ToString());

        key.SetValue(@"TileWallpaper", 1.ToString());

    }

    SystemParametersInfo(SPI_SETDESKWALLPAPER,
        0,
        tempPath,
        SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);

}

The important part is on the third line of this code ( System.Drawing.Image.FromFile(Path.GetFullPath(wpaper)); ).重要的部分在此代码的第三行( System.Drawing.Image.FromFile(Path.GetFullPath(wpaper)); )。

For a good reliable solution.一个好的可靠的解决方案。

Add the foillowing class to your project将以下类添加到您的项目中

using Microsoft.Win32;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;

namespace XXXNAMESPACEXXX
{
    public class Wallpaper
    {
        public enum Style : int
        {
            Tiled,
            Centered,
            Stretched
        }

        [DllImport("user32.dll")]
        public static extern Int32 SystemParametersInfo(UInt32 action, UInt32 uParam, String vParam, UInt32 winIni);

        public static readonly UInt32 SPI_SETDESKWALLPAPER = 0x14;
        public static readonly UInt32 SPIF_UPDATEINIFILE = 0x01;
        public static readonly UInt32 SPIF_SENDWININICHANGE = 0x02;

        public static bool Set(string filePath, Style style)
        {
            bool Success = false;
            try
            {
                Image i = System.Drawing.Image.FromFile(Path.GetFullPath(filePath));

                Set(i, style);

                Success = true;

            }
            catch //(Exception ex)
            {
                //ex.HandleException();
            }
            return Success;
        }

        public static bool Set(Image image, Style style)
        {
            bool Success = false;
            try
            {
                string TempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");

                image.Save(TempPath, ImageFormat.Bmp);

                RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);

                switch (style)
                {
                    case Style.Stretched:
                        key.SetValue(@"WallpaperStyle", 2.ToString());

                        key.SetValue(@"TileWallpaper", 0.ToString());

                        break;

                    case Style.Centered:
                        key.SetValue(@"WallpaperStyle", 1.ToString());

                        key.SetValue(@"TileWallpaper", 0.ToString());

                        break;

                    default:
                    case Style.Tiled:
                        key.SetValue(@"WallpaperStyle", 1.ToString());

                        key.SetValue(@"TileWallpaper", 1.ToString());

                        break;

                }

                SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, TempPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);

                Success = true;

            }
            catch //(Exception ex)
            {
                //ex.HandleException();
            }
            return Success;
        }

    }

}

Note: Replace XXXNAMESPACEXXX with the default namespace of your project.注意:将 XXXNAMESPACEXXX 替换为您项目的默认命名空间。

Windows 7的

Then it can be used like the following:然后它可以像下面这样使用:

string FilePath = TxtFilePath.Text;

Wallpaper.Set(FilePath, Wallpaper.Style.Centered);

It can also be used like this:它也可以这样使用:

if(Wallpaper.Set(FilePath, Wallpaper.Style.Centered))
{
    MessageBox.Show("Your wallpaper has been set to " + FilePath);

}
else
{
    MessageBox.Show("There was a problem setting the wallpaper.");

}

This is verfied working on Windows XP, 7, 8, 8.1 and Windows 10.这已在 Windows XP、7、8、8.1 和 Windows 10 上得到验证。

Note It is worth bearing in mind that this method will bypass any desktop wallpaper security restictions applied by network admin.注意值得记住的是,此方法绕过网络管理员应用的任何桌面壁纸安全限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM