Powershell - 使桌面背景更改立即生效

时间:2014-09-10 20:26:31

标签: windows winapi powershell

我正在运行一个PowerShell脚本来将背景更改为某组颜色。我想这样做而不重新启动,但遗憾的是无法让更改立即在Windows 7/8平台上生效。我在网上找到了很多解决方案,但我找不到适合我的解决方案。我认为它可能与设置SystemParametersInfo有关,但我不确定。我已经看过一些解决方案,并为自己尝试过,但我也无法让它们工作。注册表项更新只是找到,但更改不会生效,直到我重新启动后。以下是我到目前为止,如果有人看到任何我可以做的不同,我会很感激帮助!

backgroundtest.ps1

Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;


namespace Background 
{
    public class Setter {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);
        public const int UpdateIniFile = 0x01;
        public const int SendWinIniChange = 0x02;
        public const int SetDesktopBackground = 20; <# following examples online to set parameters #>

        public static void SetBackground() {
            SystemParametersInfo(SetDesktopBackground, 0, "", UpdateIniFile | SendWinIniChange);
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
            key.SetValue(@"WallPaper", 0); <#remove wallpaper#>
            RegistryKey key2 = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);
            key2.SetValue(@"Background", "0 118 163"); <#set background to new color>
            key.Close();
            key2.Close();
         }

    }


}

"@

[Background.Setter]::SetBackground() 

3 个答案:

答案 0 :(得分:6)

更改系统颜色的文档化方法是SetSysColors函数。

这会将WM_SYSCOLORCHANGE消息发送到所有顶级窗口,以通知他们更改。

我已更新您的课程以清除背景并将颜色设置为紫色。它需要复制到PowerShell的东西。请注意,我声明SetSysColors的方式一次只能更改一种颜色。

public class Setter {
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SetSysColors(int count, [In] ref int index, [In] ref int colour);

    public const int UpdateIniFile = 0x01;
    public const int SendWinIniChange = 0x02;
    public const int SetDesktopBackground = 20;
    public const int COLOR_BACKGROUND = 1;

    public static void SetBackground() {
        SystemParametersInfo(SetDesktopBackground, 0, "", UpdateIniFile | SendWinIniChange);
        int index = COLOR_BACKGROUND;
        int colour = 0xFF00FF;
        SetSysColors(1, ref index, ref colour);
    }
}

答案 1 :(得分:4)

昨天是我的第一次Powershell体验,我很遗憾我需要做的事情。要将桌面背景更改为纯色,首先需要删除墙纸,然后可以使用SetSysColors功能立即更改桌面背景。这个链接帮了我很大的忙。 http://gallery.technet.microsoft.com/scriptcenter/Change-window-borderdesktop-609a6fb2

希望这能帮助某人帮助我。

更新代码

$code = @'
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using Microsoft.Win32;


namespace Background 
{
    public class Setter {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =true)]
        private static extern int SetSysColors(int cElements, int[] lpaElements, int[] lpRgbValues);
        public const int UpdateIniFile = 0x01;
        public const int SendWinIniChange = 0x02;
        public const int SetDesktopBackground = 0x0014;
        public const int COLOR_DESKTOP = 1;
        public int[] first = {COLOR_DESKTOP};


        public static void RemoveWallPaper() {
        SystemParametersInfo( SetDesktopBackground, 0, "", SendWinIniChange | UpdateIniFile );
        RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
        key.SetValue(@"WallPaper", 0);
        key.Close();
        }

        public static void SetBackground(byte r, byte g, byte b) {
            RemoveWallPaper();
            System.Drawing.Color color= System.Drawing.Color.FromArgb(r,g,b);
            int[] elements = {COLOR_DESKTOP};
            int[] colors = { System.Drawing.ColorTranslator.ToWin32(color) }; 
            SetSysColors(elements.Length, elements, colors);
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);
            key.SetValue(@"Background", string.Format("{0} {1} {2}", color.R, color.G, color.B));
            key.Close();

        }

    }


}

'@
Add-Type -TypeDefinition $code -ReferencedAssemblies System.Drawing.dll -PassThru
Function Set-OSCDesktopColor
{
    <# Powershell function to remove desktop background and set background to colors we want #>
    Process
    {
        [Background.Setter]::SetBackground(0,118,163)
        return
    }
}

Set-OSCDesktopColor

答案 2 :(得分:2)

我抓住了Wheatfairies代码,对其进行了更新并在(使用归因)中发布到PowerShell Gallery: https://www.powershellgallery.com/packages/Set-DesktopBackGround/1.0.0.0/DisplayScript

所以现在每个人都可以输入: Install-Script -Name Set-DesktopBackGround

得到它。

干杯!