如何以编程方式更改Windows桌面墙纸? [英] How do I change my Windows desktop wallpaper programmatically?

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

问题描述

我希望使用C#为Windows XP设置墙纸.我已经开发了代码,因此它可以在Windows 7中完美运行,但显然与XP不同.我将该墙纸添加为资源,将其编译操作设置为内容",然后始终复制.奇怪的是,它在桌面的属性对话框中设置了正确的墙纸名称.但是,未设置墙纸.我的代码如下:

I'd wish to set a wallpaper for Windows XP using C#. I've developed the code so it perfectly works in Windows 7, but apparently it's not the same for 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);
    }
}

在调用Wallpaper.Set("wpapername")时,它将从项目资源中获取墙纸.它适用于Win7,但不适用于WinXP.我在做错什么吗?

When calling Wallpaper.Set("wpapername"), it gets the wallpaper from project resources. It works on Win7, but not on WinXP. Am I doing something wrong?

推荐答案

好吧,这有点尴尬,但是我将用发现的问题回答我自己的问题.

Well, this is a bit awkward, but I'll answer my own question with what I found.

我不得不在接受的答案中重用更多代码.此处 . 基本上,XP中的问题是它需要使用bmp文件,因此我使用前面的示例并做了一些调整,设法将项目资源转换为bmp文件. Set方法可以通过以下方式完美工作:

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. The Set method works perfectly this way:

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);

}

重要的部分在此代码的第三行(System.Drawing.Image.FromFile(Path.GetFullPath(wpaper));).

The important part is on the third line of this code (System.Drawing.Image.FromFile(Path.GetFullPath(wpaper));).

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

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